有这么个表
sp_id num
1001 2
1001 3
1001 -1
1001 -3
1002 10
1002 2
1002 -5
怎样实现这样的查询结果?
sp_id num num
1001 5 -4
1002 12 -5
也就是把同一个sp_id号的num正数相加,负数相加进行统计.
sql语句该如何写呢?
sp_id num
1001 2
1001 3
1001 -1
1001 -3
1002 10
1002 2
1002 -5
怎样实现这样的查询结果?
sp_id num num
1001 5 -4
1002 12 -5
也就是把同一个sp_id号的num正数相加,负数相加进行统计.
sql语句该如何写呢?
解决方案 »
- Delphi中indy8设置Authentication验证不通过
- 关于fastreport4.0打印debgird里内容的问题
- DBGRID显示时,为什么数据库中单精度数值(4位小数位),在DBGrid中显示长达12位之多?
- 救急:有没有人碰到过类似的问题!
- 如何用Tmaskedit控件限定输入格式为G-121形式?(在线)
- 如何拦截memo的上下翻页的消息?
- 25岁了,散分!
- skinengine问题,急等
- 在dbgrid数值字段内输入23.36.36时就出错,不知道应该怎么解决
- Dim doc As Object 翻译成DELPHI的语句是什么?
- 公司全线上下换液晶,再散
- SaveDialog保存不了???郁闷,帮忙啊!!!!!!
(select sp_id,sum(num) as num from table
where num>0 group by sp_id) as a,
(select sp_id,sum(num) as num from table
where num<0 group by sp_id) as b
where a.sp_id=b.sp_id
(select sum(num) from table where sp_id = a.sp_id and num > 0) as num,
(select sum(num) from table where sp_id = a.sp_id and num < 0) as num1,
from table as a
(Select Sum(num) from 表名 i where i.sp_id = x.sp_id and num >= 0) as PosiNum,
(Select Sum(num) from 表名 j where j.sp_id = x.sp_id and num < 0) as NegiNum
from 表名 x
group by sp_id
order by sp_id