sql语句包含子查询怎么进行分组

tangdao999 2011-08-03 09:54:38

select date(orderTime) as '日期', count(*) as '总笔数',
(select count(*) from morder where orderTime between '2011-05-10' and '2011-05-20' and

payStatus=1 ) as '成功笔数',
(select count(*) from morder where orderTime between '2011-05-10' and '2011-05-20' and

payStatus=0) as '未支付笔数',
(select count(*) from morder where orderTime between '2011-05-10' and '2011-05-20' and

payStatus=3) as '冲正笔数',
(select sum(amount)*0.01 from morder where orderTime between '2011-05-10' and '2011-05-20'

and payStatus=1) as '成功总金额'
from morder where orderTime between '2011-05-10' and '2011-05-20' GROUP by date(orderTime)


这是我的sql语句

我想实现成这样的

日期 总笔数 成功笔数 未支付笔数 冲正笔数 成功总金额
5.10 33 0 33 0 1.1
5.11 45 2 40 3 3.2
总计 78 2 73 3 4.3


要实现这样的
怎么办
...全文
469 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tangdao999 的回复:]
数据库用的是mysql
[/Quote]
建议去MYSQL问问
tangdao999 2011-08-03
  • 打赏
  • 举报
回复

谢谢
AcHerat 2011-08-03
  • 打赏
  • 举报
回复
MYSQL应该也可以,楼主去问问MYSQL版,在其他数据库那里。
tangdao999 2011-08-03
  • 打赏
  • 举报
回复
case when 在mysql中可以使用吗??
xuam 2011-08-03
  • 打赏
  • 举报
回复
有必要这么复杂么?
用case when 语句,不需要子查询语句.
tangdao999 2011-08-03
  • 打赏
  • 举报
回复
数据库用的是mysql
快溜 2011-08-03
  • 打赏
  • 举报
回复
select date(orderTime) as '日期', count(*) as '总笔数',
成功笔数=sum(case when payStatus=1 then 1 else 0 end),
未支付笔数=sum(case when payStatus=0 then 1 else 0 end),
冲正笔数=sum(case when payStatus=3 then 1 else 0 end),
成功总金额=sum(case when payStatus=1 then amount else 0 end)*0.01,
from morder
where orderTime between '2011-05-10' and '2011-05-20'
GROUP by date(orderTime)
AcHerat 2011-08-03
  • 打赏
  • 举报
回复
最后加个with rollup
AcHerat 2011-08-03
  • 打赏
  • 举报
回复
不要用子查询,用 sum(case when .. then .. end)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧