简体   繁体   English

在日期范围内并按天分组运行sql查询

[英]Run a sql query over a date range and group by day

I have a specific query: 我有一个具体的查询:

DECLARE
  --TYPE date_array IS TABLE of DATE Index BY PLS_INTEGER;
   endDate Date := SYSDATE;
   startDate Date := endDate - 5;
BEGIN

Select
   count(*) as Manual
from 
  document D, 
  Export_Document ED ,
  Archive_Location AL 
where 
  scan_type_nm = 'wonky' 
  and 
    D.document_id = ed.document_id
  and
    ed.export_document_id = Al.export_document_id
  and
    ed.record_create_user_id != 'Sporky'
  and
    Al.Archive_Location_NM IS NOT NULL
  and
    D.record_create_gmts >= startDate
  and
    D.record_create_gmts < endDate
group by D.record_create_gmts;
--Not Auto-Archived
 Select   
   D.record_create_gmts,
  count(*) As "Auto indexed"
from 
  document D, 
  Export_Document ED ,
  Archive_Location AL 
where 
  scan_type_nm = 'huh' 
  and 
    D.document_id = ed.document_id
  and
    ed.export_document_id = Al.export_document_id
  and
    ed.record_create_user_id = 'what'
  and
    Al.Archive_Location_NM IS NOT NULL
  and
    D.record_create_gmts >= startDate
  and
    D.record_create_gmts < endDate
  group by  D.record_create_gmts;  
--Total indexed
 Select   
   D.record_create_gmts,
  count(*) As "Total Indexed"
from 
  document D, 
  Export_Document ED ,
  Archive_Location AL 
where 
  scan_type_nm = 'huh' 
  and 
    D.document_id = ed.document_id
  and
   ed.export_document_id = Al.export_document_id
  and
    Al.Archive_Location_NM IS NOT NULL
  and
    D.record_create_gmts >= startDate
  and
    D.record_create_gmts < endDate
    Group by D.record_create_gmts;  

    --Total
Select   
   D.record_create_gmts,
  count(*) as Universe
from 
  document D 
  --Export_Document ED ,
  --Archive_Location AL 
where 
  scan_type_nm = 'huh'
 /* and 
    D.document_id = ed.document_id
  and
    ed.export_document_id = Al.export_document_id
  and
    ed.record_create_user_id != 'whawt'
  and
    Al.Archive_Location_NM IS NOT NULL
    */
  and
    D.record_create_gmts >= startDate
  and
    D.record_create_gmts < endDate
    group by  D.record_create_gmts;      
END;

Which is basically the same query run for different scenarios. 对于不同的情况,基本上运行相同的查询。 I am trying to figure out a way to get this into a format that can be run over a time period and group by the date. 我正在尝试找出一种将其转换为可以在一段时间内运行并按日期分组的格式的方法。 I am not a PL SQL guru, so I am running into some trouble. 我不是PL SQL专家,所以遇到了一些麻烦。 Any ideas? 有任何想法吗?

The basic thing it looks like you're searching for should be something like, 您正在寻找的基本内容应该是

SELECT COUNT(*)
FROM table t
GROUP BY DATE(created_at)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM