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

时间:2014-08-14 21:44:29

标签: sql oracle plsql

我有一个特定的查询:

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;

对于不同的场景,这基本上是相同的查询运行。我试图找到一种方法,将其转换为可以在一段时间内运行并按日期分组的格式。我不是PL SQL大师,所以我遇到了一些麻烦。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在搜索的基本内容应该是类似的内容,

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