sql难题
有一个表 jkx
字段 ID : varchar2
length: number
xh : varchar2
怎么查询出jkx表length的总长度,和字段xh以'J'开头的表length的总长度,用一条sql语句写。
也就是把:
select sum(length) from res_jkx_info;
select sum(length) from res_jkx_info where xh like 'J%';
合并成一条sql语句。
我都想了好几天了,都没想出怎么写,求各位帮帮忙。
sql
------解决方案--------------------select sum(case when xh like 'J%' then length*2 else length end) from res_jkx_info
------解决方案--------------------select sum(t.length),sum(decode(substr(t.xh,0,1),'J',t.length,0)) from jkx t
------解决方案--------------------
with t1 as
(
select 1 id,10 length,'aaa' xh from dual union all
select 2 id,20 length,'jbb' xh from dual union all
select 3 id,30 length,'cc' xh from dual union all
select 4 id,40 length,'jjj' xh from dual
)
select sum(length) c1,
sum(case when xh like 'j%' then length else 0 end) c2
from t1
c1 c2
----------------------
1 100 60
------解决方案--------------------SELECT SUM(T. length),
SUM(CASE
WHEN T.xh LIKE 'J%' THEN
T. length
ELSE
0
END)
FROM jkx T