oracle数据统计

qq_28398475 2017-10-16 06:11:52

如图所示,根据工作人员分组,create_date字段统计今天、本周、本月的数据量。要用一条sql怎么实现
...全文
327 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
碧水幽幽泉 2017-10-16
  • 打赏
  • 举报
回复
用行转列调整下显示:

select person
from(
select person,count(*) cnt,'今天' flag
from table_name
where create_date = trunc(sysdate)
group by person
union all
select person,count(*) cnt,'本周' flag
from table_name
where create_date in (trunc(sysdate)-to_char(sysdate,'d')+2+level-1 from dual connect by level <= 7)
group by person
union all
select person,count(*) cnt,'本月' flag
from table_name
where create_date in (select trunc(sysdate,'mm')+level-1 from dual connect by level <= last_day(trunc(sysdate,'mm'))-trunc(sysdate,'mm')+1)
group by person
union all
select person,count(*) cnt,'本年' flag
from table_name
where trunc(create_date,'year') = trunc(sysdate,'year')
group by person
select person,count(*) cnt,'系统至今' flag
from table_name
group by person
)
pivot(sum(cnt) for flag in('今天','本周','本月','本年','系统至今')

碧水幽幽泉 2017-10-16
  • 打赏
  • 举报
回复
由于你没有给出字段类型和数据格式,我这里假设create_date为date类型,数据格式为:yyyy-mm-dd
假设工作人员字段名称为person

select person,count(*),'今天' type
from table_name
where create_date = trunc(sysdate)
group by person
union all
select person,count(*),'本周' type
from table_name
where create_date in (trunc(sysdate)-to_char(sysdate,'d')+2+level-1 from dual connect by level <= 7)
group by person
union all
select person,count(*),'本月' type
from table_name
where create_date in (select trunc(sysdate,'mm')+level-1 from dual connect by level <= last_day(trunc(sysdate,'mm'))-trunc(sysdate,'mm')+1)
group by person;
碧水幽幽泉 2017-10-16
  • 打赏
  • 举报
回复
本周是指周一到周日吗?

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧