Oracle DB-单查询非线性时间分组报告(1天,1周,1个月,3个月总计)

理查德

我正在尝试构建一个报告,该报告本质上将是一个合并了这些查询输出的单个查询:

select count(*) from my_table where createddate >= CURRENT_DATE - 1;
select count(*) from my_table where createddate >= CURRENT_DATE - 7;
select count(*) from my_table where createddate >= CURRENT_DATE - 30;
select count(*) from my_table where createddate >= CURRENT_DATE - 90;
select count(*) from my_table;

这样输出将类似于:

Time_Period   Count
===================
Yesterday        5
Last Week       20
Last Month      50
Last 90 Days   100
Total         5000

我已经成功构建了几个线性时间序列查询(按天,按周,按月等)。但是,能够构建非线性报告查询并不算运气。

戈登·利诺夫

您可以轻松地将结果放在单独的列中:

select sum(case when createddate >= CURRENT_DATE - 1 then 1 else 0 end) as yesterday,
       sum(case when createddate >= CURRENT_DATE - 7 then 1 else 0 end) as last_week,
       sum(case when createddate >= CURRENT_DATE - 30 then 1 else 0 end) as last_month,
       sum(case when createddate >= CURRENT_DATE - 90 then 1 else 0 end) as last_90days,
       count(*) as total
from my_table;

如果您想要单独的行,则可以取消上述内容,或者只使用union all

select 'Yesterday' as which, count(*) from my_table where createddate >= CURRENT_DATE - 1
union all
select 'Last week', count(*) from my_table where createddate >= CURRENT_DATE - 7
union all
select 'Last month', count(*) from my_table where createddate >= CURRENT_DATE - 30
union all
select 'Last 90 days', count(*) from my_table where createddate >= CURRENT_DATE - 90
union all
select 'Total', count(*) from my_table;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

oracle db的日期和时间

来自分类Dev

查询以获取Oracle DB版本

来自分类Dev

查询ORACLE DB时出错

来自分类Dev

Oracle Db按两列分组

来自分类Dev

Oracle DB LIKE查询NULL值

来自分类Dev

Oracle DB-锁定取消更新查询

来自分类Dev

通过Jenkins运行我的Oracle DB查询

来自分类Dev

如何简化 Oracle DB 的此查询?

来自分类Dev

Oracle SQL-最近12个月的数据

来自分类Dev

Oracle:根据过去6个月的时间间隔为每行计算count()

来自分类Dev

到日期时间+/- 1个月

来自分类Dev

Oracle SQL:获取过去3个月的平均值并添加其他列

来自分类Dev

我可以在Oracle中将范围间隔分区从一个月更改为一天吗

来自分类Dev

Oracle DB只需并排合并两个表

来自分类Dev

Oracle DB只需并排合并两个表

来自分类Dev

Oracle DB中的VARCHAR

来自分类Dev

Oracle DB的实体框架

来自分类Dev

Oracle DB 的 JDBC 超时

来自分类Dev

IceCube / Ruby每1个月10天的规则

来自分类Dev

从Oracle DB时间戳到Java时间戳:混乱

来自分类Dev

Oracle DB sql将table_names模式分组

来自分类Dev

如何从Oracle DB查询波斯搜索匹配项?

来自分类Dev

将Excel Oracle DB查询转换为Python Pandas

来自分类Dev

如何增加查询的默认访存大小(Oracle DB)

来自分类Dev

在mysql,db2和oracle中分别查询

来自分类Dev

在Microsoft Query中执行有效的Oracle DB查询

来自分类Dev

使用Shell脚本从oracle DB进行sql查询

来自分类Dev

使用oracle db中的查询获取表名及其行数

来自分类Dev

用于 ORACLE DB 查询中的分号的 Regexp_Substr