【问题标题】:separating information based on weekends and weekdays in oracle sql在oracle sql中根据周末和工作日分离信息
【发布时间】:2021-10-21 21:55:15
【问题描述】:

我想得到以下输出

enrollment_month weekend_revenue weekday_revenue
jun 550 790
may 1570 2020

这是我的代码:

select g.enrollment_month, sum(g.total_revenue) as weekday_revenue 
from gracieMusicFact g
where g.lesson_day in ('Monday','Tuesday','Wednesday','Thursday','Friday')
group by enrollment_month;

select g.enrollment_month, sum(g.total_revenue) as weekend_revenue 
from gracieMusicFact g
where g.lesson_day in ('Saturday','Sunday')
group by enrollment_month;

Union 只给我一个weekday_revenue 或weekday_revenue。如何使用不同的条件创建两个不同的选择语句?

谢谢

编辑:使用 union all 给我这个输出:

| enrollment_month   | weekday_revenue |

| jun|  790 |
| may| 2020 |
| jun|  550 |
| may| 1570 |

【问题讨论】:

  • 你可以加入基于enrollment_month的两个select语句

标签: sql oracle select


【解决方案1】:

使用条件聚合:

select g.enrollment_month,
       sum(case when g.lesson_day in ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday') then g.total_revenue else 0 end) as weekday_revenue,
       sum(case when g.lesson_day in ('Saturday', 'Sunday') then g.total_revenue else 0 end) as weekend_revenue
from gracieMusicFact g
group by enrollment_month;

【讨论】:

  • 天哪,你是个传奇人物——这很完美!谢谢!
  • @oraclehelpplease,然后接受答案。
  • not in ('Saturday', 'Sunday') 或 not like 'S%' 会比 in ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday') 少输入。
【解决方案2】:

我认为您需要使用 UNION ALL

Union 和 Union All 唯一的区别是 Union All 不会删除重复的行或记录,而是从所有表中选择满足您的具体查询条件的所有行并将它们组合到结果表中. ... 而 UNION ALL 可用于所有数据类型列。

What is the difference between UNION and UNION ALL?

【讨论】:

  • 不幸的是联合都不起作用。我将编辑我的帖子以显示它的作用。谢谢你的回复!
猜你喜欢
  • 2019-07-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2014-09-20
相关资源
最近更新 更多