qq_27446037 2017-08-01 07:53 采纳率: 0%
浏览 3077

请教大神怎么解决sql中多个select合并问题

SELECT months, total, ruzhu, cancels
FROM (SELECT to_char(ordertime,'mm') as months,count(*) as total, 0 ruzhu, 0 cancels
FROM tb_order where B_ID = '1'
GROUP BY to_char(ordertime,'mm')

UNION ALL

SELECT to_char(ordertime,'mm') as months, 0 total, count(*) as ruzhu, 0 cancels
FROM tb_order where B_ID = '1' and (status = 1 or status = 2 or status = 3)
GROUP BY to_char(ordertime,'mm')

UNION ALL

SELECT to_char(ordertime,'mm') as months, 0 total, 0 ruzhu, count(*) as cancels
FROM tb_order where B_ID = '1' and (status = 6 or status = 7)
GROUP BY to_char(ordertime,'mm'))
查询结果如下图
图片说明

我想想要的结果是
moths total ruzhu cancels

08 1 1 0
07 7 5 2

  • 写回答

4条回答 默认 最新

  • 呓语的风 2017-08-01 08:00
    关注

    把你的查询语句用select包起来,然后按月份分组
    select sum(a.total) total, sum(a.ruzhu) ruzhu, sum(a.cancels) cancels from ( 你的sql ) a group by a.months

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料