简体   繁体   中英

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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