Select id,sum(case when state = '03' then num else 0 end) - sum(case when state = '01' then num else 0 end)
group by id
group by id
解决方案 »
- 请教查询怎么写
- 奇怪問題,我同一查詢語句,我打開了兩個查詢分析器,當執行第一個時好久沒有回應,然後我再執行第二個中的一下就查出來了,第一個還沒有出來,當我關掉第一個,執行第二個時又沒反應了,執行第一個結果一下就查出來了。
- sql 语句 如何将多个逗号字符串拆分成多条数据
- 急急求..哪位大哥知道哪有SQL2000开发版下载啊? 最好英文的, 没有的话中文也行
- 关于数据库范式问题 求解答 谢谢 很重要 跪谢
- 怎样删除一个表中某个字段重复的列呀,
- 在vfp7的Grid中彈出窗體選擇數據后,無法使用鍵盤到下個單元格
- 问一个SQL 语句
- 请大家给出个题目吧!分数不是问题!小弟就要面试了!回复越多越好!
- ANSI_NULLS,ANSI_WARNINGS在存储过程中怎样启用?
- 两个表的修改问题。
- 问专家:跨库执行查询的性能问题!
Select id,sum(case when state = '03' then num else 0 end) - sum(case when state = '01' then num else 0 end)
from 表
group by id
2:
Select id,sum(case state when '03' then num when '01' then -num else 0 end)
from 表
group by id
insert t1 select 2, '01',12
union all select 2 , '03',11
union all select 3 , '03',15
union all select 5 , '01',10
union all select 3 , '01',132
union all select 2 , '01',2
union all select 5 , '03',11:
Select id,sum(case when state = '03' then num else 0 end) - sum(case when state = '01' then num else 0 end)
from t1
group by id
id
----------- -----------
2 -3
3 -117
5 -9(所影响的行数为 3 行)
2:
Select id,sum(case state when '03' then num when '01' then -num else 0 end)
from t1
group by id
id
----------- -----------
2 -3
3 -117
5 -9(所影响的行数为 3 行)
- isnull(sum(case when state = '01' then num else 0 end),0)
from table1 group by ID