sql - SQL 查询:是否可以在单个查询中投影四个结果?
问题描述
目前,我正在执行四个查询来预测基于过去 24 小时、上周、上个月和上个月的结果。是否可以在单个查询中投影四个结果?
解决方案
如果您需要与当前日期相比的最后一周、上个月、上一年,那么您可以使用 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 = ...
干杯!!
推荐阅读
- python-3.x - 从 sqlite3 数据库中提取单个值
- javascript - jQuery中AJAX成功返回的for循环索引增量
- php - 如何显示在 mySQL 中存储为路径的个人资料图像(头像)?
- jupyter-notebook - Jupyter Notebook 的 GUI 工具
- javascript - Material-UI 开关状态未从 DB 值更新
- python - python将拆分并加入到1行代码中
- c# - 连接到我的数据库时出错(MySQL、C#)
- python - Pandas 逻辑运算符 & 不起作用,但 'and' 起作用
- javascript - 如何在模态中实现自动完成?(红宝石/JavaScript)
- c++ - 在 C++ 中,如何制作一个可以包含相同变体向量的变体?