易截截图软件、单文件、免安装、纯绿色、仅160KB

一个简单的SQL语句

select
sum(case a when 0 then b else 0 end) as qty1,
sum(case a when 1 then b else 0 end) as qty2,
sum(b) - qty1 - qty2 as qty3  //qty3我是想实现这样的功能,但我知道这样写肯定不对,不知道怎么能实现?
from table1
group by...
SQL code:
select
sum(case a when 0 then b else 0 end) as qty1,
sum(case a when 1 then b else 0 end) as qty2,
sum(b) - sum(case a when 0 then b else 0 end)-sum(case a when 1 then b else 0 end) as qty3
from table1
group by...


sum(b) - qty1 - qty2 as qty3
等同于
sum(case a when 0 then 0 when 1 then 0 else b end) as qty3

sum(b) - qty1 - qty2 as qty3 替换成sum(b)-sum(case when a=0 or a=1 then b else 0)

或者:

select qty1,qty2,b-qty1-qty2
from(
select
sum(case a when 0 then b else 0 end) as qty1,
sum(case a when 1 then b else 0 end) as qty2,
sum(b) as b
from table1
group by..) b


就这个就可以了,看起来清爽,呵呵,谢谢各位
引用
sum(b) - qty1 - qty2 as qty3
等同于
sum(case a when 0 then 0 when 1 then 0 else b end) as qty3


select
sum(case a when 0 then b else 0 end) as qty1,
sum(case a when 1 then b else 0 end) as qty2,
sum(b) - qty1 - qty2 as qty3  //qty3我是想实现这样的功能,但我知道这样写肯定


相关问答:

jsp链接sql2000的疑问?



type Exception report


message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jas ......

sql性能求助 - MS-SQL Server / 疑难问题

场景如下:
客户把备份好的数据库,发给我,我在本机还原后,运行写好的存储过程,比较快,并且在实施那边运行同样比较快。但是当实施在客户那边运行的时候速度就非常的慢,时间超出了程序的时间限制。远程在客户那 ......

sql小小的疑问 - .NET技术 / C#

可能因为工作的原因 接触数据库这块比较少,之前都是做程序这块,数据库这块都有专门的人来做 分工都很明细 所以对数据库这一块完全不了解。前段时间 去面试了几家公司 几乎都是在数据库这块挂掉的 连个简单的SQ ......

求一SQL - MS-SQL Server / 基础类

tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......

如何使用MySQL - MS-SQL Server / 基础类

刚刚接触MySQL,不知道该如何去查看数据,插入数据,创建数据库、表,用哪为前辈能指教一二?

MySQL 5.1参考手册

引用
兄弟你会结贴吗?
mysql 参考手册

baidu

MySQL官方文档 http://dev.mysql.com/doc ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号