1

请不要批评我这个问题,因为它已经被简化了。

假设我得到了CALENDAR包含两列的表:CLIENT_ID (INTEGER)并且ENTRY_DATE(DATE)这些数据:

+----------------------------+
| CLIENT_ID |   ENTRY_DATE   |
+-----------+----------------+
|   100     |    2012-01-03  |
|   100     |    2012-01-23  |
+-----------+----------------+

如何使用内联INSERT缺失天数?

4

4 回答 4

1

生成日期并离开加入日历。

select 
   nvl(calendar.client_id,100), 
   dt.day as entry_date
from (select to_date('1-Jan-2012','dd-mon-yyyy')+level-1
      from dual connect by level<=366
      ) dt
left join Calendar
  on (calendar.entry_date=dt.day and client_id = 100)
于 2013-01-30T12:58:34.037 回答
1

此查询将为所有客户端插入日历孔:

INSERT INTO calendar (client_id, entry_date) (
    SELECT holes.client_id, holes.hole
    FROM (
            SELECT cal.client_id, cal.mn + level AS hole
            FROM (
                    SELECT client_id, MIN(entry_date) AS mn, MAX(entry_date) AS mx
                    FROM calendar
                    GROUP BY client_id
                    HAVING MAX(entry_date)>MIN(entry_date)+1
                 ) cal
            CONNECT BY cal.mn + level < cal.mx
            GROUP BY cal.client_id, cal.mn + level
        ) holes LEFT OUTER JOIN calendar c ON holes.client_id = c.client_id AND c.entry_date = hole
    WHERE
        c.entry_date IS NULL
)
于 2013-01-30T14:16:28.927 回答
0

也许这有帮助 - Oracle 查询。您将需要弄清楚如何比较您的日期并仅插入缺失的日期。开始日期是您的入境日期。其余的是另外...:

-- Annual table by date and ISO week - use WW format for non-iso week--
SELECT start_date                               
    , TRUNC(start_date, 'iw')                  wk_starts    
    , TRUNC(start_date, 'iw') + 7 - 1/86400    wk_ends
    , TO_NUMBER (TO_CHAR (start_date, 'IW'))   ISO_wk#_iw  
  FROM
  (
   SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS start_date  -- 11/1/2013 --
    FROM dual
   CONNECT BY LEVEL <= 
   (-- Number of Days in curr year = 365 --
   SELECT TRUNC(ADD_MONTHS (SYSDATE, 12), 'Y')-TRUNC(SYSDATE, 'Y')    
    FROM dual
   )
  )
 /

 SQL>

 START_DATE   WK_STARTS   WK_ENDS                  ISO_WK#_IW
 --------------------------------------------------------------
 1/1/2013     12/31/2012    1/6/2013 11:59:59 PM    1
 1/2/2013     12/31/2012    1/6/2013 11:59:59 PM    1
 ....
 1/14/2013    1/14/2013 1/20/2013 11:59:59 PM       3
 1/15/2013    1/14/2013 1/20/2013 11:59:59 PM       3
 ....
 12/29/2013   12/23/2013    12/29/2013 11:59:59 PM  52
 12/30/2013   12/30/2013    1/5/2014 11:59:59 PM    1
 12/31/2013   12/30/2013    1/5/2014 11:59:59 PM    1
于 2013-01-30T13:49:16.040 回答
0

基于 FlorinGhita 的回答:

SELECT 100, TO_DATE('2012-01-03', 'YYYY-MM-DD') + LEVEL 
  FROM DUAL
CONNECT BY LEVEL < TO_DATE('2012-01-23', 'YYYY-MM-DD') - TO_DATE('2012-01-03', 'YYYY-MM-DD')
于 2013-01-30T13:49:35.447 回答