简体   繁体   中英

Oracle sql query to create aggregate view

Have a company calendar stored in the database as shown below. Note that company calendar starts from Sat through Fri:-

   Date1      Week     Month   Quarter     Year    Shift
23-FEB-13   9       6        2         2013     ABC
23-FEB-13   9       6        2         2013     DEF
22-FEB-13   9       6        2         2013     ABC1
22-FEB-13   9       6        2         2013     DEF2
21-FEB-13   8       6        2         2013     DEF2

What am trying to do is to create a view to store the Week#, StartWeek, EndWeek ie store the current week's start and end date like

Week   StartWeek    EndWeek

  9        22-FEB-13     23-FEB-13

This is what i have so far

SELECT START_DATETIME, F_WEEK, ROW_NUMBER( ) OVER (PARTITION BY F_WEEK, F_YEAR ORDER BY SHIFT DESC) RNUM FROM COMMON.FISCAL_CALENDAR  WHERE F_WEEK IN (SELECT F_WEEK FROM COMMON.FISCAL_CALENDAR WHERE TO_DATE(START_DATETIME, 'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) AND F_YEAR IN (SELECT F_YEAR FROM COMMON.FISCAL_CALENDAR WHERE TO_DATE(START_DATETIME, 'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) ORDER BY START_DATETIME DESC

Would highly appreciate if anyone can throw more light on this?

Annual table of weeks with week start/end dates and week number - you can use your dates for start and end dates and add your calculations:

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#  
 FROM
 (
  SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS start_date
    FROM dual
  CONNECT BY LEVEL <= 
  (
  SELECT TRUNC(ADD_MONTHS (SYSDATE, 12), 'Y')-TRUNC(SYSDATE, 'Y') "Num of Days in 2013"   
   FROM dual
  )
 )
/

START_DATE    WK_STARTS    WK_ENDS               ISO_WK#
----------------------------------------------------
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/7/2013    1/7/2013    1/13/2013 11:59:59 PM    2
1/8/2013    1/7/2013    1/13/2013 11:59:59 PM    2

These are quick fix suggestions. You may add more calculations as you know your tables and dates... For company calendar you need to add some calculations and changes in above query. You may add your end date. I built annual table but you can replace dates in CONNECT BY to your dates eg number of days between your dates

...CONNECT BY LEVEL <= (your_end_date - your_start_date) 

Other fixes:

  1. Replace my start_date in FROM clause to your start date - it will be the first SAT of the year after this change. You may pick any other SAT for start date, does not have to be first SAT of the year:

      SELECT Next_Day(Trunc(Sysdate,'Y'),'SAT') + LEVEL-1 AS start_date FROM dual... 
  2. Add CASE to the outer query - after ISO_Wk#. Remove unwanted columns...:

     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# , (CASE WHEN TO_CHAR(start_date, 'DY') = 'SAT' THEN start_date END) your_wk_start , (CASE WHEN TO_CHAR(start_date, 'DY') = 'FRI' THEN TRUNC(start_date, 'iw') + 7 - 1/86400 END) your_wk_end .... START_DATE WK_STARTS WK_ENDS ISO_WK# WK_DAY YOUR_WK_START YOUR_WK_END -------------------------------------------------------------------------------------- 1/5/2013 12/31/2012 1/6/2013 11:59:59 PM 1 SAT 1/5/2013 1/6/2013 12/31/2012 1/6/2013 11:59:59 PM 1 SUN .... .... 1/11/2013 1/7/2013 1/13/2013 11:59:59 PM 2 FRI 1/13/2013 11:59:59 PM 1/12/2013 1/7/2013 1/13/2013 11:59:59 PM 2 SAT 1/12/2013 .... .... 1/18/2013 1/14/2013 1/20/2013 11:59:59 PM 3 FRI 1/20/2013 11:59:59 PM 

I assume that your needs are about getting the min and max date grouped by week:

SELECT Week, MIN(Date) As StartWeek, MAX(Date) As EndWeek  
FROM original_table
GROUP BY Week;

If you are interested only in the current week:

SELECT Week, MIN(Date) As StartWeek, MAX(Date) As EndWeek  
FROM original_table
WHERE Week = TO_CHAR(sysdate, 'IW');

就这个

SELECT MAX(DATE1) AS SHIFT_END_TIME, MIN(DATE1) AS SHIFT_START_TIME, WEEK, YEAR FROM MYTABLE GROUP BY WEEK, YEAR HAVING WEEK IN (SELECT WEEK FROM MYTABLE  WHERE TO_DATE(DATE1,   'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) AND YEAR IN (SELECT YEAR FROM MYTABLE WHERE TO_DATE(DATE1, 'DD- MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY'))

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