Grouping by month

I need to display a month-by-month record of several indicators. The data is entered in daily, and I need to do basic things like average, sum, compare to target, etc. I've got a couple of views that simplify the data down to an organization, a date, and a value to ease the UNIONs. Now what I'm struggling with is the grouping of values. I've seen some posts that suggest using TRUNC(my_date,'MM') as a grouping method. My concern is: if the data spans multiple years, won't that grab data from the same months but different years?
Just looking for a little confirmation. If that works, it's and easy solution. If not, can someone suggest a way to group based on both year and date in the same statement? I am trying to pivot/aggregate the information based on date to display for more than a dozen indicators, so I'll be using a CASE statement for the values, thus the need for a single evaluation criteria.
Here's a sample:
select 'C3' as SECTION, '<strong># of Audits</strong>' as HEADING, NULL as OWNER,
        NVL (SUM (CASE WHEN TRUNC(DATE_INDEX,'MM') = TRUNC(to_date((:P3_DATE_START,'MM/DD/YYYY'),'MM')
          THEN VALUE END),0) AS MON1,
        NVL (SUM (CASE WHEN TRUNC(DATE_INDEX,'MM')+1 = TRUNC(to_date((:P3_DATE_START,'MM/DD/YYYY'),'MM')+1
          THEN VALUE END),0) AS MON2,
  from V_VF_SAFETY_AUDITS
where ORG_ID = :P3_ORG_ID
group by 'C3', '<strong># of Audits</strong>', NULL
union
...

Hi,
blarman74 wrote:
... I've seen some posts that suggest using TRUNC(my_date,'MM') as a grouping method. My concern is: if the data spans multiple years, won't that grab data from the same months but different years?Exactly, it will grab data from different years and put them into different groups.
TRUNC (my_date, 'MM') will return the first DATE in the same month as my_date. For example, if my_date is
27-Apr-2009 17:50:03, then TRUNC(my_date, 'MM') will return
01-Apr-2009 00:00:00.
Did you try it in your query? Did it not do what you wanted?
If you want to put everything from the month of April 2009 in the same group with April 2008 (and April 2007, and ...) then use TO_CHAR instead of TRUNC: "GROUP BY TO_CHAR (my_date, 'MM')"
If you need help, post some sample data (that does not work with your existing query, modified to include TRUNC or TO_CHAR) and the correct results you want from that data.

Similar Messages

  • Variable (Perhaps replacement Path) to group past months together

    Hello.
    I'm trying to perform a query that would group past months data within the current month to aggregate my Key Figures.
    As an example, I have in my InfoCube:
    - Material
    - CalendarMonth
    - Initial Qty (Key Figure)
    - Added Qty (Key Figure)
    - Substracted Qty (Key Figure)
    I'd like to get something like my materials on rows, CalendarMonth on column with my 3 Key Figures on rows as well.  The only thing is that all data pertaining to months prior to "Right now (i.e. August 08)" should be aggregated in August 08, therefore, my first month of data displayed would be August and not January 08 if I have data in this month. 
    This might be very basic to you experts out there but I'm pretty new to the nice BW world and can't figure how to do it.  Any help would be appreciated.
    Thank you.
    P.s. I can't find anywhere (except for the small hints on the right when posting a new thread) how I can format my post as I'd like to put TABS and space but they're getting removed in the preview, is there anywhere the list of ALL possible codes I can use when posting ?

                                August 08     September 08     October 08     November 08     December 08     January 09     February 09     March 09
    MATERIAL-1     Initial Qty        20,000                                   
                   Added Qty                            5,000                         8,000         2,000         2,500                         3,000
                 Subst. Qty                          2,000         6,500                     15,000         2,800         6,400       5,000
                 Total                20,000         23,000        16,500        24,500        11,500        11,200         4,800       2,800
    MATERIAL-2     Initial Qty             u2026             u2026            u2026         u2026                   u2026                    u2026            u2026           u2026
                 Added Qty             u2026                 u2026            u2026         u2026                   u2026                    u2026            u2026           u2026
                      Subst. Qty             u2026             u2026            u2026         u2026                   u2026                    u2026               u2026           u2026
                 Total                     u2026             u2026            u2026         u2026                   u2026                    u2026               u2026           u2026
    Ok, I've finally been able to manage a "similar" output to what I'm trying to achieve.  The total line would actually be a calculated key figure based on the three others.  So in the example above, user as specified March 2009 to be the end calendar period he wants to see the inventory "estimates".  All data prior August 2008 should be shown in August 2008 in their respective Key Figures and then the "Total" would adjust accordingly.  So to answer your question, yes, the same logic need to apply to all 3 Key Figures.

  • Group by month

    Hi All
    i m using 10g Forms and Reports. i have the following fields.
    1) ID
    2) Student Name
    3) Father Name
    4) Entry Date.
    now i want reports group by months, for example the report shows me entries of students in seperate months.
    Regards
    Saqib

    Hello,
    I could not understand very well, But if you are talking about the month and year of entry date then query can be like this...
    SELECT student_id, student_name, father_name, TO_CHAR(entry_date,'MON-YYYY') DT
    FROM table_name;Now while creating report in wizard use the group above report and choose the DT field as group and rest field in the below tabular block. Then run the report.
    -Ammad

  • Group by Month, Group By Year

    I have a date column in my table and it looks like 2009/06/29. Is there a way I can GROUP BY MONTH and GROUP BY YEAR on this column?
    Or do I have to create a YEAR column to GROUP BY YEAR, and a month column to GROUP BY MONTH?
    Thanks.

    When you insert a group on date field you will be having an option in group option "The section will be printed" For each month or year. So you can select for each month or week or quarter or year.
    Regards,
    Raghavendra

  • Group by Month then week !!!

    Hey All,
    I would like to group by month of a year and then group by weeks and display the values for the weeks.
    For example: In the year 2008,January . The table should have (January,2008) in the first row and then (week 1 ,week 2 ,week 3 , week 4 and week 5 )
    in the second row . followed by the pertaining values for those weeks.
    Project ID----|----------------------January,2008------------------------|-----
    ----------------|Week 1 | Week 2 | Week 3 | Week 4 | Week 5 |-----
    xx34----------|---- 1--- |---- 2 ---|---- 1 ---|---- 1 ---|---- 1 ---|-----
    It would great if anybody could help me in resolving this usse either through XML Publisher or through SQL coding.
    Thank u all...

    Hey All,
    I would like to group by month of a year and then group by weeks and display the values for the weeks.
    For example: In the year 2008,January . The table should have (January,2008) in the first row and then (week 1 ,week 2 ,week 3 , week 4 and week 5 )
    in the second row . followed by the pertaining values for those weeks.
    Project ID----|----------------------January,2008------------------------|-----
    ----------------|Week 1 | Week 2 | Week 3 | Week 4 | Week 5 |-----
    xx34----------|---- 1--- |---- 2 ---|---- 1 ---|---- 1 ---|---- 1 ---|-----
    It would great if anybody could help me in resolving this usse either through XML Publisher or through SQL coding.
    Thank u all...

  • News articles grouped by month and year

    PHP and MySQL...
    Don't know if I am going to get an answer here, but I am looking for a bit of advice on how to handle grouping of news articles into months and each then into the respective year to make it easier for the user to navigate content. I am trying to acheive this effect:
    2012
    - July
    - June
    - March
    - January
    2011
    - December
    - October
    - April
    2010
    - November
    - You get the idea I hope
    I have set up MySQL to use a 'date' field called fld_articledate (formatted as YYYY-MM-DD). I need to structure my recordset query to select by year and group it, but then I also need to group by month relative to that year, but I have no idea of where to begin. So I guess my question is two-part:
    How do I group by year?
    How do I then group by month of that year?
    Once I have my queries sorted, I assume I'd print to page with  repeat and nested repeat regions!
    Thanks.
    Mat

    PHP and MySQL both have a lot of built in functions for working with dates.
    Here is a little code example from one of my projects that is similar to what you want to do. this example uses PDO, but the query is the same if you are using the older mysql extension. the last line builds an array of the result. You may not need this.Also note that the month and year are returned from the query using aliases (month, year); They could just as well be called "mymonth' and myyear, etc. The second to the last like formats the result for display.
    $sql=$dbh->prepare("SELECT DISTINCT EXTRACT(YEAR FROM(startdate)) AS year, EXTRACT(MONTH FROM (startdate)) as month FROM courses WHERE startdate > $currentdate order by startdate");
    $sql->execute();
    while ($monthorder = $sql->fetch(PDO::FETCH_ASSOC)){
    $month = $monthorder['month'];
    $year = $monthorder['year'];
    $monthtext = date('F Y', mktime(0,0,0,$month,1,$year));
    $monthlist[]=array('month'=>$month, 'year'=>$year);
    Since you want to link to articles, you then need a second query that will retrieve those details based on whatever month is selected. something like this:
    ("SELECT courses.course_id, title, startdate, enddate, highlight_photo FROM courses WHERE startdate > '$currentdate' AND EXTRACT(YEAR FROM (startdate)) = '$year' AND EXTRACT(MONTH FROM (startdate)) = '$month' ORDER BY startdate")

  • Grouping by month within date range

    Hi,
    I have a date range which I like to group by months, but not from the 1st to last date of the calendar month but by given start date.  For example the table below represents the date range with some values in a table.  So the
    first month range in the group should be from 06/06/2013 to 08/07/2013.  The 06/07/2013 is absent because it's Saturday.  In fact the data is populated with working days only.  
    Table below shows what I really would like to get:
    There is a simple calculation for each month anniversary date, if the value from the start date, 06/06/2013, here 12 is less than the next month anniversary date, 08/07/2013, which is 9, then it should ignore it.  And only show the value from the month
    who's anniversary date value is less than the first date value, 12.  So the example above shows value 14 for the second month's anniversary date, 06/08/2013.  If the value has been found in any month the show, and ignore the rest. 
    I am just interested if there are any and report which number of month and the value. 
    I can implement this in procedural way, such as in VBA, but because the above looped many times it will take hours to run the process.  I have tried in SQL Server, but first problem I came up with is that I can't group by month from the given start
    date.
     Is this possible to do in T-SQL ?

    I'm not sure I entirely understand your question.  So I'll break the answer into two parts.
    First, as I understand it, you are looking for a way to group data by months, but not all dates in June, 2013 as one group, all dates in July, 2013 as the next group, etc.  Instead you want all dates from June 6, 2013 to July 5, 2013 as the first month,
    all dates from July 6, 2013 to August 5 as the second month etc.  The way to do that is use
    DATEDIFF(month, '20130606', <your date column>) + CASE WHEN DAY(<your date column>) < 6 THEN -1 ELSE 0 END
    So you can assign the month number you want to every row when you select from your table by
    SELECT DATE, VALUE, DATEDIFF(month, '20130606', DATE) + CASE WHEN DAY(DATE) < 6 THEN -1 ELSE 0 END AS MonthNumber
    FROM <your table name>
    Now as I understand it (but am not sure), you want the first value in each "month" (as you are defining "month" which is greater than the value contained in your start date and if no value in a particular month is greater than that value,
    you don't want any row for that month.  For that you could do something like
    Declare @StartDate date;
    Declare @StartValue int;
    Set @StartDate = '20130606';
    Select @StartValue = Value From <your table name>
    Where Date = @StartDate;
    ;With cteMonthNumber As
    (SELECT DATE, VALUE, DATEDIFF(month, @StartDate, DATE) + CASE WHEN DAY(DATE) < DAY(@StartDate) THEN -1 ELSE 0 END AS MonthNumber
    FROM <your table name>
    WHERE DATE > @Date AND VALUE > @Value),
    cteOrdered As
    (Select DATE, VALUE, MonthNumber,
    ROW_NUMBER() OVER (PARTITION BY MonthNumber ORDER BY DATE) As rn
    From cteMonthNumber)
    Select MonthNumber, Value As KickOutValue
    From cteOrdered
    Where rn = 1;
    Tom
    P.S.  Notice that I wrote my date literal as YYYYMMDD.  This is a good idea in SQL Server since '20130806" will always be interpreted as August 6.  But depending on the settings on your server and/or client '06/08/2013' might be dd/mm/yyyy
    and so be August 6, but it might be interpreted as mm/dd/yyyy and be June 8.
    It is also a good idea to express date literals in this forum as yyyymmdd.  There are a lot of us on the forum from the United States and we think 06/08/2013 is June 6 and a lot of us from other places who think 06/08/2013 is Aug 8.  If you give
    your dates as yyyymmdd we don't have to try to figure out what format you are using.

  • How can I stop iPhoto from grouping each month's Photo Stream into one huge event?

    My iPhoto preferences are set to split events up based on 2-hour time periods. But photos that enter my iPhoto library via Photo Stream (basically all photos from my iPhone) get grouped into one big monthly event (i.e. titled something like "Jun 2012 Photo Stream"), which I then have to manually break up into smaller, more meaningful events.
    Does anyone know of a way to make this behaviour stop? I'd prefer that iPhoto follow the preferences I have set, which is to break up events based on 2-hour time spans.

    The problem is that every import is at least one new Event, and so it's quite possible that you could have 1,000 new Events every month thanks to Phootstream.
    However, it's not such a huge deal to just select the Event at the end of the month and auto-split it (in the Events menu)
    Regards
    TD

  • Group by month/year from DATE FORMAT column

    Hi Chaps,
    I have a recordset that pulls data from a database, then presents the data in a table. The data is grouped by 'projid', repeated for every 'projid', with a show/hide control, to show all the 'jobid's' relating to that particular 'projid'
    This is what I have so far:
    Code:
    SELECT             
    tbl_projects.projid,
    tbl_projects.projtitle,
    tbl_projects.projdue, DATE_FORMAT(tbl_projects.projdue, '%%d/%%m/%%Y') as projdue_format,
    tbl_projects.projtype, 
    tbl_projects.projinvtype,
    tbl_projects.FK_custid,
    tbl_projects.projcompletedate
    tbl_languaget.langtname,
    tbl_doctype.doctypename,
    tbl_jobs.jobid,
    tbl_jobs.FK_projid,              
    tbl_jobs.jobname,              
    tbl_jobs.FK_langid,              
    tbl_jobs.jobpages,              
    tbl_jobs.jobshipped,
    tbl_jobs.jobinvsent,   
    tbl_jobs.jobtranslatorcharge,    
    'tbl_jobs' as fromtable,
    tbl_customers.custid,
    FROM tbl_projects  
    INNER JOIN tbl_jobs              
    ON tbl_projects.projid=tbl_jobs.FK_projid  
    INNER JOIN tbl_languaget              
    ON tbl_languaget.langtid=tbl_jobs.FK_langid  
    INNER JOIN tbl_customers              
    ON tbl_customers.custid=tbl_projects.FK_custid
    INNER JOIN tbl_costs
    ON tbl_costs.FK_custid=tbl_customers.custid 
    INNER JOIN tbl_doctype
    ON tbl_doctype.doctypeid=tbl_jobs.FK_doctypeid
    WHERE tbl_projects.projstatus='Complete'        
    AND tbl_projects.projinvtype='Costing Sheet'
    AND langtname!='TH'
    AND langtname!='ID'
    AND langtname!='KO'
    AND langtname!='JP'
    AND jobinvsent='y'
    AND FK_custid = %s
    ORDER BY projid ASC", GetSQLValueString($colname_rsInvPending, "int");
    $rsInvPending = mysql_query($query_rsInvPending, $conndb2) or die(mysql_error());
    //$row_rsInvPending = mysql_fetch_assoc($rsInvPending);
    $totalRows_rsInvPending = mysql_num_rows($rsInvPending);
    Code:
    // REPEAT - FOR EVERY PROJECT
      <?php
      $previousProject = '';
      if ($totalRows_rsInvPending > 0) {
      // Show if recordset not empty
        while ($row_rsInvPending = mysql_fetch_assoc($rsInvPending)) {
          if ($previousProject != $row_rsInvPending['projid']) {
          // for every Project, show the Project ID
      ?>
    Code:
    // SHOW/HIDE CONTROL
        <tr>
        <td colspan="9" class="highlight"><span class="blueBold"><a href="#" onclick="toggle2('proj1<?php echo $row_rsInvPending['projid'] ?>', this)"><img src="../../Images/plus.gif" border="0" /></a> <?php echo $row_rsInvPending['projid'] ?> - </a></span><span class="blueNOTBold"><em><?php echo $row_rsInvPending['projtitle'] ?></em></span></td>
        </tr>
    Code:
    // SHOW/HIDE
        <?php $previousProject = $row_rsInvPending['projid']; } ?>
        <tr class="proj1<?php echo $row_rsInvPending['projid'] ?>" style="display:none">
        <td>column 1</td>
        <td>column 2</td>
    What I want, is to put in another grouped by stage, where the 'projid's' themselves are in a show/hide region, grouped by 'projcompletedate' (year/month). 'projcompletedate' is in DATE format, but how to I get PHP/SQL to take the month and year and then group them correctly?

    Hi David, I'm not sure if I've got this right, would you mind looking at my latest code?:
    <table border="0" cellpadding="0" cellspacing="0" id="tblinvoice">
        <tr>
          <th>Job Title</th>
          <th>Type</th>
          <th>Language</th>
          <th>Translator</th>
          <th>Total</th>
          <th>Full</th>
          <th>Fuzzy</th>
          <th>Proof</th>
          <th>Full Price</th>
          <th>Discount Price</th>
          <th>Document Format</th>
          <th>Pages</th>
          <th>Typesetting Cost</th>
          <th>EN Proofreading Cost</th>
          <th>Total</th>
          <th>Translator Charge</th>
          <th>Profit</th>
          <th>Profit Percentage</th>
        </tr>
        <?php
          $previousMonth = '';
          if ($totalRows_rsInvPending > 0) {
          // Show if recordset not empty
          do ($row_rsInvPending = mysql_fetch_assoc($rsInvPending)) {
          if ($previousMonth != $row_rsInvPending['themonth']) {
          // for every Month, show the Month Name
       ?>
        <tr>
          <td colspan="18" class="highlight"><?php echo $row_rsInvPending['theyear'] ?> - <?php echo $row_rsInvPending['themonth'] ?></td>
        </tr>
        <?php $previousMonth = $row_rsInvPending['themonth']; } ?>
        <?php
          $previousProject = '';
          if ($totalRows_rsInvPending > 0) {
          // Show if recordset not empty
          while ($row_rsInvPending = mysql_fetch_assoc($rsInvPending)) {
          if ($previousProject != $row_rsInvPending['projid']) {
          // for every Project, show the Project ID
       ?>
       <tr>
        <td colspan="18" class="highlight"><span class="blueBold"><a href="#" onclick="toggle2('proj1<?php echo $row_rsInvPending['projid'] ?>', this)"><img src="../../Images/plus.gif" border="0" /></a> <?php echo $row_rsInvPending['projid'] ?> - </a></span><span class="blueNOTBold"><em><?php echo $row_rsInvPending['projtitle'] ?></em></span></td>
        </tr>
        <?php $previousProject = $row_rsInvPending['projid']; } ?>
        <tr class="proj1<?php echo $row_rsInvPending['projid'] ?>" style="display:none">
            <td><?php echo $row_rsInvPending['jobname']; ?></td>
            <td><?php echo $row_rsInvPending['projtype']; ?></td>
            <td><?php echo $row_rsInvPending['langtname']; ?></td>
            <td><?php echo $row_rsInvPending['translator']; ?></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <?php } while ($row_rsInvPending = mysql_fetch_assoc($rsInvPending)); ?>
          <?php } // Show if recordset not empty ?>

  • Grouped by Month Name.  How do I sort it correctly?

    I have a report grouped by a field name that contains only the names of the month (ie. January, February, etc).
    When displayed on the report, it'll sort the groups alphabetically (ie. April, then August, then December).
    Is there any way to have these group be sorted in the actual month sequence?

    Group on this formula:
    month ()
    This will group numerically 1 through 12.
    Then simply DISPLAY the month name in your group header using this formula:
    MONTHNAME ()

  • Group by month of the date

    I need to generate a report based on the month of a date field.its look like
    month arrived patients
    May 100
    june 129
    July 154
    How can i write the group by select command on the field of a date. Kindly help me.

    SQL> create table mytable (arrival_date,patient)
      2  as
      3  select date '2007-05-10', 'Bob' from dual union all
      4  select date '2007-06-05', 'Mary' from dual union all
      5  select date '2007-06-20', 'Phil' from dual union all
      6  select date '2007-06-25', 'George' from dual union all
      7  select date '2007-07-10', 'Sue' from dual union all
      8  select date '2007-07-15', 'Mike' from dual
      9  /
    Tabel is aangemaakt.
    SQL> select trunc(arrival_date,'mm') month
      2       , count(*) arrived_patients
      3    from mytable
      4   group by trunc(arrival_date,'mm')
      5  /
    MONTH                                     ARRIVED_PATIENTS
    01-05-2007 00:00:00                                      1
    01-06-2007 00:00:00                                      3
    01-07-2007 00:00:00                                      2
    3 rijen zijn geselecteerd.or formatting the first column:
    SQL> select to_char(trunc(arrival_date,'mm'),'month','nls_date_language=american') month
      2       , count(*) arrived_patients
      3    from mytable
      4   group by trunc(arrival_date,'mm')
      5  /
    MONTH                           ARRIVED_PATIENTS
    may                                            1
    june                                           3
    july                                           2
    3 rijen zijn geselecteerd.Regards,
    Rob.

  • Group by month, and also show months with no records

    hi - having a major mental block this morning, pretty sure this is an easy one so just need one of you expert types to point me in the right direction..
    Using the sample data below, I just need to generate a report to show userid, mon-yy, count of transactions. I need to show the full 12 months, so with a zero if there's no transactions.
    I'm not sure if this is the right approach but I'm using a subquery to generate the 12 months, as obvisouly they're not in the transaction data. The fact I have to to_char against the subquery kind of set alarm bells ringing though, I think there's a better solution but as I say, mental block.
    The query at the end was to show the subquery thing works, but for the life of me I can't think today how to extend this to include the userid and give me the desired result (bottom code block).
    Frustrating when this should be relatively simple!?!
    thanks in advance all :o)
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0
    create table trans (pkid number, userid number, transdate date);
    insert into trans values (1, 10, sysdate-300);
    insert into trans values (2, 10, sysdate-100);
    insert into trans values (3, 20, sysdate-100);
    insert into trans values (4, 20, sysdate-50);
    insert into trans values (5, 20, sysdate-20);
    commit;
    select * from trans;
          PKID     USERID TRANSDATE
             1         10 05-AUG-10
             2         10 21-FEB-11
             3         20 21-FEB-11
             4         20 12-APR-11
             5         20 12-MAY-11
    select sub.mon, count(t.transdate) from trans t,
    (select to_char(add_months(sysdate, -level),'MON-YY') as mon
    from dual
    connect by level <=12) sub
    where sub.mon = to_char(transdate(+),'MON-YY')
    group by sub.mon
    order by to_date(sub.mon,'MON-YY')
    MON    COUNT(T.TRANSDATE)
    JUN-10                  0
    JUL-10                  0
    AUG-10                  1
    SEP-10                  0
    OCT-10                  0
    NOV-10                  0
    DEC-10                  0
    JAN-11                  0
    FEB-11                  2
    MAR-11                  0
    APR-11                  1
    MAY-11                  1
    12 rows selected.Desired:
    10  JUN-10  0
    10  JUL-10  0
    10  AUG-10  1
    ...and so on until...
    10  MAY-11  0
    20  JUN-10  0
    20  JUL-10  0
    20  AUG-10  0
    ...and so on until...
    20  FEB-11  1
    20  MAR-11  0
    20  APR-11  1
    20  MAY-11  1

    Hi,
    That's what a Partitioned Outer Join does:
    select        sub.mon
    ,        count (t.transdate)
    from        (
              select      to_char ( add_months (sysdate, -level)
                        , 'MON-YY'
                        ) as mon
              from      dual
              connect by      level      <= 12
           ) sub
    left outer join trans     t     PARTITION BY (t.userid)
                        ON      sub.mon = to_char (transdate,'MON-YY')
    group by  t.userid
    ,       sub.mon
    order by  t.userid
    ,       to_date (sub.mon,'MON-YY')
    ;A regular outer join guarantees that you'll get each row from the driving table displayed at least once. But you want each row from sub displayed n times, where n is the number of distinct userids found in t. "PARTITION BY t.userid" means "treat each distinct value of userid as a separate table", and the results will all be UNIONed together.
    Partitioned outer joins were new in Oracle 10. Before that, you had to form a result set of the distinct values of userid, then cross-join that with the distinct months from sub, and finally outer-join that to the real data.

  • Doubt regarding Grouping of Months

    Hello,
    Can i get any idea or any solution for grouping according to months.... <i.e> The output should appear like
    Jan --- Some calculation --- etc --- etc
    Feb --- Some calculation --- etc --- etc
    Mar --- Some calculation --- etc --- etc
    April --- Some calculation --- etc --- etc
    May --- Some calculation --- etc --- etc
    June --- Some calculation --- etc --- etc

    Hi,
    It depends if you want to group months from several years, or months within a year.
    Using the following sample data :with s as (
    select to_date('2009/01/13','yyyy/mm/dd') dt, 10 val from dual
    union all select to_date('2009/01/19','yyyy/mm/dd') dt, 15 val from dual
    union all select to_date('2009/02/09','yyyy/mm/dd') dt, 15 val from dual
    union all select to_date('2009/02/24','yyyy/mm/dd') dt, 20 val from dual
    union all select to_date('2010/01/11','yyyy/mm/dd') dt, 25 val from dual
    union all select to_date('2010/01/17','yyyy/mm/dd') dt, 30 val from dual
    union all select to_date('2010/02/10','yyyy/mm/dd') dt, 35 val from dual
    union all select to_date('2010/02/21','yyyy/mm/dd') dt, 40 val from dual
    select * from s;To group months within their respective year you can :SQL> with s as (
      2  select to_date('2009/01/13','yyyy/mm/dd') dt, 10 val from dual
      3  union all select to_date('2009/01/19','yyyy/mm/dd') dt, 15 val from dual
      4  union all select to_date('2009/02/09','yyyy/mm/dd') dt, 15 val from dual
      5  union all select to_date('2009/02/24','yyyy/mm/dd') dt, 20 val from dual
      6  union all select to_date('2010/01/11','yyyy/mm/dd') dt, 25 val from dual
      7  union all select to_date('2010/01/17','yyyy/mm/dd') dt, 30 val from dual
      8  union all select to_date('2010/02/10','yyyy/mm/dd') dt, 35 val from dual
      9  union all select to_date('2010/02/21','yyyy/mm/dd') dt, 40 val from dual
    10  )
    11  select to_char(trunc(dt,'month'),'Mon') month, sum(val) sval
    12  from s
    13  group by trunc(dt,'month')
    14  order by trunc(dt,'month') ;
    MON       SVAL
    Jan         25
    Feb         35
    Jan         55
    Feb         75Or you want to group months from several years, you can :SQL> with s as (
      2  select to_date('2009/01/13','yyyy/mm/dd') dt, 10 val from dual
      3  union all select to_date('2009/01/19','yyyy/mm/dd') dt, 15 val from dual
      4  union all select to_date('2009/02/09','yyyy/mm/dd') dt, 15 val from dual
      5  union all select to_date('2009/02/24','yyyy/mm/dd') dt, 20 val from dual
      6  union all select to_date('2010/01/11','yyyy/mm/dd') dt, 25 val from dual
      7  union all select to_date('2010/01/17','yyyy/mm/dd') dt, 30 val from dual
      8  union all select to_date('2010/02/10','yyyy/mm/dd') dt, 35 val from dual
      9  union all select to_date('2010/02/21','yyyy/mm/dd') dt, 40 val from dual
    10  )
    11  select to_char(dt,'Mon') Month, sum(val) sval
    12  from s
    13  group by to_char(dt,'Mon')
    14  order by to_number(to_char(to_date(to_char(dt,'Mon'),'Mon'),'mm')) ;
    MON       SVAL
    Jan         80
    Feb        110

  • Column Grouping By Month

    Hello. Here's my data set:
    I'm trying to produce a Reporting Services report that displays counts by month -- but for both calls open and closed under a single month heading. For example:
    I don't see a straightforward way to do this. I'm using a Tablix in SSRS 2008 R2. I can easily group counts by months if I'm working with just one column (just the open or closed date), but this isn't so clean. I appreciate any ideas. Thank you.

    Hi Bvy,
    I have tested on my local environment and you can modify the query in the dataset to count the open and closed for each month and then design the report, finally we will get the same structure as the snapshot you have provided.
    Details information below for your reference:
    1. Modify the query in the main dataset like below:
    ;with cte as
    select CallCategory,DATEPART(month,DateOpen) DateOpen,DATEPART(month,DateClose) DateClose from [TableName]
    cte2 as
    select sum(case when c.DateOpen is null then 0 else 1 end) cn,t.callcategory,t.dateopen from cte c right join (select * from (select distinct dateopen from cte) a cross join (select distinct callcategory from cte) as t) t
    on c.dateopen=t.dateopen and c.callCategory=t.callcategory group by t.CallCategory,t.DateOpen
    cte3 as (
    select sum(case when c.DateClose is null then 0 else 1 end) cn,t.callcategory,t.dateClose from cte c right join (select * from (select distinct dateClose from cte) a cross join (select distinct callcategory from cte) as t) t
    on c.dateClose=t.dateClose and c.callCategory=t.callcategory group by t.CallCategory,t.dateClose
    select c.callcategory,c.cn as [open], isnull(t.cn,0) as [close],isnull(c.DateOpen,t.DateClose) as month from cte2 c full join cte3 t on c.CallCategory=t.CallCategory and c.DateOpen=t.DateClose
    2. Design the Report structure using the Matrix instead of the tablix like below:
    Row Group:Call_Category
    Column Group:Month
    3. Preview you will get the result like below:
    If you still have any problem, please feel free to ask.
    Regards
    Vicky Liu
    If you have any feedback on our support, please click
    here.
    Vicky Liu
    TechNet Community Support

  • Finding 'Quantity-On-Hand' for an item group by month

    Hi,
    How can I find ITEM (QOH) "Quantity-On-Hand" at the end of each month using sql? Grouping mtl_material_transactions or mtl_onhand_quantities_detail by MONTH does not give the desired results.
    Thanks.
    - Vijay

    Hi Experts
    Kindly help to generate the statement for a given period with details of QOH as on the First Day of the given period, Transactions IN and OUT during the given period and Closing QOH.
    Warm Regards
    Srinivasan K

Maybe you are looking for

  • Download excel File WITHOUT IWDLinkToURL

    Good morning.        I need to open an excel file for the customer to open or save, as it is with a IWDLinkToURL, but I can not do it with a IWDLinkToURL by specifications of the application that I'm doing.        Consider the following code:        

  • ANSI SQL JOIN

    Hi How to use ANSI SQL JOINS (9i) for below query SELECT EMP.EMPNO,EMP_T.TNO,EMP_T.SAL1 FROM EMP,EMP_T WHERE EMP.EMPNO=EMP_T.TNO UNION ALL SELECT EMP.EMPNO,EMP_T.TNO,EMP_T.SAL2 FROM EMP,EMP_T WHERE EMP.EMPNO=EMP_T.TNO UNION ALL SELECT EMP.EMPNO,EMP_T

  • How to get refund?

    Dear Whom it may concerns, My skype account was hacked on 2012, JAN. Skype officer said that they will refund money to me and send email to contact me several times but I didn't receieve refund from skype. So, I want to know that when I will recieve

  • When opening a new tab, how do I make firefox automatically switch to it?

    Pressing Control+T or the "+" button next to tabs will open the new tab. However, I want the new tab to be automatically selected when it is generated. Right clicking links and Opening in new tab works fine.

  • Crash on exit

    Every single time I exit Pages, without fail, it crashes. I don't lose my work, because I've saved when exiting, but it's annoying. Any ideas what to check or fix?