选择两个日期之间的所有日期,并显示库存ID的唯一计数

时间:2015-08-03 07:53:49

标签: sql oracle date

我正在使用oracle db。我有2个表专门的Inventory和grounding_info。每个库存可以有多个接地信息或没有。表结构如下。

Inventory
---------              
Inventory_id


Grounding_info
--------------
Info_id
Inventory_id
Grounding_date

我希望在给定日期范围之间的每个日期获得库存计数。如果没有基于日期的库存,则该日期应显示为计数0。

请帮忙.. 我尝试了以下查询:

select tble.dte,count(distinct(groundinginfo.INVENTORY_ID)) cmt from ATL_GROUNDING_INFO groundinginfo
outer join (
    SELECT TO_DATE('2015/01/01 12:00:00 A.M.' ,'YYYY/MM/DD hh:mi:ss A.M.') - 1 + rownum AS dte FROM all_objects groundinginfo
    WHERE TO_DATE('2015/01/01 12:00:00 A.M.' ,'YYYY/MM/DD hh:mi:ss A.M.') - 1 + rownum <= TO_DATE('2015/04/01 11:59:59 P.M.' ,'YYYY/MM/DD hh:mi:ss P.M.') 
) tble
on groundinginfo.DATE_TURNED_IN = tble.dte
group by tble.dte

但是它返回了错误:&#34; ORA-00933:SQL命令未正确结束&#34;

1 个答案:

答案 0 :(得分:0)

尝试此查询:

SELECT tble.dte, COUNT(DISTINCT INVENTORY_ID) As cmt FROM ATL_GROUNDING_INFO groundinginfo
right join (
    SELECT TRUNC(TO_DATE('2015/01/01' ,'YYYY/MM/DD') - 1 + rownum) dte FROM DUAL
    CONNECT BY ROWNUM < 366 AND TRUNC(TO_DATE('2015/01/01' ,'YYYY/MM/DD') - 1 + rownum) <= TRUNC(TO_DATE('2015/04/01' ,'YYYY/MM/DD'))
) tble
on tble.dte = TRUNC(groundinginfo.DATE_TURNED_IN)
group by tble.dte
order by tble.dte ASC