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 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)