SQL query to get the number of days monthwise

Hello
I'm new to sql, can someone please tell me query to find the number of days between the two dates months wise.
say
FIRSTDATE last date
21/03/2011-25/06/2011

March April May June
9 22 23 18

Hello

Welcome to the forum!

Here's one way:

WITH     all_dates     AS
(
     SELECT     start_date + LEVEL - 1     AS a_date
     FROM     (
               SELECT     TO_DATE ('21/03/2011', 'DD/MM/YYYY')     AS start_date
               ,     TO_DATE ('25/06/2011', 'DD/MM/YYYY')     AS end_date
               FROM     dual
          )
     CONNECT BY     LEVEL     <= end_date + 1 - start_date
)
SELECT       TO_CHAR ( TRUNC (a_date, 'MONTH')
            , 'fmMonth YYYY'
            )               AS month
,       COUNT (*)               AS num_days
FROM       all_dates
WHERE       a_date - TRUNC (a_date, 'IW')     < 5
GROUP BY  TRUNC (a_date, 'MONTH')
ORDER BY  TRUNC (a_date, 'MONTH')
;

What is a 'working day '? I guess you mean every day except Saturday or Sunday, but the query aboveare sometimes figures less than you have asked:

MONTH             NUM_DAYS
--------------- ----------
March 2011               9
April 2011              21
May 2011                22
June 2011               18

Are a few days working on Saturday or Sunday? How do you get the 22 working days in April 2011 and 23 in may?

SQL is good at obtaining results with a variable number of rows, but you have to say exactly the desired number of columns when you write the query.
If you really need the output of the way you said, with any number of columns, then watch in swing or a grouping of the chain . See the FAQ forum
https://forums.Oracle.com/forums/Ann.jspa?annID=1535
"4. How can I convert rows to columns.

Tags: Database

Similar Questions

  • Suggest the Sql query to get the years, months, days of the two dates.

    Hi all

    SELECT TO_DATE('06-02-2014','DD-MM-YYYY') - TO_DATE('01-04-1998','DD-MM-YYYY') SERVICE_DAYS OF THE DOUBLE

    5790 SERVICE_DAYS

    POWER REQUIRED:
    YEARS MONTHS DAYS
    15 10 5

    OUTPUT DISPLAY:

    Select
    Floor(5790/ 365.25),
    Floor (mod (5790,365.25) / (365.25 / 12)),.
    ceil (mod(5790,365.25 / 12))
    Double;

    YEARS MONTHS DAYS
    15 10 7 - TWO DAYS MORE


    How they calculate manually is as follows:

    2014 02-06
    1998-04-01
    ---------------
    05 - days

    2014 02-06
    1998-04-01
    + 12
    ---------------
    10 month 02 is lower than 04, so we borrow 12 months of TI (2014) years will be (2013)

    2014 02-06
    1998-04-01
    -(1).
    ---------------
    15. for the years since we-1

    Kind regards

    Afzal.

    OK... so try not not to multiply the reminder to add_months with 30 but to solve the 'day-thing' in a different way...

    first step (data) is only my implementation testdata.

    WITH DATA AS (SELECT TO_DATE('02-03-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('07-04-2008','DD-MM-YYYY') AS dayfrom

    OF the double

    UNION ALL

    SELECT TO_DATE('12-04-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('11-05-1994','DD-MM-YYYY') AS dayfrom

    OF the double

    UNION ALL

    SELECT TO_DATE('06-02-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('01-04-1998','DD-MM-YYYY') AS dayfrom

    OF the double

    )

    calc AS (SELECT months_between (dayto, dayfrom) SERVICE_DAYS

    Degnide

    dayfrom

    DATA)

    SELECT Degnide

    dayfrom

    floor(service_days/12) IN years

    floor (mod(service_days,12)) AS month

    , BUSINESS sign (EXTRACT (Degnide DAY) - EXTRACT (DAY FROM dayfrom))

    WHEN THEN-1 EXTRACT (DAY FROM Degnide) + 30 - EXTRACT (DAY OF THE dayfrom)

    WHEN 0 then 0

    WHEN 1 THEN EXTRACT (Degnide DAY) - EXTRACT (DAY FROM dayfrom)

    0 OTHERWISE

    end as days

    CALC

    HTH

  • Get the number of days in a month based on the month and year of fields

    I have a column in my form which lists the days in a month. I want to configure a hidden field that calculates the total number of days in a month, based on the month and year of the field inputs. The number of days will determine what appears on the column. For example, if I put 4 months, and 2016 in the field of the year, I get 30 in the hidden field. Thus, on the column 'Day', I'll have numbers 1-30. Or if I put 2 months and 2016 in the field of the year, I get the 29 in the hidden field. If the numbers 1-29 appears in the column 'day '.

    Found this on some forum javascript code:

    //Month is 1 based
    function daysInMonth(month,year) {
      
    return new Date(year, month, 0).getDate();
    }

    //July
    daysInMonth
    (7,2009); //31
    //February
    daysInMonth
    (2,2009); //28
    daysInMonth
    (2,2008); //29

    I do not know how to convert this code in JavaScript to adobe and don't really know how to use it. All I know how to do is to configure the field values for the field month and year as variables. I am a novice programmer and would appreciate it really all the help I can get. Thank you in advance!

    The code seems to be JavaScript and runs as needed by using the JavaScript console.

    I would like to consider making more general code, so if you have a date string that includes at least the month and year we could just call the function and get the number of days for that month.

    The following script will calculate the number of days in a month, by using at least the month and year values can display the result on the JavaScript console and all of the value field for the field that has this code as the custom calculation Script.

    function daysInMonth (oDate) {}
    return new Date (oDate.getFullYear (), oDate.getMonth () + 1, 0) .getDate ();
    }

    nMonth var = this.getField("Month").valueAsString; get the value of month;
    nYear var = this.getField("Year").valueAsString; get the value of the year;

    Event.Value = "";

    If (nMonth! = "" & nYear!) = "") {}
    var MyDate = util.scand ("' / mm/yyyy ', nMonth +" / "+ nYear); convert to date object;
    var nDaysInMonth = daysInMonth (MyDate); get the number of days;

    Console.Open (); Open the JavaScript console;

    Console.clear(); clear the console;

    Console.println ("Days in" + nMonth + ":" + nDaysInMonth); show days in month;

    Event.Value = nDaysInMonth; Set the value of the field;

    }

  • Get the number of days, months and year

    Hi all

    I'm using OBIEE 11 g. I have a line of dash for year (2010, 2011, etc.) and months (Jan, Feb,..., Dec). How can I get the number of days in a given month and year in my report?

    For example, if I choose to Jan & 2011, I'd get 31 and 356. And if I choose Feb & 2011, I would get 28 and 356.

    Thank you!

    I have something more good option here... Try to use this

    DAYOFMONTH (TIMESTAMPADD (SQL_TSI_DAY, DAYOFMONTH (CURRENT_DATE) *-1, TIMESTAMPADD (SQL_TSI_MONTH, 1, CURRENT_DATE)))

    This will give you the days for the month in progress (Oct 2011) after calculating the last day of each month... Similary dynamically pass the value Date or month... You will need to modify this function but shud be simple. Hope this helps :)

  • Compare and get the number of days since the same date field

    Hello

    I need to get the results in days when comparing the same field, date

    For example,.

    Primary_key_field Date_field
    100000002 1 January 10
    100000004 February 1st, 10
    100000005 30 April 10
    100000006 April 18, 10
    100000007 29 April 10
    100000008 May 1st, 10

    extract the first two date_field records based on the primary_key_field. (January 1, 2010 and February 1, 2010) and compare and find the difference and who appear in number of days, the same fetch two disks and do the same math... How can I achieve this... is it possible to make the SQL thro? Please help me solve this problem...

    Thank you and best regards,
    KBG.

    This is the query using functions oracle Analytics

    Primary_key_field Date_field
    100000002 1 January 10
    100000004 February 1st, 10
    100000005 30 April 10
    100000006 April 18, 10
    100000007 29 April 10
    100000008 May 1st, 10

    Select rn, date_field, next, next - date_field "Diff".
    de)
    ROW_NUMBER() SELECT over (ORDER BY primary_key_field) rn,
    date_field, LEAD (date_field) OVER (ORDER BY date_field) next
    FROM table_name)
    /

  • SQL query to get the NULL records after the last matching flag

    I have a xx1 table with id and flag columns. So I want the data in this table, after the last flag matched. I want that data to id 7 in the rooms. Even if the id 2,3,5 are null flag 'Y' was at 6. ID so I need a query to get the data of the xx1 table after the last correspondence flag (from 7 to 9 id).

    SQL > create table xx1

    2 (identification number,

    3 flag varchar2 (10));

    Table created.

    SQL > insert into xx1 values (1, 'Y');

    1 line of creation.

    SQL > insert into values xx1 (2, null);

    1 line of creation.

    SQL > insert into values xx1 (3, null);

    1 line of creation.

    SQL > insert into xx1 values (4, 'Y');

    1 line of creation.

    SQL > insert into values xx1 (5, null);

    1 line of creation.

    SQL > insert into xx1 values (6, 'Y');

    1 line of creation.

    SQL > insert into values xx1 (7, null);

    1 line of creation.

    SQL > insert into values xx1 (8, null);

    1 line of creation.

    SQL > insert into values xx1 (9, null);

    1 line of creation.

    SQL > select * from xx1.

    FLAG OF THE ID

    ---------- ----------

    1. IS

    2

    3

    4. IS

    5

    6. IS

    7

    8

    9

    9 selected lines.

    SQL >

    Hello

    user11164339 wrote:

    Hi Frank - when I run the query, I don't see the results data.

    I get

    FLAG OF THE ID

    ----- ----------

    7

    8

    9

    What you do differently?

  • Query to get the number of records

    Hi all

    I would get the number of records in all tables in a schema. I don't want to run select count (*) of < table name >, because there are nearly 400 paintings. So I need a generic quqeyr that will give the recordcount for all tables.

    Experts good help yourslef to tide me over.

    Do you need a specific account? Or an approximate count is sufficient? Do you use the RBO and CBO?

    If you use the CBO and want an approximate count, column NUM_ROWS USER_TABLES should give you what you want.

    If you need a specific account, you will have full scan all tables. You can automate this process via SQL dynamic, i.e.

    DECLARE
      l_cnt INTEGER;
    BEGIN
      FOR x IN (SELECT table_name FROM user_tables)
      LOOP
        EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || x.table_name INTO l_cnt;
        dbms_output.put_line( 'Table ' || x.table_name || ' has ' || l_cnt || ' rows.' );
      END LOOP;
    END;
    

    Lawrence was also a [solution based on XML Query | http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html] to count all the rows in each table in a schema.

    Justin

  • get the number of days of the week to sysdate

    Hi all

    my need is to get the number of the day of the week of sysdate.

    Here's how can I make it right now:

    Select to_char(sysdate,'Dy') t1 double

    But he returns the abbreviated name of the day of the week while I need number of days of the week.

    Please help me. Thanks in advance.

    Best regards

    --

    Marco

    Or if you want the ISO week (Monday = day 1):

    Select trunc (sysdate) - trunc(sysdate,'IW') + 1 double;

  • IOM sql Query to get the status of the failed task

    Hello world

    We have an obligation as we need to get the status of a particular task (say Create User in OID - Completed\Rejected status resource) for the particular user. We are able to get the status of the provisioed of resources to the user but not the status of the special mission which is trigerred for the user.can someone put some light on it. We must have the SQL query to do this.

    Thanks in advance.

    Kind regards
    MKN

    Hello
    Use this query to get the status of the task, also check the cooments

    SELECT USR. USR_LOGIN, OSI. SCH_KEY, ANN. SCH_STATUS, STA. STA_BUCKET OF
    OSI, CHS, STA, MIL, TOS, PKG, OUEDRAOGO, USR, OBJ, OST
    WHERE OSI.MIL_KEY = MIL.MIL_KEY
    AND ANN. SCH_KEY = OSI. SCH_KEY
    AND STA. STA_STATUS = SCH. SCH_STATUS
    AND TOS. PKG_KEY = PKG. PKG_KEY
    AND MIL. TOS_KEY = TOS. TOS_KEY
    AND OUÉDRAOGO. USR_KEY = USR. USR_KEY
    AND OUÉDRAOGO. OST_KEY = OST. OST_KEY
    AND OST. OBJ_KEY = OBJ. OBJ_KEY
    AND OSI. ORC_KEY = OUEDRAOGO. ORC_KEY
    AND OBJ. OBJ_NAME = "User AD".
    AND OST. OST_STATUS = "Provisioning" - filter accordinglly
    AND STA. STA_BUCKET = 'pending' - filter accordinglly
    AND PKG. PKG_NAME = "AD User" - filter accordinglly
    AND MIL.MIL_NAME = 'System' - filter accordinglly Validation
    ;
    Thank you
    Kuldeep

  • How to get the number of days in a month of belonging to a range of dates

    Hi, I'm going crazy around a problem, I have 2 dates and one month I wanto to retrieve the number of days belonging to the months that fall within the range.

    for example:

    month of January 2011. begin_date = January 11, 2011, May 30, 2011 end_date result is 21
    month of January 2011. begin_date = December 11, 2010, result of end_date, January 10, 2011 10
    month January 2011 .begin_date = 2 February 2011, end_date may 25, 2011 result 0
    month of January 2011. begin_date = January 3, 2011, January 5, 2011 result DATE END 3

    and so on...

    I appreciate any suggestion
    Thank you


    Andrea

    Something like that... ?

    SQL> with t as
      2  (select  to_date('11/01/11','dd/mm/yy') from_dt,
      3           to_date('30/05/11','dd/mm/yy') to_dt,
      4           'Jan-11' mnth from dual)
      5  select least(last_day(to_date(mnth,'Mon-yy')),to_dt) -
      6         greatest(to_date(mnth,'Mon-yy'),from_dt) cnt
      7  from t  ;
    
           CNT
    ----------
            20
    

    If 0 is also expected...

    SQL> with t as
      2  (select  to_date('11/01/11','dd/mm/yy') from_dt,
      3           to_date('30/05/11','dd/mm/yy') to_dt,
      4           'Jan-11' mnth from dual union all
      5           select  to_date('11/01/11','dd/mm/yy') from_dt,
      6           to_date('30/05/11','dd/mm/yy') to_dt,
      7           'Jan-12' mnth from dual
      8           )
      9  select from_dt,to_dt,mnth,
     10         greatest(
     11              least(last_day(to_date(mnth,'Mon-yy')),to_dt)
     12              -
     13              greatest(to_date(mnth,'Mon-yy'),from_dt)
     14                 ,0) cnt
     15  from t;
    
    FROM_DT   TO_DT     MNTH          CNT
    --------- --------- ------ ----------
    11-JAN-11 30-MAY-11 Jan-11         20
    11-JAN-11 30-MAY-11 Jan-12          0
    

    Published by: JAC on February 9, 2012 19:22

  • SQL query to get the numbers from 0 to 99

    How can we write a sql query to get values from 0 to 99... This should not come from any table
    SELECT LEVEL - 1
      FROM  DUAL
      CONNECT BY LEVEL <= 100
    /
    

    SY.

  • How to get the number of days

    Hai bloggers,
    See us have 365 days a year for all first half we'll have 180 + days (if we choose first half as the guest and we select week as guest number and the last date of this particular week must indicate in the report) and the number of days until this date or a week should also display... If we choose the second half and half week number it should show number of days until this week and more Date of this issue of the day week should be from 1 to 180 + only way (July 1 m is 1)

    Thank you

    You must use the timestampdiff function to calculate the number of days. One of the way to calculate is how

    cases where "Time". "" Calendar semester "= 1 then timestampdiff (SQL_TSI_DAY, CAST (EVALUATE ('TO_DATE (%1, %2)','' 01-01 |' @{YEAR} {2010}',"DD-MM-YYYY") AS DATE), cast OFF (EVALUATE ('TO_DATE' (1%, 2%), 30-06' |' @{YEAR} {2010}',"DD-MM-YYYY") AS DATE)) else timestampdiff (SQL_TSI_DAY, MOUNT (EVALUATE ('TO_DATE (%1, %2)','01-07' |' @{YEAR} {2010}',"DD-MM-YYYY") AS DATE), cast OFF (EVALUATE ('TO_DATE' (1%, 2%),' 31-12 |' @{YEAR} {2010}',"DD-MM-YYYY") AS DATE)) end

    If you want to calculate between the 1st day of the half and the last date a week then after

    cases where "Time". ' ' Calendar semester ' = 1 then timestampdiff (SQL_TSI_DAY, CAST (EVALUATE ('TO_DATE (%1, %2)','01-01' |' @{YEAR} {2010}', 'DD-MM-YYYY') AS DATE), MAX (MAX ("Time".) "Calendar Date"))) else timestampdiff (SQL_TSI_DAY, CAST (EVALUATE ('TO_DATE (%1, %2)','01-07' |' @{YEAR} {2010}', 'DD-MM-YYYY') AS the DATE), MAX (MAX ("Time".) " End Calendar Date")))

    Hope that it will answer your question

    Thank you
    Phani.

  • SQL query to get the items for which transactions have not been saved

    Hi people,

    Have a small doubt I want to compile a SQL query in the inventory where I get these items for what transactions do not have been recorded during a period of at least one specified number of days.

    The days might be 30 days or 2 months depends on. If I want to get those items for which no transactions were reported for lets say 30 days. Could someone give me an idea of how to go about this thing? I use r12. I came up with the following query, but it gives a lot of records. The commented parts of this query is only commented on


    SELECT DISTINCT msi.segment1,

    MSI. Description,

    MSI.primary_uom_code,

    MSI.inventory_item_id

    MSI mtl_system_items_b / *,.

    mtl_material_transactions mmt * /.

    WHERE / * msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND NVL ((SELECT SUM (transaction_quantity)

    OF mtl_onhand_quantities

    WHERE inventory_item_id = msi.inventory_item_id),

    0) = 0

    AND TRUNC (mmt.transaction_date) < = SYSDATE - & D

    AND * / NOT EXISTS (SELECT *)

    OF mtl_material_transactions mmt

    WHERE msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND TRUNC (mmt.transaction_date) < SYSDATE - & D);

    Hi fatimakhellil

    Try the following code and mark it as correct if your problem be resolved.

    SELECT DISTINCT msi.segment1,

    MSI. Description,

    MSI.primary_uom_code,

    MSI.inventory_item_id,

    MSI.organization_id,

    Ms.secondary_inventory_name,

    (select max (transaction_date) in the mtl_material_transactions where msi.inventory_item_id = inventory_item_id

    AND msi.organization_id = organization_id) last_transaction_date

    OF mtl_system_items_b msi.

    Ms. mtl_secondary_inventories

    WHERE ms.organization_id (+) = msi.organization_id

    AND msi.source_subinventory = ms.secondary_inventory_name (+)

    AND NOT EXISTS (SELECT *)

    OF mtl_material_transactions mmt

    WHERE msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND mmt.subinventory_code = ms.secondary_inventory_name

    AND mmt.organization_id = ms.organization_id

    AND TRUNC (mmt.transaction_date) BETWEEN TRUNC (SYSDATE - & d) AND TRUNC (SYSDATE))

  • SQL query to get the dates between two dates

    Hello

    We have a chart with start date and end date... Now I need to get all the dates between the start date and end date...

    Table looks to below...

    Create the table date_table (start_date, end_date date);

    The table data will be as below:

    start_date end_date

    January 1, 2013 January 4, 2013

    February 1, 2013 February 3, 2013

    ...............          .................

    ...............          ..................

    ...............           .................

    May 1, 2013 may 3, 2013


    I want a result like below...


    holiday_dates

    January 1, 2013

    January 2, 2013

    January 3, 2013

    January 4, 2013

    February 1, 2013

    February 2, 2013

    February 3, 2013

    .................

    .................

    .................

    .................

    May 1, 2013

    May 2, 2013

    May 3, 2013


    Can anyone help... ?

    Ramesh9158 wrote:

    Hello

    Your query will not work for our case...

    First... We do not know the number of rows in the table... If we cannot use the union...

    Second... hard coding of dates... but we do not know what could be that goes back in the table... it could be no matter what it takes not only as appearing to the interpreter...

    Hey riri.

    My code will work everywhere I use with with union all statement to create the example data.

    Try the query:

    -The main query

    Select d1 + row_number() over (partition by iden) - stopped by iden holiday 1

    Of

    connect by level<= d2="" -="" d1="" +="">

    and prior iden iden =

    and prior sys_guid() is not null

    ----

    Ramin Hashimzade

  • SQL query to get the range of Date values

    Hello

    The database is Oracle11i.

    I'm looking for a way to generate a list of the dates of a fixed date in the past (could be hardcoded) at the time of the day (sysdate).

    In other words, if the fixed date is June 19, 2011, and assuming today ' today is June 24, 2011 the SQL must be able to generate the

    Next: -.

    June 19, 2011
    June 20, 2011
    June 21, 2011
    June 22, 2011
    June 23, 2011
    June 24, 2011

    And the constraint is that I can not make any change to the database in question. I can only shoot a (SELECT) SQL query. NO.

    use time dimension type of approach (time dimension is not available here) and no procedure, etc. from PL/SQL. Is it possible?

    Thank you

    Jaimeen Shah wrote:
    Hello

    The database is Oracle11i.

    I'm looking for a way to generate a list of the dates of a fixed date in the past (could be hardcoded) at the time of the day (sysdate).

    In other words, if the fixed date is June 19, 2011, and assuming today ' today is June 24, 2011 the SQL must be able to generate the

    Next: -.

    June 19, 2011
    June 20, 2011
    June 21, 2011
    June 22, 2011
    June 23, 2011
    June 24, 2011

    And the constraint is that I can not make any change to the database in question. I can only shoot a (SELECT) SQL query. NO.

    use time dimension type of approach (time dimension is not available here) and no procedure, etc. from PL/SQL. Is it possible?

    Thank you

    SQL> def date_start = '13/11/2010'
    SQL> def date_end   = '22/11/2010'
    SQL> with
      2    data as (
      3      select to_date('&date_start', 'DD/MM/YYYY') date1,
      4             to_date('&date_end',   'DD/MM/YYYY') date2
      5      from dual
      6    )
      7  select to_char(date1+level-1, 'DD/MM/YYYY') the_date
      8  from data
      9  connect by level <= date2-date1+1
     10  /
    THE_DATE
    ----------
    13/11/2010
    14/11/2010
    15/11/2010
    16/11/2010
    17/11/2010
    18/11/2010
    19/11/2010
    20/11/2010
    21/11/2010
    22/11/2010
    

Maybe you are looking for

  • Solutions to the problem of CD/DVD drive cannot read DVD discs

    This article will tell you 3 ways to solve the problem of the CD/DVD drive do not read the DVD disc. Please the process step by step. Solution1. Clean the DVD disc If your Player cannot detect the DVD disc, the first thing you need to do is to clean

  • Compaq fan not working, overheating and stopping.

    I've had my Compaq Presario CQ60 - 227ca for about 2 years now and after getting my favorite fan on this last time he stopped working. The laptop since I bought it was always heating to a high temperature, but the fan just always worked harder. Think

  • Electromagnetic radiation on monitors $

    Does anyone know where to find the measures of electromagnetic radiation for different monitors? When I called the company, the representative was helpless as to what electromagnetic radiation is. Environmental data sheets ideally leave this empty (P

  • HP pavilion dv 7 ev 4050: new battery and new charger

    Hello! I'm looking for new battery and new charger for my laptop but I'm not sure to buy... any help? also want to know the measures of the charger plug.

  • Black screen, caps lock flashes on startup

    Dear everybody. I know there are already a lot of similar topics, but none seem to portray exactly my situation, so I hope someone will be able to land a helpful hand. Then. A few months ago I had to move to Japan in my country (Italy). The warranty