Week and month calculation from date column

I have 3 column data like
with tab as
  select 'Topshop' brand, '10-JUL-11' deliverydate, '100' qty from dual union all
  select 'Topshop' brand, '10-JUL-11' deliverydate, '400' qty from dual union all
  select 'NewSita' brand, '11-JUL-11' deliverydate, '200' qty from dual union all
  select 'LaGress' brand, '12-JUL-11' deliverydate, '300' qty from dual union all
  select 'LaGress' brand, '10-AUG-11' deliverydate, '100' qty from dual union all
  select 'LaGress' brand, '11-AUG-11' deliverydate, '200' qty from dual union all
  select 'Topshop' brand, '12-AUG-11' deliverydate, '300' qty from dual union all
  select 'NewSita' brand, '10-SEP-11' deliverydate, '100' qty from dual union all
  select 'Topshop' brand, '11-SEP-11' deliverydate, '200' qty from dual union all
  select 'NewSita' brand, '12-SEP-11' deliverydate, '300' qty from dual
) select * from tabI need to convert it into 4 columns
Brand | Month | Week(start date) | Qty (sum)
Please let me know what are the options that i have, especially the date-time calculation functions available to solve such problems.
Thanks
w\

with tab as
  select 'Topshop' brand, to_date('10-JUL-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
  select 'Topshop' brand, to_date('10-JUL-11', 'dd-mon-yy') deliverydate, '400' qty from dual union all
  select 'NewSita' brand, to_date('11-JUL-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
  select 'LaGress' brand, to_date('12-JUL-11', 'dd-mon-yy') deliverydate, '300' qty from dual union all
  select 'LaGress' brand, to_date('10-AUG-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
  select 'LaGress' brand, to_date('11-AUG-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
  select 'Topshop' brand, to_date('12-AUG-11', 'dd-mon-yy') deliverydate, '300' qty from dual union all
  select 'NewSita' brand, to_date('10-SEP-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
  select 'Topshop' brand, to_date('11-SEP-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
  select 'NewSita' brand, to_date('12-SEP-11', 'dd-mon-yy') deliverydate, '300' qty from dual
), week_data as (
   select brand
   ,      to_char(deliverydate, 'Month', 'nls_date_language=american') MON
   ,      trunc(deliverydate, 'day') WEEK_START
   ,      sum(TO_NUMBER(qty)) SUM_QTY
   from   tab
   group by brand
   ,      to_char(deliverydate, 'Month', 'nls_date_language=american')
   ,      trunc(deliverydate, 'day')
), weeks as (
   select
   w.week_min + (level - 1) * 7 week_start
   from (
      select min(week_start) week_min, max(week_start) week_max
      from week_data
   ) w, dual
   connect by level <= 1 + (w.week_max - w.week_min) / 7
select
week_data.brand,
weeks.week_start,
nvl(week_data.sum_qty,0) sum_qty
from week_data
partition by (brand)
right outer join weeks
on (weeks.week_start = week_data.week_start)
order by
weeks.week_start,
week_data.brand;You construct a set of records of all "week_start" dates from your minimum to your maximum (classic connect by level trick.)
You use a partitioned outer join to fill in the gaps in your sparse data (see doc example [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_10002.htm#i2177515]here.
Hope that helps you :-)
(PS. I dislike implicit conversions - that is the only reason I have added a TO_NUMBER to your code within the sum() ;-) )
(PPS. Actually you probably should have asked this in a new question - I believe it is not really good forum etiquette to continue in a thread that has already been answered...)

Similar Messages

  • X axis to represent Yearin weeks and months ?

    I want to create an X axis to represent a year in weeks and months.  Anyone know how please ?
    The current limits of 10 max for major and minor will not allow a sensible scale or grid.

    You made two mistakes.
    (1) I don't know how many times it will be necessary to repeat that _date-time items aren't numerical ones._
    Given that, the chart use them as labels, not as numerical values.
    (2) You didn't read carefully the User Guides which states that each point requires a pair of values.
    You tried to chart three columns when four are required.
    Here you may see the way date_time values are treated.
    In Tableau 2 I added a column calculating a numerical value describing the date_time ones.
    To do that I use:
    =1+DATEDIF($A$2,A,"D")
    in B2
    then I apply fill_down.
    And I use = B in column D
    I added 1 to DATEDIF so that the numerical value matches the index of the day in the month.
    I'm no sure that it would be a good idea if dates span on several months.
    If you wish to display the dates on the X axis, you will be forced to use a text box like the one which is displayed.
    To adjust the spacing I played with the attribute: "between".
    Yvan KOENIG (VALLAURIS, France) jeudi 27 août 2009 15:36:23

  • Year and Month calculation

    Hi
    I have created a report with two prompts, they are "Enter Start date:" and "Enter End date:" where i take date values from a calendar while prompting.
    For example:
    Start date = 10/11/2003
    End date = 10/10/2003
    If i want to display the End date means, i ll use UserResponse("Enter End Date:") in the webi reports. What should i do to calculate Year and Month from this user response value,
    i used these calculations below, but it is not working, showing me #ERROR message.
    1) ToDate(UserResponse("Enter End Date:");"dd/MM/yyyy")     =  #ERROR
    2) FormatDate(ToDate(UserResponse("Enter End Date:");"dd/MM/yyyy");"dd/MM/yyyy")     = #ERROR
    3) Year(UserResponse("Enter End Date:")   = The expression or sub-expression at position 6 in the 'Year' function uses an    invalid data type. (WIS 10037)
    4) FormatDate(ToDate(UserResponse("Enter End Date:");"dd/MM/yyyy  hh:mm:ss A");"MM/dd/yyyy") =#ERROR
    so help me how to get the year and month values from prompts ?
    Thanks
    Dineshkumar

    to get the Month Value and year value in Webi
    month name
    =Month(ToDate(UserResponse("Enter End Date:");""))
    month number of the year
    =MonthNumberOfYear(ToDate(UserResponse("Enter End Date:");""))
    to get the Year Value
    =Year(ToDate(UserResponse("Enter End Date:");""))
    good luck

  • Extract Month & Year  From Date

    Hi All,
    I have a key figure quantity and i want the data on date,for the month and for the year.
    i create the variables for the date,for the month(ie from date to date) and for the year(ie, from date to date) seperatly that means i have three input variables which may sometimes create confusion.
    My problem is that I want to enter only date and the variable itself calculate the month and year from the date so that it returns the data as desired above.
    for eg. : Now i to enter date : 3/14/2009
              enter MTD (FROM/TO) : 3/01/2009 - 3/14/2009
              enter YTD (FROM/TO) : 4/01/2008 - 3/14/2009
    I want to enter only date : 3/14/2009
    and the variable itself extract the month and year till date.
    and also the same variable calculates number of days so that need not to take the formula variable of date difference to calculate the average qty.
    Neha..

    Hi,
    1. Create User Entry Variable on 0CALDAY : Name = ZCDAY.
    2. Craete a Customer Exit Variable on 0CALDAY: Name  = ZMTD.
    3. Craete a Customer Exit Variable on 0CALDAY: Name  = YMTD.
    Properties of Customer Exit Variables.
    Variable reporesents = Single
    Variable Entry = Mandatory
    Processing By = Customer Exit.
    Character = Calender Day
    Here I'm thinking that ZKF is your key figure.
    In columns you create two selections one is for MTD and other is for YTD.
    In MTD selection, drag and drop ZKF and Drag and Drop 0CALDAY and then Right Clcik and Restrict-->
    Selection = Value Range. (In Between) and restrict with the follwoing Variables
    []ZMTD; ZCDAY.
    In YTD selection, drag and drop ZKF and Drag and Drop 0CALDAY and then Right Clcik and Restrict-->
    Selection = Value Range. (In Between) and restrict with the follwoing Variables
    []YTD; ZCDAY.
    Then write the following code in I_STEP = 2.
    DATA: ZT_DT1 TYPE SY-DATUM,
              ZT_DT2 TYPE SY-DATUM,
              ZT_SDT TYPE SY-DATUM,
              ZT_YR(4) TYPE N,
              ZT_DY(2) TYPE N,
              ZT_MT(2) TYPE N,
              ZE_TT(2) TYPE N,
              ZPOPER TYPE POPER,
             ZRELJR TYPE RELJR.
    WHEN 'ZMTD_A'.
      LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.
        IF sy-subrc = 0.
          CLEAR: l_s_range.
          ZT_DY = '01'.
          ZT_SDT = loc_var_range-low.
          ZT_MT = ZT_SDT+4(2).
          ZT_YR = ZT_SDT+0(4).
          CONCATENATE ZT_YR ZT_MT ZT_DY INTO ZT_DT1.
          l_s_range-low = ZT_DT1.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
        ENDIF.
      ENDLOOP.
    WHEN 'ZYTD'.
      LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.
        IF sy-subrc = 0.
          CLEAR: l_s_range.
          ZT_DY = '01'.
          ZT_SDT = loc_var_range-low.
          CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
            EXPORTING
              I_DATE               = ZT_SDT
    *         I_MONMIT             = 00
              I_PERIV              = 'V3'
           IMPORTING
             E_BUPER              = zbuper
             E_GJAHR              = zbdatj
           EXCEPTIONS
             INPUT_FALSE          = 1
             T009_NOTFOUND        = 2
             T009B_NOTFOUND       = 3
             OTHERS               = 4
          IF SY-SUBRC <> 0.
             MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
          CALL FUNCTION 'FIRST_AND_LAST_DAY_IN_YEAR_GET'
            EXPORTING
              I_GJAHR              = zbdatj
              I_PERIV              = 'V3'
           IMPORTING
             E_FIRST_DAY          = ZT_DT2
    *         E_LAST_DAY           =
           EXCEPTIONS
             INPUT_FALSE          = 1
             T009_NOTFOUND        = 2
             T009B_NOTFOUND       = 3
             OTHERS               = 4
          IF SY-SUBRC <> 0.
             MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
          l_s_range-low = ZT_DT2.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
        ENDIF.
      ENDLOOP.
    Thanks
    Reddy

  • How to derive month/year from date in SAP BW 3.5 data flow

    Hi
    How we can derive cal year/month and fiscal month/year from date in SAP BW 3.5 data flow (we're using transfer and update rule)..
    Thanks,
    PK

    Hi,
    if you have any date filed in source side you can just map to any time char system will automatically convert to target objects.
    please look at the screen shot for understanding. (not 3.x it is 7.x)
    Thanks,
    Phani.

  • In "history" only today appears and not thelast days weeks and months

    History :only appear internet sites of theday. Never those of the previous days weeks and months
    My Firefox : 3.6.9

    * Make sure that you do not use [[Clear Recent History]] to clear the Browsing History.
    * Make sure that the History is set to at least 1 day: Tools > Options > Privacy > History: "Remember visited pages for at least"
    * Make sure that you do not start Firefox in Private Browsing mode (Tools > Stop Private Browsing is grayed, see [[Private Browsing]])
    * To see History and Cookie settings in Tools > Options > Privacy, choose the setting "Firefox will: Use custom settings for history"
    You can look at these prefs on the about:config page.
    *http://kb.mozillazine.org/browser.history_expire_days (180) (also affects saved form data)
    *http://kb.mozillazine.org/browser.history_expire_days_min (90)
    To open the <i>about:config</i> page, type <b>about:config</b> in the location (address) bar and press the "<i>Enter</i>" key, just like you type the url of a website to open a website.<br />
    If you see a warning then you can confirm that you want to access that page.<br />

  • SCSM2012 R2 Custom Reports with Weekly and monthly schedule to email

    Hi
    I am able to pull standard ListofIncidents Reports, want a similar kind of report with a Weekly and monthly scheduled reports to email, I have tried using pull reports from Cubes to Excel and do a custom reports but is not showing like standard ListofIncidents
    Reports. Could you please guide me on this.
    Thanks and Regards,
    Pradeep Kumar
    Pradeep Kuimar

    Hi,
    Would you please check the articles below regarding to publishing and Subscribing to Service Manager Reports:
    http://blogs.technet.com/b/servicemanager/archive/2010/06/30/publishing-and-subscribing-to-service-manager-reports.aspx
    How to Schedule a Standard Service Manager Report
    https://technet.microsoft.com/en-us/library/hh519682.aspx
    Hope this can be helpful for you!
    Regards,
    Yan Li
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected]

  • How to Calculate Duration from Date Columns

    Hello,
    I would like to calculate duration from date columns. Using the following query
    SELECT DISTINCT
    EVENT_ID,
    To_Char(RU.START_TIME, 'HH24:MI') START_TIME,
    To_Char(RU.END_TIME, 'HH24:MI') END_TIME
    FROM ROOM_USAGE RU
    What I need is to subtract START_TIME from END_TIME and result should be in minutes.
    Many Thanks,

    But, i think you need to calculate the number of days if the days are not same -
    satyaki>
    satyaki>
    satyaki>
    satyaki>with ru
    2 as
    3 (
    4 select '05-JAN-2007 15:10:20' start_time, '07-JAN-2007 19:20:20' end_time from dual
    5 union all
    6 select '08-JAN-2007 05:10:20' start_time, '07-JAN-2007 07:07:20' end_time from dual
    7 )
    8 select ru.start_time, ru.end_time, SUBSTR(numtodsinterval(to_date(RU.END_TIME,'DD-MON-YYYY HH24:MI:SS')-to_date(RU.START_TIME,'DD-MON-YYYY HH24:MI:SS'), 'DAY'), 12, 8) duration
    9 from ru;
    START_TIME END_TIME DURATION
    05-JAN-2007 15:10:20 07-JAN-2007 19:20:20 04:09:59
    08-JAN-2007 05:10:20 07-JAN-2007 07:07:20 22:03:00
    satyaki>
    Or, Am i missing anything?
    Regards.
    Satyaki De.
    is it possible to to add the duration? making the total hours to 26:12 (twentysix hours and twelve minutes)

  • Displaying Tasks in Weekly and Monthly Views

    Hi,
    According to the on-line help for oracle calendar 9.0.4, tasks cannot be viewed in the weekly and monthly views - when using the web client. I was wondering whether anyone has found a way to work around this - apart from usinng the 'view task' icon.
    Thanks
    Naqi

    Hi All,
    I am closing this thread as I have raised this in wrong module.
    Apologies for the incovienence
    Regards,
    Laxminarsimha

  • Daily,weekly and monthly report format

    hi
       i  was asked to create a format for daily, weekly and monthly system performance report .. can anyone send me  sample report format..

    Hi Sandy,
    System Performance is always checked by a list of our lovely transactions in BASIS I generally had created one for my self and keep a track of performance..
    IT goes like this..
    Daily : DB12(Check Backup Logs);SM51(Check serv are UP);RZ20(Look for ALerts); SM13;SM21;SM37(check for cancelled jobs);SM12; st22 (dumps); ST02; SM35; SP01 (Spool jobs for more time); AL02; ST04; AL16.
    Weekly : DB12; DB02; SAPDBA checks ; DB drives and space;  EWA report of the past week.
    Monthly : More or less the same stuff of Weekly  with Archive files as well ...and SAPDBA cleanup logs... Monitor the DB growth and check for the backup logs...
    Hopefully this info shd help yoiu in this regard.
    Please award suitable points for the post.
    Br,
    Sri

  • Can I get a list of users that visits a certain page on a Sharepoint site on a daily, weekly and monthly basis.

    Hi, We need a list of users that visits a certain page on a SharePoint site on a daily, weekly and monthly basis.
    Now the question is, is that possible out of the box or through the custom web development.
    Thanks
    RRS

    Hi,
       Hope this will help you.
    http://sharepoint.stackexchange.com/questions/83640/how-to-check-user-has-visited-site-collection-first-time
    http://blogs.msdn.com/b/ecm/archive/2010/05/03/web-analytics-in-sharepoint-2010-insights-into-reports-and-metrics.aspx
    Regards,
    MS

  • The daily, weekly, and monthly maintanence scripts

    Hello All,
    I have a power mac G4 Digital Audio at 733mhz, my question is why am I not seeing the automatic maintanence scripts coming on at 3am in the morning? I am usually up on my computer at those times, but still have yet to see the system go into automatic maintanence scripts at that time of the morning? Is there something wrong with my configuration file for the scripts, or is there something I need to do in order for it to work?
    Nadav

    Hiya,
    Just a quick check: have you looked in /var/log/daily.out to see if the daily jobs are outputting anything to the logs, or maybe that's what you're talking about? Nothing logged?
    Um, if there's nothing in the log, then you may have a /etc/daily.local file overriding the default script in /etc/periodic/daily in someway. A quick check of /etc/periodic/daily/500.daily may prove helpful. It should be doing all it's normal log rotations and jazz... If the file looks normal (not corrupted or just blank say) maybe try putting a little command to write something to /var/log/daily.out to tell yourself it's working like (you'll need to sudo this):
    echo "this is daily.out working like a champ"
    or whatever. Same thing for the weekly and monthly scripts...
    If you've done all this already and haven't looked yet, check out the manual pages for periodic, periodic.conf, cron, crontab, and so forth...maybe there's some goofy setting hosing things somewhere.
    Hope that helps!

  • Daily, weekly and monthly acitivities in Oracle EBS 11i

    Hi All,
    I need to know the daily, weekly and monthly maintainance activities for Oracle EBS 11i .
    tq

    user_exit wrote:
    Hi All,
    I need to know the daily, weekly and monthly maintainance activities for Oracle EBS 11i .
    tqthis is not forum for EBS, so please post in
    General EBS Discussion
    Thanks

  • Need dissociation street name and street number from one column?

    Hi all,
    I have one table A with one column with Street name and street number.
    And I was adding second column and now need update this second column with street number
    and delete street number from first column.
    How can unpluging street name and street number from one column?
    (street name are 90% char, number is 100 numeric, in 10% street name name
    starts with number (1.HEROES 223, 1.HEROES 224...)
    Gordan

    Hi,
    Solution of Mr. Wijk come first, however I like to post (it's very similar and incomplete, I know...). Supose you have a table call address (use position -1 like Mr. Wijk to look backward). It's better in cases like '1.My Street 123' (the space before the number is very important)
    SELECT substr(a.street_name,
                  1,
                  instr(a.street_name,
                        -1) - 1) street_name,
           substr(a.street_name,
                  instr(a.street_name,
                        -1) + 1) street_number
      FROM address a;Regards,

  • Creating Time Dimension from date columns in fact tables.

    I remember watching a demo from a BI Tool a couple years ago, wich I swear was OBIEE, and the presentator stated it was possible to create a Time Dimension in the admin tool, based on a date column in other table.
    Can you guys tell me if there's such functionality in OBIEE?
    If so, how could I achieve that?!
    Thanks in advance!
    Marcos

    hi,
    You are trying to make Fact table as Dim table ???
    Fact table has some dim columns??
    We can do this by making a fact table as dim table ,create a dim hierarchy on that table
    Year level :Extract(year from fact_date_column)
    Month and year  level:CAST (Extract(month from fact_date_column) As CHAR(5) ) || CAST (Extract(yearfrom fact_date_column) As CHAR(5) )
    Like this
    But,be careful while doing this make sure that all joins and content levels are good
    As per my knowledge this is not a good way,Experts can add some words lets see!!!!!! :-)
    thanks,
    saichand.v

Maybe you are looking for

  • Trace Problem

    Hi Experts, In Qurey Designer when i want to access one of MultiProvider ...it showing the problem...like... Terminate : System error in Program CL_RSD_MULTIPROV and from FACTOR -02 Terminate : Program error in class SAPMSY1 method : UNCAUGHT_EXCEPTI

  • How to automatically shut down iMac after downloading a file?

    I'm downloading a large size file at the moment & I want this computer to shut down by itself by the time the download process has finished. what should I do? pls suggest thx.

  • About Fault messages?

    Hi experts, i have a senario where i have to create fault message,What is the use off fault message........To create the fault message sholud i create complete source structrue in data type of fault messsage.....can any one please send doc's and info

  • I downloaded Yosemite and now I have lots of pop ups and my safari isn't working correctly. How do I remove Yosemite?

    I downloaded Yosemite and now I have lots of pop ups and my safari isn't working correctly. How do I remove Yosemite?

  • CD Burning error 4701

    This is driving me up the wall. I have tried to burn a playlist using ITunes to CD. I insert a blank CD and the burning process begins. After a period of 1 or 4 tracks (depending on how many attempts to burn) the burning process self cancels, then a