在oracle数据库中,要求两个字段的和可以用sql语句(前提是两个字段都是数字型):
SELECT num1+num2 AS num FROM table_name;
其中num1、num2是要求和的两个字段,num是新命名的和字段,table_name是要查询的表名。
扩展资料:
Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。
可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。
1,如果都是数字类型的直接把这两个字段相加
select
a+b as ab
from S ;
或者你的意思是 select sum(a+b) from S;
2,如果是不同的字段类型就不能求和了,但是可以使用“||”或者CONCAT()函数
21 select a||b from S;
22 select concat(a,b) from S;
参考资料来源:百度百科-Oracle数据库
可以创建一个函数,然后来求和。
1先创建一个函数
CREATE OR REPLACE FUNCTION res_exp( p_Express varchar)RETURN NUMBER
IS
v_RESULT NUMBER;
v_Sql varchar(200);
BEGIN
v_Sql := 'SELECT ' || p_Express || ' FROM dual';
EXECUTE IMMEDIATE ( v_Sql ) INTO v_RESULT ;
RETURN v_RESULT;
END res_exp;
2通过正则表达式,将PARA_VALUE转为求和表达式,我这里用自己的例子来做:
with t as (select 'item1=1,item2=2,item3=3' aa from dual
union all
select 'item1=1,item2=2,item3=3' aa from dual
)
SELECT res_exp(regexp_replace(regexp_REPLACE(aa, ',item[[:digit:]]=', '+'), 'item[[:digit:]]=', '')) res
FROM t
你的可以这样写
SELECT res_exp(regexp_replace(regexp_REPLACE(PARA_VALUE, ',item[[:digit:]]=', '+'), 'item[[:digit:]]=', '')) resFROM 你的表名
SELECT CASE
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '00:00:00' AND
'00:59:59' THEN
0
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '01:00:00' AND
'01:59:59' THEN
1
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '02:00:00' AND
'02:59:59' THEN
2
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '03:00:00' AND
'03:59:59' THEN
3
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '04:00:00' AND
'04:59:59' THEN
4
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '05:00:00' AND
'05:59:59' THEN
5
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '06:00:00' AND
'06:59:59' THEN
6
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '07:00:00' AND
'07:59:59' THEN
7
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '08:00:00' AND
'08:59:59' THEN
8
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '09:00:00' AND
'09:59:59' THEN
9
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '10:00:00' AND
'10:59:59' THEN
10
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '11:00:00' AND
'11:59:59' THEN
11
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '12:00:00' AND
'12:59:59' THEN
12
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '13:00:00' AND
'13:59:59' THEN
13
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '14:00:00' AND
'14:59:59' THEN
14
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '15:00:00' AND
'15:59:59' THEN
15
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '16:00:00' AND
'16:59:59' THEN
16
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '17:00:00' AND
'17:59:59' THEN
17
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '18:00:00' AND
'18:59:59' THEN
18
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '19:00:00' AND
'19:59:59' THEN
19
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '20:00:00' AND
'20:59:59' THEN
20
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '21:00:00' AND
'21:59:59' THEN
21
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '22:00:00' AND
'22:59:59' THEN
22
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '23:00:00' AND
'23:59:59' THEN
23
END TIME_NUM,
SERV_NUMBER,
SUM(A) + SUM(B) + SUM(C) TOTAL
FROM TABLE
GROUP BY CASE
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '00:00:00' AND
'00:59:59' THEN
0
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '01:00:00' AND
'01:59:59' THEN
1
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '02:00:00' AND
'02:59:59' THEN
2
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '03:00:00' AND
'03:59:59' THEN
3
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '04:00:00' AND
'04:59:59' THEN
4
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '05:00:00' AND
'05:59:59' THEN
5
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '06:00:00' AND
'06:59:59' THEN
6
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '07:00:00' AND
'07:59:59' THEN
7
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '08:00:00' AND
'08:59:59' THEN
8
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '09:00:00' AND
'09:59:59' THEN
9
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '10:00:00' AND
'10:59:59' THEN
10
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '11:00:00' AND
'11:59:59' THEN
11
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '12:00:00' AND
'12:59:59' THEN
12
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '13:00:00' AND
'13:59:59' THEN
13
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '14:00:00' AND
'14:59:59' THEN
14
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '15:00:00' AND
'15:59:59' THEN
15
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '16:00:00' AND
'16:59:59' THEN
16
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '17:00:00' AND
'17:59:59' THEN
17
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '18:00:00' AND
'18:59:59' THEN
18
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '19:00:00' AND
'19:59:59' THEN
19
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '20:00:00' AND
'20:59:59' THEN
20
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '21:00:00' AND
'21:59:59' THEN
21
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '22:00:00' AND
'22:59:59' THEN
22
WHEN TO_CHAR(START_TIME, 'HH24:mi:ss') BETWEEN '23:00:00' AND
'23:59:59' THEN
23
END,
SERV_NUMBER
以上就是关于在oracle数据库中,要求两个字段的和要怎么写sql语句全部的内容,包括:在oracle数据库中,要求两个字段的和要怎么写sql语句、oracle中怎么将每条记录中的值加起来呢、0oracle大神们: 求解这么样一次性查询一天每小时的数据累加按照 号码分类,每小时都统计等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)