Month count

Oracle -9i Release 2
How do I write a sql query to get month-wise count for actual working day, i.e. exclude saturday and sundays from the month count, is there a way to do it, i have the following query that gives total count for each month, also it is not ordered by month.
SELECT TO_CHAR(xmonth.ddate,'MON'),COUNT(1)
FROM (SELECT TRUNC(SYSDATE,'mon') + ROWNUM - 1 ddate
FROM ALL_OBJECTS
WHERE TRUNC(SYSDATE,'mon') + ROWNUM - 1 <= LAST_DAY(TO_DATE('12/31/2005','mm/dd/yyyy')) ) xmonth
GROUP BY TO_CHAR(xmonth.ddate,'MON')

The following example should help.
  1  SELECT TO_CHAR(MYDATE,'MONTH YYYY') MONTH, COUNT(*) COUNT
  2  FROM(SELECT DATE '2004-01-01'+ ROWNUM -1 MYDATE
  3         FROM(SELECT 1 FROM DUAL
  4               GROUP BY CUBE(1,2,3,4,5,6,7,8,9)))
  5  WHERE MYDATE < DATE '2005-01-01'
  6  AND TO_CHAR(MYDATE,'DY') NOT IN ('SAT','SUN')
  7  GROUP BY TO_CHAR(MYDATE,'MONTH YYYY')
  8* ORDER BY TO_DATE(TO_CHAR(MYDATE,'MONTH YYYY'),'MONTH YYYY')
SQL> /
MONTH                               COUNT
JANUARY   2004                         22
FEBRUARY  2004                         20
MARCH     2004                         23
APRIL     2004                         22
MAY       2004                         21
JUNE      2004                         22
JULY      2004                         22
AUGUST    2004                         22
SEPTEMBER 2004                         22
OCTOBER   2004                         21
NOVEMBER  2004                         22
DECEMBER  2004                         23
12 rows selected.
SQL> disconnect
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production

Similar Messages

  • Month count from 0CALMONTH

    I am trying to get the count of the month from 0CALMONTH using an exit variable. Is this code correct? Thanks.
      WHEN 'ZMONTHCT'.
        IF i_step = 2.
    data: month_num type i.
            LOOP AT I_T_var_range
               INTO loc_var_range WHERE vnam = '0PCALMONTH' or
                                        vnam = 'ZCMONTH2' OR
                             vnam = 'ZCMONTH3' OR
                             vnam = 'ZCMONTH4'.
            month_num = loc_var_range-low+4(2).
            l_S_range-low = month_num.
            l_s_range-sign = 'I'.
            l_s_range-opt = 'EQ'.
            APPEND l_s_range TO E_T_range.
            EXIT.
          ENDLOOP.
        ENDIF.

    sohini,
    Instead of doing all that...
    Take the calmonth variable value - have a formula variable in your query to get the text value of the charactewristic..
    Lets take 022007...
    The internal format would be 022007 use the formula to calculate absolute value of (022007 / 10000) you would get 2 which is what you want - you need not use any exit for this or use the other number functions to get the value that you want when you divide this value by 10000 ...
    Arun
    Assign points if useful

  • Months:  Counting backwards

    To create a rolling display in a report I would like to allocate sequence numbers to months (in this case counting backwards).
    For example, if the current month is November, then 200611 would be 01, 200610 would be 02, 200609 would be 03, etc.
    I can supply a number (neg or pos) to command ADD_MONTHS to calculate months in the future or in the past but is there a way to do the oposite: To declare two months and return the number of months difference between these? For example if the two months are 200603 and 200511, the code would return '4' (Nov 2005 is four months before Mar 2006).
    I hope that someone out there has a trick (a snippet of syntax) to do this? That would be a great help to us.
    Many thanks,
    Alan Searle

    select months_between(to_date('200603', 'YYYYMM'), to_date('200511', 'YYYYMM')) from dual
    4Regards
    Dmytro

  • Count days in a month for a date range

    i am trying to find no. of days between 2 Date Ranges for a list of Ids. i used the logic in the below link:
    count days of the month
    My query is giving duplicates since, I have list of Ids.
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15
    25391509     42972298     2/4/2013 22:45     3/8/2013 22:03
    746779     42972331     1/4/2013 23:00     3/26/2013 21:50
    763305     42972338     3/4/2013 22:19     3/6/2013 19:35
    763305     42972411     11/4/2013 22:32     3/29/2013 17:30
    I am looking for query to give me for Patient_ID = 42972229
    MONTH     COUNT_DAYS
    201210     14
    201211     30
    201212     31
    201301     31
    201302     28
    201303     14
    I am running the following code and it loops through the months and gives duplicates when I remove where Patient_id IN (42972229)
    select
    Doctor_ID
    , Patient_ID
    , AR_DTTM
    , DSC_DTTM
    , TO_CHAR(ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1), 'YYYY MM') MONTHS_BET
    , (LEAST(date2, ADD_MONTHS(TRUNC(date1, 'MONTH') - 1, LEVEL)) - GREATEST(date1, ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1)))+ 1 AS DAYSCOUNT
    from (select
    Doctor_ID
    , Patient_ID
    , ARRIVE_DT_TM AR_DTTM
    , DISCH_DT_TM DSC_DTTM
    ,TRUNC(ARRIVE_DT_TM,'DDD') AS date1
    ,TRUNC(DISCH_DT_TM,'DDD') AS date2
    from temp where Patient_id IN (42972229)
    CONNECT BY LEVEL <= MONTHS_BETWEEN(TRUNC(date2, 'MONTH'), TRUNC(date1, 'MONTH')) + 1
    Please help!

    Hi,
    ASTRA_007 wrote:
    Results I would like to see are:
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM     Month     CountofDays
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 10     14
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 11     30
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 12     31
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 01     31
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 02     28
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 03     14
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 01     25
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 02     28
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 03     15
    and so on...So each row represents a patient-month, and you want to display several columns from the temp table on each output row. In that case, include all those columns in both the SELECT and GROUP BY clauses, like this:
    WITH     universe     AS
         SELECT     *
         FROM     temp
    --     WHERE     patient_id     IN (42972229)
    ,     date_range     AS
         SELECT     TRUNC (MIN (arrive_dt_tm))     AS first_date
         ,     TRUNC (MAX (disch_dt_tm))     AS last_date
         FROM     universe
    ,     all_dates     AS
         SELECT     first_date + LEVEL - 1     AS a_date
         FROM     date_range
         CONNECT BY     LEVEL     <= (last_date + 1) - first_date
    SELECT    u.doctor_id
    ,       u.patient_id
    ,       u.arrive_dt_tm
    ,       u.disch_dt_tm
    ,       TO_CHAR ( TRUNC (a.a_date, 'MONTH')
                  , 'YYYY MM'
                )          AS month
    ,       COUNT (*)          AS count_days
    FROM       all_dates  a
    JOIN       universe   u  ON  a.a_date  BETWEEN  TRUNC (u.arrive_dt_tm)
                                         AND      u.disch_dt_tm
    GROUP BY  u.doctor_id
    ,       u.patient_id
    ,       u.arrive_dt_tm
    ,       u.disch_dt_tm
    ,         TRUNC (a.a_date, 'MONTH')
    ORDER BY  u.patient_id
    ,       TRUNC (a.a_date, 'MONTH')
    ;Output from your sample data (with no filtering):
    `DOCTOR_ID PATIENT_ID ARRIVE_DT_TM     DISCH_DT_TM      MONTH   COUNT_DAYS
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 10         14
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 11         30
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 12         31
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 01         31
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 02         28
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 03         14
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 01         25
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 02         28
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 03         15
      25391509   42972298 2/4/2013 22:45   3/8/2013 22:3    2013 02         25
      25391509   42972298 2/4/2013 22:45   3/8/2013 22:3    2013 03          8
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 01         28
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 02         28
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 03         26
        763305   42972338 3/4/2013 22:19   3/6/2013 19:35   2013 03          3
    In the end the objective is to count the no. of days in each month between the arrival and discharge dates by Physician and for his/her patients.Then is the output above really what you want? Say you're interested in physician 763305. That physician had 18-patient days in March, 2013, but the output doesn't make it clear.
    I ran your query, it works great but I have a long list of patients for whom I have to run these counts.the query above includes all patient_ids.
    That's a separate problem, to be solved in the first sub-query, universe. The rest of the query will be unchanged.
    How will you know which patients to include? If you can derive the list from temp itself, just use a WHERE clause in universe. If you need to look at other tables, join them in universe, or use them in sub-queries in universe, or both.
    For exmple, if you decide that the list of patient_ids has no pattern, and that you'll need to store their ids in a separate table (perhaps a global temporary table), then universe might be:
    WITH     universe     AS
         SELECT     t.*     -- or list columns needed
         FROM     temp                        t
         JOIN     patient_ids_to_include  p 
                      ON  p.patient_id = t.patient_id
    ) ...The rest of the query can be the same as above.
    If a same patient is admitted again then Patient_ID will be different no matter when readmitted.Are you saying that patient_id identifies a visit, not a patient, and that the same person is assigned a different patient_id every time that person is admitted?
    For
    INSERT INTO temp (doctor_id, patient_id, arrive_dt_tm, disch_dt_tm)
    VALUES ( 755722
    , 42972229
    , TO_DATE ('03/14/2013 23:00', 'MM/DD/YYYY HH24:MI')
    , TO_DATE ('04/01/2013 12:00', 'MM/DD/YYYY HH24:MI')
    First the Patient ID will be different from the earlier admission. Second the results will show like:
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM     Month     CountofDays
    755722     42972229     3/14/2013 23:00     4/1/2013 12:00     2012 03     14
    755722     42972229     3/14/2013 23:00     4/1/2013 12:00     2012 04     1Are you saying that temp.patient_id is unique, and so the situation is impossible?
    Edited by: Frank Kulash on May 7, 2013 10:23 AM

  • Line chart to plot count of 2 date fields against calendar month

    Post Author: springle
    CA Forum: Charts and Graphs
    I would like to create a line chart with 2 lines.  I would like the x axis to be a calendar month.  I would like the y axis to be the count of a date field for that month.  I would like 2 lines, one the the count of applications recieved in a month and the other to be the number of shipments in a month.  I can make a chart to plot one or the other but not both on one chart.So in summary I want of a count of date1 and a count of date2 by calendar month in one line chart.Thanks in advance,Scott

    Post Author: springle
    CA Forum: Charts and Graphs
    Thanks for the reply but I am afraid this does not solve my problem.  Your proposal make the month count of Date1 records in the fields and the count of Date2 in the month of date1 I am try to graph...In the month of Jan: 56 apps and 49 deliveriesIn the month of Feb: 76 apps and 79 deliveriesIn the month of Feb: 86 apps and 69 deliveriesThe change on date cannot be either app date or del date.  It needs to be independent from a calendar timeline. Scott 

  • How to get count for missing month

    I created a view as follows:
    CREATE OR REPLACE FORCE VIEW "Vinfection1" ("MONTH", "COUNT") AS
    select "MONTH","COUNT" from (
    select to_char(s.pdate,'Mon-yyyy') as month, count(*) as count
    from surproc s, diagnosis_surproc d
    where s.surprocid = d.surprocid and d.diagnosisid in ('506', '507', '508', '509', '510')
    group by to_char(pdate,'Mon-yyyy'))
    order by to_date(month,'Mon-yyyy')
    Some months are missing in the view. I want to put that month and 0 in the view. How to do it?
    Thanks,
    Jennifer

    Hi, Jennifer,
    You need to outer-join to some table or result set (such as all_months, below) that has one roe per month.
    Here's one way:
    CREATE OR REPLACE FORCE VIEW Vinfection1 (month, month_s, cnt) AS
    WITH    all_months     AS
         SELECT  ADD_MONTHS (first_month, LEVEL - 1)     AS month
         FROM     (
                  SELECT  TRUNC (MIN (pdate), 'MONTH')     AS first_month
                  ,         TRUNC (MAX (pdate), 'MONTH')     AS last_month
                  FROM    surproc
         CONNECT BY     LEVEL <= 1 + MONTHS_NETWEEN (last_month, first_month)
    SELECT  a.month
    ,     TO_CHAR (a.month, 'Mon-yyyy')     AS month_s
    ,     COUNT (*)                 AS cnt
    FROM              all_months      a
    LEFT OUTER JOIN      (     surproc               s
                   JOIN   diagnosis_surproc      d  ON   s.surprocid     = d.surprocid
                                          AND  d.diagnosisid     IN ('506', '507', '508', '509', '510')
               )     ON  a.month     = TRUNC (s.pdate)
    GROUP BY  a.month
    /Avoid table- and column names that are non-standatrd (such as names that need double-quotes), or are built-in function names (such as COUNT).
    Don't use ORDER BY in a view. Almost anything you do with the view will cause the ordering to be lost anyway.
    Even if you do have an ORDER BY clause in the view, you'll often want to ORDER BY month. Rather than call TO_DATE whenever you need to sort, I suggest having two month columns; month (whcih is a DATE) for sorting, filtering and joining, and month_s (whcih is a VARCHAR2) for displaying.
    Use ANSI join syntax, especiallly for outer joins.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on Mar 20, 2012 12:03 PM
    Added outer join condition

  • Count days per month

    dear members,
    I have been provided with the date range.
    i want to calculate the no. of days for each month, within the above date range.
    e.g. date1='10-01-2011' and date2='18-03-2011'
    for month=01 count days=22
    for month=02 count days=28
    for month=03 count days=18
    thanks
    teefu
    developer

    WITH     parameters     AS
         SELECT     TO_DATE ( '10-01-2011'
                   , 'DD-MM-YYYY'
                   )     AS start_dt
         ,     TO_DATE ( '18-03-2011'
                   , 'DD-MM-YYYY'
                   )     AS end_dt
         FROM     dual
    SELECT     TO_CHAR ( start_dt + LEVEL - 1
              , 'FMMonth YYYY'
              )     AS month
    ,     COUNT (*)     AS days
    FROM     parameters
    CONNECT BY     LEVEL <= 1 + end_dt - start_dt
    GROUP BY TO_CHAR ( start_dt + LEVEL - 1
              , 'FMMonth YYYY'
    ORDER BY MIN (LEVEL)
    duplicate thread already one has given the above answer
    count days of the month
    Edited by: LPS on Jul 21, 2011 2:16 AM

  • SSRS expression for today,yesterday,Lastweek ,Last fortnight,Last Month, Year to date

    Hi All;
    I have a field called createdon 
    Using this field i need to create the SSRS expression for the table as below 
    Any help much appreciated
    Thanks
    Pradnya07

    use expressions as below
    assuming this is to be done in SSRS
    Today
    =COUNT(IIF(
    DateDiff(DateInterval.Day,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Day,Cdate("01/01/1900"),Now()),Fields!YourRequiredField.Value,Nothing))
    Yesterday
    =COUNT(IIF(
    DateDiff(DateInterval.Day,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Day,Cdate("01/01/1900"),Now())-1,Fields!YourRequiredField.Value,Nothing))
    LastWeek
    =COUNT(IIF(
    DateDiff(DateInterval.Week,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Week,Cdate("01/01/1900"),Now())-1,Fields!YourRequiredField.Value,Nothing))
    Last fortnight
    =COUNT(IIF(
    (DateDiff(DateInterval.Week,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Week,Cdate("01/01/1900"),Now())-1)
    Or (DateDiff(DateInterval.Week,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Week,Cdate("01/01/1900"),Now())-2),Fields!YourRequiredField.Value,Nothing))
    Last Month
    =COUNT(IIF(DateDiff(DateInterval.Month,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Month,Cdate("01/01/1900"),Now())-1,Fields!YourRequiredField.Value,Nothing))
    Year To Date
    =COUNT(IIF(DateDiff(DateInterval.Year,Cdate("01/01/1900"),Fields!createdon.Value) = DateDiff(DateInterval.Year,Cdate("01/01/1900"),Now())
    And
    DateDiff(DateInterval.Day,Cdate("01/01/1900"),Fields!createdon.Value) <= DateDiff(DateInterval.Day,Cdate("01/01/1900"),Now()),Fields!YourRequiredField.Value,Nothing))
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • What is the best way to create a SSRS 2005 Line Chart Report for a 12 month period?

    I'm looking for advice on how to create a SQL Server 2005 query and line chart report for SSRS 2005.
    I need to display the peak number of patients assigned to a medical practice each month for a 12 month period based on the end-user selecting a
    single month and year.
    I've previously created a report that displays all patients assigned to the practice for any single month but I’m looking for advice on how to
    how to produce a resultset that shows the peak number of patients each month for a 12 month period. I thought about creating a query that returns the peak count for each month (based on my previously created report which displays all patients assigned to the
    practice for any single month) and then use a UNION statement to join all 12 months but I'm sure that isn't the most efficient way to do this. The other challenge with this approach (twelve resultsets combined via a UNION) is that the end-user needs to be
    able to select any month and year for the parameter and the report needs to display the 12 month period based on the month selected (the month selected would be the last month of the 12 month period).
    For the report I’ve previously created that displays all patients assigned to the practice for any single month, the WHERE statement filters the
    resultset on two fields:
    Start Date - The date the patient was assigned to the practice. This field is never null or blank.
    End Date - The date the patient left the practice. This field can be null or blank as active patients assigned to the practice do not have an End Date. When the patient
    leaves the practice, the date the patient left is populated in this field.
    Using these two fields I can return all patients assigned to the practice during Nov 2012 by looking for patients that meet the following criteria:
    start date prior to 11/30/2012 (using the last day of the month selected ensures patients added mid-month would be included)
    AND
    end date is null or blank (indicates the patient is active) OR the end date is between 11/1/2012 -11/30/2012 (returns patients that leave during the month
    selected)
    Regarding the query I need to create for the report that displays the peak count each month for 12 months, I'm looking for advice on
    how to count patients for each month the patient is assigned to the practice if the patient has been assigned for several months (which applies to most patients). Examples are:
    John Doe has a start date of 6/01/2012 and an End Date of 10/07/2012
    Sally Doe has a start date of 8/4/2012 and no End Date (the patient is still active)
    Jimmy Doe has a  start of 7/3/2012 and an End Date of 9/2/2012
    Given these examples how would I include John Doe in the peak monthly count each month for May - October, Sally Doe in the peak monthly count for
    August - December and Jimmy Doe in the peak monthly count for July – Sept if the end-user running the report selected December 2012 as the parameter?
    Given the example above and the fact I'm creating a line chart I think the best way to create this report would be a resultset that looks like
    this:
    Patient Name              
    Months Assigned
    John Doe
    June 2012
    John Doe                     
    July012
    John Doe                     
    Aug 2012
    John Doe                     
    Sept 2012
    John Doe
    Oct 2012
    Sally Doe                     
    Aug 2012
    Sally Doe                     
    Sept 2012
    Sally Doe
    Oct 2012
    Sally Doe                     
    Nov 2012
    Sally Doe
    Dec 2012
    Jimmy Doe                  
    July 2012
    Jimmy Doe
    Aug 2012
    Jimmy Doe
    Sept 2012
    From the resultset above I could create another resultset that would count\group on month and year to return the peak count for each month:
    June 2012 - 1
    July 2012 – 2
    Aug 2012 - 3
    Sept 2012 - 3
    Oct 2012 - 2
    Nov 2012 - 1
    Dec 2012 - 1
    The resultset that displays the peak count for each month would be used to create the line chart (month would be the X axis and the count would
    be the y axis).
    Does this sound like the best approach?
    If so, any advice on how to create the resultset that lists each patient and each month they were assigned to the practice would be greatly appreciated.
    I do not have permissions to create SPs or Functions within the database but I can create temp tables.
    I know how to create the peak monthly count query (derived from the query that lists each patient and month assigned) as well as the line chart.
    Any advice or help is greatly appreciated.

    Thanks for the replies. I reviewed them shortly after they were submitted but I'm also working on other projects at the same time (hence the delayed reply).
    Building a time table and doing a cross join to my original resultset gave me the desired resultset of the months assigned between dates. What I can't figure out now is how to filter months I don't want. 
    Doing a cross  join between my original resultset that had two dates:
    08/27/2010
    10/24/2011
    and a calendar table that has 24 rows (each month for a two year period)
    my new resultset looks like this:
    I need to filter the rows in yellow as the months assigned for stage 3 that started on 8/27/2010 should stop when the patient was assigned to stage 4 on 10/24/2011.
    You'll notice that Jan - Sept 2011 isn't listed for Stage 4 assigned on 10/24/2011 as I included a filter in the WHERE clause that states
    the Months Assigned value must be greater than or equal to the date assigned value.
    Any advice would be appreciated.

  • Assign Month within a date range (by most days in a given month)

    I have a begin and end date, sample data as such
    select to_date('01-13-12','mm-dd-yy') from_dt,
    to_date('02-23-12','mm-dd-yy') to_dt
    from dual
    union all
    select to_date('03-15-2012','mm-dd-yy') from_dt,
    to_date('04-16-2012','mm-dd-yy') to_dt
    from dual
    union all
    select to_date('05-13-2012','mm-dd-yy') from_dt,
    to_date('07-23-2012','mm-dd-yy') to_dt
    from dual
    How do I assign a month by the most days in a month within that date range? Sometimes the date range might have the exact same amount of days in a month (like 3/15/2012 has 16 days and 4/16/2012 has 16 days). In this case, I want the earlier month (march).
    So from the sample data:
    01/13/2012, 02/23/2012, February
    03/15/2012, 04/16/2012, March
    05/13/2012, 07/23/2012, June
    Thanks
    Edited by: user4422426 on Mar 1, 2012 5:15 PM

    Hi,
    Here's one way:
    WITH     cntr          AS
         SELECT     LEVEL - 1     AS n
         FROM     (
                   SELECT      1 + MAX (to_dt - from_dt)     AS max_day_cnt
                   FROM     table_x
         CONNECT BY     LEVEL     <= max_day_cnt
    ,     got_r_num     AS
         SELECT     x.from_dt, x.to_dt
         ,     TRUNC (x.from_dt + c.n, 'MONTH')     AS month
         ,     count (*)                    AS cnt
         ,     ROW_NUMBER () OVER ( PARTITION BY  from_dt, to_dt
                             ORDER BY        COUNT (*)     DESC
                             ,             TRUNC (x.from_dt + c.n, 'MONTH')
                           )     AS r_num
         FROM       cntr     c
         JOIN       table_x  x  ON  c.n  <= x.to_dt - x.from_dt
         GROUP BY  x.from_dt, x.to_dt
         ,       TRUNC (x.from_dt + c.n, 'MONTH')
    SELECT     from_dt, to_dt
    ,     TO_CHAR (month, 'Mon YYYY')     AS mon
    ,     cnt
    FROM     got_r_num
    WHERE     r_num     = 1
    ;Thanks for posting code to create the same data. Please test your code before you post it: you got the order of arguments to TO_DATE reversed.

  • Report by Day/Week/Month/Year

    Hi All,
    I have the table showing below
    Empid     name     tikcetno     Completed date
    694     anil     10051     23-Jun-09
    695     madhu     10052     23-Jun-09
    694     anil     10053     23-Jun-09
    695     madhu     10054     22-Jun-09
    695     madhu     10055     6-Jan-09
    I need to create the report which will show employee wise count of tickets in a day,week,month and year.                  
    Day: count(tickets) for current date
    Week: count(tickets) for current week
    Month: count(tickets) for current month
    Year: count(tickets) for current year
    I need to show all these four columns in a horizantal table. I am using crystal reports2008. Please help me. ur help would be appreciated.
    Anil.

    Hi,
    create 4 formulas for day, week, Month and year
    for ex, in Day formula
    IF Date Field = CurrentDate then 1 else 0... similarly for other ranges. Create a Group based on employee, and insert a SUM  summary field of these formulas in Group header.
    Hope this will help you.
    Jyothi
    Edited by: Jyothi Yepuri on Jul 13, 2009 3:04 AM

  • Getting Counts with single query

    HI,
    I need help in writing a query that gets account counts in a single query,
    CREATE TABLE ACCOUNTINFO(     
    ACCOUNTID VARCHAR2(20 BYTE) NOT NULL,
    ACCOUNTNO VARCHAR2(10 BYTE) NOT NULL,
    LAST_DEPOSIT_DATE DATE,
    BALANCE NUMBER(10,0));
    I have a table like above and I am trying to write a query that gets
    Count of accounts with deposits made in last 1 month,
    Count of accounts with deposits made in last 2 months
    Account Count with balance > 0,
    Also, I need to join this ACCOUNTINFO with ACCOUNTMAIN to get name etc details
    CREATE TABLE ACCOUNTINFO(     
    EMPID VARCHAR2(20 BYTE) NOT NULL,
    FNAME VARCHAR2(30 BYTE) NOT NULL,
    MNAME VARCHAR2(30 BYTE),
    LNAME VARCHAR2(30 BYTE) NOT NULL,
    DOB DATE,
    ACCOUNTID VARCHAR2(20 BYTE));
    Question, how to write a query since I getting too-many counts (I have only 3 in sample above, actual goes on like 3-6, 6-9 etc).

    SELECT SUM  (CASE WHEN LAST_DEPOSIT>=ADD_MONTHS(SYSDATE,-1) THEN
                   1
                 ELSE
                   0
                 END
                ) COUNT_LAST_MONTH,
           SUM  (CASE WHEN LAST_DEPOSIT>=ADD_MONTHS(SYSDATE,-2) THEN
                   1
                 ELSE
                   0
                 END
                ) COUNT_LAST_TWO_MONTHS,
           SUM  (CASE WHEN BALANCE>0 THEN
                   1
                 ELSE
                   0
                 END
                ) COUNT_BALANCE_GREATER_ZERO
      FROM ACCOUNTINFO

  • Pivot with Month and Year

    Hi all Thanks in Advance
    I need help in Pivoting Data
    SELECT ID,MONTH,COUNT FROM TABLE_PIVOT ORDER BY ID,MONTH
    ID     MONTH          COUNT
    10     10/01/2009     60
    10     11/01/2009     80
    10     12/01/2009     78
    10     01/01/2010     81
    10     02/01/2010     73
    10     03/01/2010     84
    10     04/01/2010     100
    10     05/01/2010     107
    10     06/01/2010     90
    10     07/01/2010     0
    10     08/01/2010     0
    10     09/01/2010     73
    20     10/01/2010     71
    20     11/01/2010     76
    20     12/01/2010     79
    20     01/01/2011     79
    20     02/01/2011     81
    20     03/01/2011     88
    20     04/01/2011     97
    20     05/01/2011     87
    20     06/01/2011     97I tried to pivot with below query
    SELECT ID,
    SUM(DECODE(to_char(month,'MM'),'01',count,0)) " Jan",
    SUM(DECODE(to_char(month,'MMYY'),'02',count,0)) Feb,
    SUM(DECODE(to_char(month,'MM'),'03',count,0)) Mar,
    SUM(DECODE(to_char(month,'MM'),'04',count,0)) Apr,
    SUM(DECODE(to_char(month,'MM'),'05',count,0)) May,
    SUM(DECODE(to_char(month,'MM'),'06',count,0)) June,
    SUM(DECODE(to_char(month,'MM'),'07',count,0)) July,
    SUM(DECODE(to_char(month,'MM'),'08',count,0)) August,
    SUM(DECODE(to_char(month,'MM'),'09',count,0)) September,
    SUM(DECODE(to_char(month,'MM'),'10',count,0)) October,
    SUM(DECODE(to_char(month,'MM'),'11',count,0)) November,
    SUM(DECODE(to_char(MONTH,'MM'),'12',count,0)) December
    FROM Table_PIVOT
    GROUP BY ID
    ORDER BY ID
    ID      Jan     FEB     MAR     APR     MAY     JUNE     JULY     AUGUST     SEPTEMBER     OCTOBER     NOVEMBER     DECEMBER
    10      81     0     84     100     107     90     0     0          73          60          80          78
    20      79     0     88     97     87     97     0     0          0          71          76          79I want output to display the column names with Month and Year like below
    ID     Oct-2009     Nov-2009     Dec-2009   Jan-2010  Feb-2010 ................... OCT-2010  NOV-2010 DEC-2010 JAN-2011 FEB-2011 ......
    10     60          80          78          81          73     ...................
    20                                                                    71           76          79     79          81
    CREATE TABLE "TABLE_PIVOT"
       (     "ID" NUMBER,
         "MONTH" DATE,
         "COUNT" NUMBER
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('10/01/2009','MM/DD/YYYY'),60);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('11/01/2009','MM/DD/YYYY'),80);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('12/01/2009','MM/DD/YYYY'),78);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('01/01/2010','MM/DD/YYYY'),81);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('02/01/2010','MM/DD/YYYY'),73);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('03/01/2010','MM/DD/YYYY'),84);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('04/01/2010','MM/DD/YYYY'),100);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('05/01/2010','MM/DD/YYYY'),107);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('06/01/2010','MM/DD/YYYY'),90);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('07/01/2010','MM/DD/YYYY'),0);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('08/01/2010','MM/DD/YYYY'),0);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (10,to_timestamp('09/01/2010','MM/DD/YYYY'),73);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('10/01/2010','MM/DD/YYYY'),71);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('11/01/2010','MM/DD/YYYY'),76);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('12/01/2010','MM/DD/YYYY'),79);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('01/01/2011','MM/DD/YYYY'),79);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('02/01/2011','MM/DD/YYYY'),81);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('03/01/2011','MM/DD/YYYY'),88);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('04/01/2011','MM/DD/YYYY'),97);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('05/01/2011','MM/DD/YYYY'),87);
    Insert into TABLE_PIVOT (ID,MONTH,COUNT) values (20,to_timestamp('06/01/2011','MM/DD/YYYY'),97);
    COMMIT;

    Hi,
    user1849 wrote:
    Any Sample code is appreciated
    I didn't see any solution for following one in your linklI think Centinul was specifically referring to:
    Help for a query to add columns
    but other links from
    SQL and PL/SQL FAQ
    may help you more.
    >
    Re: How to pipeline a function with a dynamic number of columns?
    Posted: May 9, 2006 2:58 PM in response to: Billy Verreynne Reply
    Interesting stuff! It's going to take me awhile to digest it.
    For what it's worth, I was trying to build a pivoting function that would take a MYTABLE table like this:
    YEAR CITY X Y
    2000 BAL 95 96
    2000 BOS 101 101
    2001 BAL 92 94
    2001 BOS 101 101
    2002 BAL 98 98
    2002 BOS 98 99
    2003 BAL 95 96
    2003 BOS 105 104
    and allow me to do something like:
    CREATE VIEW MYPIVOT
    AS
    SELECT *
    FROM TABLE (PIVOT(MYTABLE, [with other params]))
    and get the following view MYPIVOT on the table:
    YEAR BOS_X BOS_Y BAL_X BAL_Y
    2000 101 101 95 96
    2001 101 101 92 94
    2002 98 99 98 98
    2003 105 104 95 96
    Where the number of distinct CITY values will vary over time. I am able to build the query I need dynamically, but since the CITY data values in the original table change, the columns are not necessarily static from invocation to invocation. Therefore I didn't want to just create a view using the dynamic SQL once, because it may need to be created next time I need to access the view. I wanted to be able to access the pivoted data on demand.A pipelined function is your best bet for that.
    I couldn't do was be able to execute the query and treat it as a pipelined function, hence my original question.Sorry, I don't understand.
    I'll dig into the code above to see if it does what I wanted, but if someone has a better suggestion on how to approach this, I'd be interested in hearing it.A completely different approach is String Aggregation , where you would get output like this:
    YEAR  TXT
            BOS_X    BOS_Y    BAL_X    BAL_Y
    2000      101      101       95       96
    2001      101      101       92       94
    2002       98       99       98       98
    2003      105      104       95       96Note that this output contains 6 rows and 2 columns. On the first row, year is NULL and txt='  BOS_X    BOS_Y    BAL_X    BAL_Y'. You can do this is pure, static SQL, without knowing the number of cities in advance.

  • Month average

    I would really like to make a query (I think that is the right word) to determine the average number of sales for each month (for example, Jan thru Dec). Not the average number of per month!
    I have a table as follows:
    CREATE TABLE sale
    id               VARCHAR2(32)     NOT NULL,
    item               VARCHAR2(32)     NOT NULL,
    saledate          DATE,
    PRIMARY KEY(id),
    Populated with data like:
    001 AAA 08-FEB-00
    002 ZZZ 14-MAR-01
    003 HHH 06-OCT-02
    004 DDD 30-MAR-03
    005 KKK 12-APR-03
    006 GGG 22-SEP-04
    007 BBB 15-MAR-04
    008 BBB 16-MAR-04
    009 SSS 17-MAR-04
    010 SSS 18-MAR-04
    011 BBB 19-MAR-04
    I did manage to create the below query which returns the average sales per month but this is NOT want I wish for:
    SELECT AVG(TOTAL)
    FROM (SELECT MONTH, COUNT(MONTH) as TOTAL
    FROM (SELECT TO_CHAR(saledate, 'MON') as MONTH from sale)
    GROUP BY MONTH);
    I would like an average output per month, example:
    JAN     0
    FEB     0
    MAR     2
    DEC     0
    I have no idea...
    Thanks...

    Okay, you want the average of the monthly sales. You can get that by doing this:
    SELECT to_char(x.month, 'MON') AS month, avg(mon_tot) AS avg
    FROM   ( SELECT trunc(month, 'MM') AS month, count(*) AS mon_tot
             FROM   sales
             GROUP  BY trunc(month, 'MM') x
    GROUP BY to_char(x.month, 'MON');Now your other problem is filling in the gaps for months where there are no sales. You will find Help Required for date display & mutating trigger to be helpful if you are using Oracle9i or higher, otherwise you will need to use Justin's variation.
    Cheers, APC

  • How to calculate PR converted to PO this month

    Hi experts,
    Can any one guide me on this issue both modelling and reporting.
    Selection Parameter from table EBAN u2013
    Plant (WERKS) u2013 As per selection criteria entered by the user
    Processing Status (STATU) u2013 B
    Release indicator (FRGKZ) u2013 2
    Requisition Date (BADAT) u2013 01.09.2009 to Last Month Date as entered by the user.
    Based on above select query fetch all the PO numbers for each of the PR from table EBAN.
    Then filter from above fetched POu2019s from table EBAN based on below selection criteria from table EKKO u2013
    Company Code of Plant  (BUKRS) u2013 Company Code of Plant entered by the user
    Created on (AEDAT) - 01.07.2010 to 19.07.2010 (If user is executing report on 19.07.2010)
    PR converted to PO this month = Count of the PO filtered based on above selection criteria
    Regards
    Rk

    Hi,
    I am bit confused with your requirement.
    EBAN table is for PReqs. In EBAN table if you put STATU (Processing status of purchase requisition) as B then you get all the PReqs which got converted into POs. So this will provide you the number of PReqs converter on POs for given date selection.
    If this is your requirement then staging of EBAN will satisfy your need. Just check how you are providing seletions for date/plant in report.
    If you want to display same data from EKKO table as well in report or you want to filter some values on the basis of data in EKKO field then you need to stage EKKO as well.
    Now question how to model this?
    As you said 1. Create Infoset of cubes in BW 2. Create a View in R/3
    Both are same approach as for both you need to create one join (Infoset or View) now question is that how to join both tables or Cubes. I don't think is there any key on which you may join both because in both the tables key is PO number and PReq number. Both are seperate from each other so we can not join both unless we have some link. The link is available in EKET table so we need to stage that as well.
    If you can provide the filed list and filter conditions in required report then we can think of better option.
    I hope it will help.
    Thanks,
    S

Maybe you are looking for

  • Itunes has stopped working when trying to open store

    I have gone through the uninstall/reinstall many times. Redid host file. Flush dns. Have tried many things. I noticed on one conversation on here. To go to the Action Center and click on itunes. It is saying a fault in the webkit.dll. I have no clue

  • Sharing a Printer between XP and Windows 7 computers on a Wireless Network

    Does anyone know if you can share a printer on a home network between a windows 7 and an XP SP3 computer? I had a wireless netowork set up at home with an XP desktop hardwired to a cable modem and a printer with USB connection. I also had an XP lapto

  • Many of the fonts on my PC are not available in InDesign CS5.5?

    Hi I am using a font called Narkisim in FW but it is not available in my InDesign CS5.5. I have checked the 'font' folder for InDesign and actually its empty, though the preset standard fonts are all there. Really not sure whats going on here, please

  • How to determine the Support Team from the Reported by?

    Hello Everyone, I am working with Solution Manager 4.0 and I want to use Automatic Partner Determination in Service Desk. My employer wants the Support Team (SLFN0003) to be determined by the Reported by (SLFN0002). There are already parts of the Aut

  • Email HTML Rendering bug?

    I have a system that sends me an HTML-formated email everyday. It appears that the IPhone 3G, running 2.2 and 2.2.1, is not able to render that email when it is large. For example, if the size of the email is 380K, the Iphone shows me the email as te