GROUP BY GROUPING SETS for a selected month and for year to date

Below is a code example to demonstrate this question:
declare @test table (ID int, Quantity int, Day date);
insert into @test values
(4, 500, '1/18/2014'),
(4, 550, '1/28/2014'),
(7, 600, '1/10/2014'),
(7, 750, '1/11/2014'),
(7, 800, '1/20/2014'),
(1, 100, '1/2/2014'),
(1, 125, '1/10/2014'),
(8, 300, '1/7/2014'),
(9, 200, '1/17/2014'),
(9, 100, '1/22/2014'),
(4, 900, '2/18/2014'),
(4, 550, '2/28/2014'),
(7, 600, '2/10/2014'),
(7, 700, '2/11/2014'),
(7, 800, '2/20/2014'),
(1, 100, '2/2/2014'),
(1, 150, '2/10/2014'),
(8, 300, '2/7/2014'),
(9, 200, '2/17/2014'),
(9, 100, '2/22/2014'),
(4, 500, '3/18/2014'),
(4, 550, '3/28/2014'),
(7, 600, '3/10/2014'),
(7, 750, '3/11/2014'),
(7, 800, '3/20/2014'),
(1, 100, '3/2/2014'),
(1, 325, '3/10/2014'),
(8, 300, '3/7/2014'),
(9, 200, '3/17/2014'),
(9, 100, '3/22/2014'),
(4, 500, '4/18/2014'),
(4, 550, '4/28/2014'),
(7, 100, '4/10/2014'),
(7, 750, '4/11/2014'),
(7, 800, '4/20/2014'),
(1, 100, '4/2/2014'),
(1, 325, '4/10/2014'),
(8, 300, '4/7/2014'),
(9, 200, '4/17/2014'),
(9, 100, '4/22/2014'),
(4, 500, '5/18/2014'),
(4, 550, '5/28/2014'),
(7, 600, '5/10/2014'),
(7, 750, '5/11/2014'),
(7, 50, '5/20/2014'),
(1, 100, '5/2/2014'),
(1, 325, '5/10/2014'),
(8, 300, '5/7/2014'),
(9, 200, '5/17/2014'),
(9, 100, '5/22/2014');
--detail
select *
from @test;
--aggregation
select
TotalQuantity = sum(Quantity),
[Month] = month(Day)
from @test
group by
grouping sets
(month(Day)),
(year(Day))
go
This is the aggregation query result:
However, the desired result is to return only two rows: one row for month 3 and the other row for year to date (in the picture above YTD is the row that appears with {null} in the Month column).  Is there a way to achieve this goal by modifying the
sample code above?  The requirement is to only read the data once (do not want a solution that involves a UNION which implies reading the data twice).

you can add required filters in having clause. Here is the query -
select
TotalQuantity = sum(Quantity),
[Month] = month(Day)
from @test
group by
grouping sets
(month(Day)),
(year(Day))
having
month(Day) = 3 or month(Day) is null;

Similar Messages

  • Need working days for a particular month and year

    Hi,
    I need the number of working days for a particular month and year.Saturdays and Sundays are holidays.
    Regards,
    Vignesh

    Try this:
    SQL> var yr NUMBER;
    SQL> exec :yr := 2010;
    PL/SQL procedure successfully completed.
    SQL> with t as (select :yr yr from dual)
      2  SELECT TO_CHAR(dat,'MON-RR'),COUNT(*) FROM
      3  (select TO_DATE('01-JAN-'||yr) + lv dat FROM
      4  (select level - 1 lv,yr from t
      5  connect by level <= TO_DATE('31-DEC-'||yr) - TO_DATE('01-JAN-'||yr) + 1))
      6  WHERE TO_CHAR(Dat,'DY') NOT IN ('SAT','SUN')
      7  GROUP BY TO_CHAR(dat,'MON-RR');
    TO_CHAR(DAT,   COUNT(*)
    APR-10               22
    AUG-10               22
    DEC-10               23
    FEB-10               20
    JAN-10               21
    JUL-10               22
    JUN-10               22
    MAR-10               23
    MAY-10               21
    NOV-10               22
    OCT-10               21
    TO_CHAR(DAT,   COUNT(*)
    SEP-10               22
    12 rows selected.
    SQL> Edited by: AP on Jul 27, 2010 7:54 AM

  • Creation of a Query to show the values for the current month and the last 12 months data.

    Dear All,
    Good day!
    I have to create a Query with the below requirement.
    I have to create a Query to show the values for the current month and the last 12 months data.
    Can you please guide me how to achieve this ??
    thank you,
    Regards,
    Hema

    Hema
    explain the exact problem..? as you mentioned you want to create query to show values for current month and last 12 months.. so I think you want to show values for 12 months from current data.. you can achive this by multiple way..
    you can have selection screen and field with date .. and restrict based on system current date and 12 months before or you can handle this at your target.. .. I mean there are multiple ways to restrict data by date range..
    for some more hints..
    http://www.forumtopics.com/busobj/viewtopic.php?t=34393&sid=7fba465d0463bf7ff5ec46c128754ed6
    http://businessintelligence.ittoolbox.com/groups/technical-functional/cognos8-l/how-to-display-last-12-months-in-report-based-on-todays-date-3231850
    http://scn.sap.com/thread/3217381
    search on SDN you will get many other ways..
    Thanks,
    Bhupesh

  • HT204053 We have apple computer and set up 1 apple id and for music on iPods.  Now we have 3 apple phones and want to share the music but keep our contacts separate. what should we do.

    We started with an apple computer and set up 1 apple id and for music on iPods.  Now we have 3 apple phones and want to share the music but keep our contacts separate. what should we do? Right now we have the same apple id on the PC, ipodtouch, ipod, and 3 phones.  They said make seperate clouds?

    Hello there MizzouTrojan,
    If I understand correctly you want all 6 devices to have access to a single calendar that each device gets notifications for and can edit. You can do that with an private iCloud shared calendar. First sign into www.icloud.com and use these steps from the following article to share a calendar:
    iCloud: Share a calendar with others
    Share a calendar privately
    Click the Share button to the right of the calendar name in the sidebar, then select Private Calendar.
    Note:    You can also share the same calendar publicly. For instructions, see the next task, “Share a calendar publicly.” 
    Type the invitee’s email address in the Add Person field and press Return.
    Click the pop-up menu with the invitee’s name and choose access privileges (View & Edit or View Only). 
    For each person you want to share the calendar with, repeat steps 2 and 3.
    When you’ve invited everyone and set their privileges, click OK.
    Invitation emails are sent from your iCloud email address to the participants you specified.
    Thank you for using Apple Support Communities.
    Cheers,
    Sterling

  • I want to accumulate rain for the day, month and year; What formula can I use?

    I want to accumulate rain for the day, month and year; What formula or expression can I use.
    I am using a Rain Wise product that converts pulses to an analog value.  The Rain Wise device can be
    set to measure up to 1", 5", or 10".  I will be setting the unit to 10 inches in increments of 0.01 inches.
    What I would like to do is everytime the signal increments I would like to count it as 0.01 then after reaching
    a period of time whether it be a day or a month reset back to zero.
    Need some advise on this problem.
    Solved!
    Go to Solution.

    Hello Ryan,
    Lookout gets a Modbus over Ethernet signal which originates as a 4-20mA input to a Moxa Ethernet I/O Module (E1240) in the field.  In Lookout I created a ModbusEthernet Driver and a tag which scales 0 - 65534 RAW to 0-10 Eng.  [0 - 10 is inches of rain]  Also, another piece of information is that after the rain gauge maxes out at 10 inches it will zero out and start over.
    I though the accumulator was time based and took a sample over a specific time period, for instance, one sample every 30 seconds then accumulate.  If this is so then if I have 5 inches of rain and then it stopped raining, then 30 seconds latter it would sample, it would see 5 inches and add that to be 10 inches when actually it had only rained 5 inches. 
    I really need some help with this process,
    David Lopez
    City of Corpus Christi

  • I am attempting to have cells use data from a selective month from the year before so that I can show actual from the prio. years each month.

    I am attempting to have cells use data from a selective month from the year before so that I can show actual from the prio. year each month. I need to create a spreedsheet using the the Acutlas from the year-to-date and from last year-to-date, but need to report each month.

    Hi Tony,
    Answering your question would be easier given a screen shot of the source table and one of what you want the summary table to look like.
    Is the data you want for each month in a single cell o the source table, or does the summary table need to collect February's data (for example) from several cells and do some math with those numbers before presenting them on the summary table?
    Regards,
    Barry

  • How do I retrieve call history from my iPhone 4S? It only seems to log for about a month and I need an earlier date?

    How do I retrieve call history from my iPhone 4S? It only seems to log for about a month and I need an earlier date?

    If you have backup of your files, you can get your call history back as long as you sync iPhone with iTunes frequently.
    OK, the first thing you are required to do is close the function of "prevent iPhone from syncing automatically." in iTunes, avoiding to sync iPhone again.
    Now, connect your iPhone to pc and open iTunes.
    iTunes> preferences> devices
    All the backup data in iTunes will be displayed.
    Then,click the File menu and select Devices > Restore from Back up
    your iPhone will restart and you can find them back.
    This method only can find back the latest history, to get back the old, you should turn to a third party program for help.

  • Date Picker: Only select Month and Year

    Hallo,
    I try to configure the Date Picker. For the users it should only be possible to select Month and Year. The column should be automaticly completed with the first day of the select month and a static time (00:00:00).
    How can I do this?
    Sincerly

    You can't do that with a standard date picker. What you could do is to use it and after you change the value in it you modify the value using javascript and ajax similar to what I do in this example:
    http://htmldb.oracle.com/pls/otn/f?p=31517:9
    Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://www.opal-consulting.de/training
    http://apex.oracle.com/pls/otn/f?p=31517:1
    -------------------------------------------------------------------

  • Elements 9 won't select month and year on calendar

    I just got element 9 and have tried to make a calendar for 2011.  The problem is, is that it won't even let me select the month and the year, it keeps on coming up as 2010.  I had a calendar printed out and didn't even notice the wrong year.  Have I done something wrong?  Can anyone help?
    Thanks
    meegsmum

    I'm having the same problem, but for calendar year 2012. If I enter January 2013, the program will provide a calendar for 2012. I've contacted adobe and their fix, is to update the camera raw plugin to 6.5, which won't even download. What this download has to do with creating a folder I have no idea.  I didn't catch the problem, until after I spent $180.00 in printing costs (my bad). Adobe support suggested I should have double checked the calendar before printing.  I believe we should have a product that works and doesn't require the consumer to have figure out how to out smart the bugs! Come on adobe this problem has been an issue for a year now. How about fixing the problem. Does adobe not monitor these forums?

  • To fetch column values based on selected month and year

    Hi,
    I have a requirement where I have 2 prompts Fiscal Year and month on a report.
    Therefore when I select Year and the month I should get the ID's for the selected month till the first month of that fiscal year.
    E.g By selecting July  and year 2013 I should get all the id's in the report from Jan 2013 till July 2013.
    How to acheive his in OBIEE. Please help.

    what is the column for comparing these two dates ? I am considering CALENDAR_DATE
    CALENDAR_DATE BEWEEN
    CAST(concat('01-JUL-', (CASE WHEN pv_month in ('JAN', 'FEB', 'MAR') THEN CAST(((CAST(RIGHT(pv_Year),4) AS INT) -1) as CHAR) ELSE CAST((RIGHT(v_Year),4) as CHAR) END)) AS  DATE)
    AND
    CAST(concat(concat(concat('01-', v_month),'-'),(CASE WHEN v_month in ('JAN', 'FEB', 'MAR') THEN CAST(((CAST(RIGHT(v_Year),4) AS INT) -1) as CHAR) ELSE CAST((RIGHT(v_Year),4) as CHAR) END) AS DATE)
    This logic might work, just play around type casting..
    Regards,
    Srinivas

  • TO GET WEEKS for a given MONTH AND YEAR

    Is there any FM to get the no of weeks for a given month, say 01 2005.
    Can i get the week numbers say 1 2 3 4 5 for the given month and year.
    Please let me know.
    Happy Holidays.
    Regards,
    Manohar E

    Hi Manohar,
       Use FM DATE_GET_WEEK twice; first time pass first day of the month and again pass the last day of the month as a parameter. So u will be having week number(yyyyxx) for the first week of the month and week number for the last week of the month. From these two, in between week numbers u can easily fetch. I hope this will help u out.
    Regards,
    Kavya Shah

  • Help required with Photoshop Elements 4.0 this has been installed on my new Toshiba laptop with windows 8.1 for some 18 months and operating well. Now it will not open, message pops up saying "attempt to access invalid address" then "application not respo

    Help required with Photoshop Elements 4.0 this has been installed on my new Toshiba laptop with windows 8.1 for some 18 months and operating well. Now it will not open, message pops up saying “attempt to access invalid address” then “application not responding” Suggestions how to access would be much appreciated. Tks. Stuart

    What is the size of your hard disk?  PSE 4 is a very old program and I suspect it is not able to access the entire hard disk on your machine.  Just a thought here.

  • Why is my mac so slow i have cleansed and cleaned it is a 2010 mac mini i5 but it runs like a 20 year old pc i have only had it for a few months and it was straight out of the box

    why is my mac so slow i have cleansed and cleaned it is a 2010 mac mini i5 but it runs like a 20 year old pc i have only had it for a few months and it was straight out of the box

    I'm thinking that you have a > Mac mini (Mid 2011) - Technical Specifications because the 2010 model's only have Core 2 Duo processors and the 2011 had the first i5 processor. 
    2 GB of RAM is the minimum requirement for Mountain Lion, but it's hardly enough to really enjoy doing anything else.
    see > Memory and Free Installation Guides for Apple Mac Mini
    Plus I don't have any problems running Mountain Lion and a load of App's on my 2010 Mac Mini with 8GB of RAM.

  • I just restored my iphone4 in iTunes ( after not syncing it for about three months) and it wiped all my texts and contacts and I think restored it to three months ago. Any way of undoing this?        Thanks!! this?

    I just restored my iphone4 in iTunes ( after not syncing it for about three months) and it wiped all my texts and contacts and I think restored it to three months ago. It seems it erased all texts and recent contacts since three months. Any way of undoing this?        thanks in advance, Greg

    Only if you have a more recent backup with which to restore from - if you've only got the one from 3 months ago then you will be limited to what was on that.

  • I have just use my laptop for only three months and my hard disk cable have break and i need a replacement

    i have just use my laptop for only three months and my hard disk cable have break and i need a replacement

    Talk to AppleCare about getting a replacement.
    Allan

Maybe you are looking for

  • How do I set up multiple users with different configurations

    I have a Mac Pro that is shared by multiple users. Some edit using Avid Media Composer 6.0, and some edit using Final Cut Pro 7. Our client monitor is connected using a Matrox MX02 Mini. When I install the latest drivers for the Matrox MX02, I can us

  • Due date for fi open items

    i need to calculate due date for open items as we can see in fbl5n. can ny1 suggest how to calculate dat? points 'll b rewarded

  • How can I tell what files are in Workspaces?

    The email notice says to download your Workspaces files - before Jan 6, 2015. How can I tell what files I have in Workspaces?

  • Having trouble installing Coldfusion Builder?

    I am running Windows 7 and have already installed Coldfusion 9.  When I go to install Coldfusion Builder I get an error even before the installer loads up.  The error I am getting is as follow: C:\Users\Owener\Downloads\Coldfusion..exe The NTVDM CPU

  • LR2 Sluggishness Resolution

    I was having the issue of LR2 being incredible sluggish and slow. I found that uninstalling LR 1.4.1 has resolved much if not all of the delays. Win XP Pro SP2 P4 3GHz 2 GB RAM I was watching the process monitor while running LR2 and LR14 installed (