【问题标题】:SQL Query : Is it possible to project a four results in a single query?SQL 查询:是否可以在单个查询中投影四个结果?
【发布时间】:2019-11-08 18:11:46
【问题描述】:

目前,我正在执行四个查询来预测基于过去 24 小时、上周、上个月和上个月的结果。是否可以在单个查询中投影四个结果?

【问题讨论】:

  • Oracle 还是 Postgres?这是两种不同的 DBMS 产品
  • 到目前为止你尝试了什么?
  • 我在我的应用程序中尝试了 4 个查询,但都无效。

标签: sql oracle performance


【解决方案1】:

如果您需要与当前日期相比最近一周、最近一个月、最近一年,那么您可以使用 oracle 中的条件聚合来完成,如下所示:

Select sum(case when trunc(date_col) = trunc(sysdate-1) then 1 end) as last_one_day,
sum(case when trunc(date_col) between trunc(sysdate-7) and trunc(sysdate-1) then 1 end) as last_one_week,
sum(case when trunc(date_col) between trunc(add_months(sysdate,-1)) and trunc(sysdate-1) then 1 end) as last_one_month,
sum(case when trunc(date_col) between trunc(add_months(sysdate,-12)) and trunc(sysdate-1) then 1 end) as last_one_year
From your_table
Where type = ...

干杯!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多