急求一个SQL查询的语句问题

来源:百度知道 编辑:UC知道 时间:2024/06/24 19:23:51
就是表里有个state字段,这个字段里的数据有使用,初次使用,还有个num字段,它里面是10,15,我现在能查出来的结果是:
state num
使用 10
初次使用 15
但我想要的结果是:
state num
在用 25
这个在用是我求那两个的和时起的一个名字,我界面列表要显示的也是"在用"

select '在用' as state,sum(num) as num from biao where state in ('使用','初次使用')

select sum(num) from 表名 where state='初次使用' or state='使用'

可以用视图来完成
create view plus as
select (a+b) as c from test

注:a,b代表两个想要相加的字段,C是相加后的字段

建完查询看看
select * from plus

with temp as
(select (case when a.state='使用' then '初次使用' when a.state='初次使用' then '初次使用' else '未使用' end) as state,
a.num from db2admin.test a)

select (case when min(num)<sum(num) then '在用' else '初次使用' end) as state,sum(num) as num from temp a
group by a.state
我测试过了,给分吧