How to build a SQL query to print ranges of week of a month

Hi gurus!

I have a requirement to create a query to display the times of week of a month.
The entries will be Year and month.
The result should be something like this:

If the year = 2009 and month = June

The SQL query should return the following:

June 1, 2009-June 6, 2009
June 7, 2009 to June 13, 2009
June 14, 2009-June 20, 2009
June 21, 2009-June 27, 2009
June 27, 2009 to June 30, 2009

I'm confused how to proceed. Please help me. Any help would be appreciated grately.

Thank you
Ahmed

Hello

This might help (the exit is not exactly the same):

Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr

SQL>
SQL> SELECT TRUNC(to_date('01-Jun-2009'),
  2               'D') + 7 * (LEVEL - 1) AS START_DATE,
  3         TRUNC(to_date('01-Jun-2009'),
  4               'D') + 7 * (LEVEL - 1) + 6 AS END_DATE
  5    FROM dual
  6  CONNECT BY LEVEL <= 4;

START_DATE  END_DATE
----------- -----------
31/05/2009  06/06/2009
07/06/2009  13/06/2009
14/06/2009  20/06/2009
21/06/2009  27/06/2009

SQL> 

Kind regards

Tags: Database

Similar Questions

  • Please help build a sql query

    Hello

    Please help build a sql query


    My Table Test2015 has given below

    Header_id Line_id Ordered_item       

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887292 SH-POS-NO-BR POS-INS

    199926 723887293 MAIN2

    199927 723887294 IC-ENV-NON-BR-ENV-PXY

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.

    199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE

    I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');

    But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.

    While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.

    Header_id Line_id Ordered_item            

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887293 MAIN2

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199929 723887299 MAIN1

    Please suggest.

    Thank you and best regards,

    Prasad.

    Hello

    Try like this...

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)

  • How to build a SQL for this logic.

    Hello

    I have a requirement like, I need to derive a column with a calculation.

    Here is the psedocode,

    number of occurrence (deptno = 10) / count (deptno = 10)

    Expected result:

    If the deptno = 10 occurs 3 times in the table, then at the time of fist should be the calcuation

    second 1/3, 2/3 of the time, the third time 3/3 and so on.

    How can I build in a sql query. Help, please.

    Any help will be appreciated with points.

    Thank you.

    Try this.

        SELECT LEVEL / CNT CALC
          FROM (SELECT COUNT (*) CNT
                  FROM EMP
                 WHERE DEPTNO = 10)
    CONNECT BY LEVEL <= CNT
    

    G.

  • How to find the sql query generated by BI Publisher

    I'm looking for the physical sql query generated by BEEP when I send parameters as part of the data model.

    Of course, this can be noted in newspapers...

    Can anyone tell me the location of newspapers?

    Thanks in advance

    Have a look here http://gerardnico.com/wiki/dat/bip/log
    I explain a bit how you can get the newspaper.

    See you soon
    Nico

  • How to get the sql query result?

    Hello

    Currently I use LV2012 to connect to an Oracle database server. After the Oracle Express and Oracle ODBC driver facilities/settings made.

    I managed to use the SQL command to query the data through my command prompt window.

    Now the problem is, how to do the same task in Labview using database connectivity tools?

    I have build a VI to query as being attached, but I have no idea of what range to use to get the result of the query.

    Please help me ~ ~

    Here is a piece of code that I use to test the SQL commands, you can use the part that retrieves the results of sql.

    It is also possible to get the rear column headers, but it's for the next lesson!

    ;-)

  • How to get the SQL query running of af: search?

    I use JDeveloper 11.1.2.3.0. I have a page where I've set up an af:query and an array of result. The problem is that I can't get the exact query that is used during the execution of this component in a managed bean method. Is it possible to get the query string that is run within the af: query?

    Thank you

    Hello

    Method of the ViewObject getQuery() returns what you need;

    You can replace the executeQueryForCollection(), and prior to calling super, you can print the result of getQuery() method:

    public void executeQueryForCollection (rowset Object,

    Object [] params,

    {int noUserParams)

    System.out.println ("SQL =" + getQuery());

    call the super method here...

    }

  • How to find the sql query

    four months back someone updated my table of billing through sql developer.

    How can I find the details of the request or the original data.

    now the problem is that we rebooted the server, if it's not rest with time stamp.

    log mode is Archieve record only.
    Please help me

    I don't know why you're getting this error, I tried and it works:

    SQL> exec dbms_logmnr.add_logfile( logfilename=> 'C:\oracle\product\11.2.0\dbhome_1\RDBMS\ARC0000000736_0765299251.0001', options=> dbms_logmnr.new);
    
    PL/SQL procedure successfully completed.
    
    SQL> EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);
    
    PL/SQL procedure successfully completed.
    
    SQL> exec dbms_logmnr.start_logmnr
    
    PL/SQL procedure successfully completed.
    
    SQL> select seg_owner,seg_name,sql_redo,sql_undo from v$logmnr_contents;
    
    .....
    
    35 rows selected.
    
    SQL> exec dbms_logmnr.end_logmnr
    
    PL/SQL procedure successfully completed.
    

    @Boris - you don't have to have an additional connection to the database. The thing is that if you don't have additional logging, Oracle will give you only the ROWID of the records and if it is not the same database or something has changed (as "alter table... move"), ROWID is not relevant. If you enable additional logging, you will get more information on the line (like the other columns or PK) which will help you a lot to identify RELEVANT changes. As I said, it is not mandatory, but as you say, if you want extra logging, it must be turned on BEFORE the changes in the database.

    Hope that's clear,
    Liron

  • How to get the sql query

    Hello

    I already set date value as below.
    SET THE VALUE OF START_TIME = 2011-08-12 09:00
    DEFINE END_TIME = 2011-08-12 10:00
    and executes the table based on the defined date as below,
    Select * from my_table
    where
    (to_date (Accounting_Start_Time,'yyyy-mm-dd HH24:MI:SS) > = to_date ('& start_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Stop_Time,'yyyy-mm-dd HH24:MI:SS) < = to_date ('& end_time ',' yyyy-mm-dd HH24:MI:SS))
    GOLD to_date (Accounting_Start_Time,'yyyy-mm-dd HH24:MI:SS) > = to_date ('& start_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Stop_Time,'yyyy-mm-dd HH24:MI:SS) > = to_date ('& end_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Start_Time,'yyyy-mm-dd HH24:MI:SS) < = to_date ('& end_time ',' yyyy-mm-dd HH24:MI:SS)
    GOLD to_date (Accounting_Start_Time,'yyyy-mm-dd HH24:MI:SS) < = to_date ('& start_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Stop_Time,'yyyy-mm-dd HH24:MI:SS) < = to_date ('& end_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Stop_Time,'yyyy-mm-dd HH24:MI:SS) > = to_date ('& start_time ',' yyyy-mm-dd HH24:MI:SS)
    GOLD to_date (Accounting_Start_Time,'yyyy-mm-dd HH24:MI:SS) < = to_date ('& start_time ',' yyyy-mm-dd HH24:MI:SS) and to_date (Accounting_Stop_Time,'yyyy-mm-dd HH24:MI:SS) > = to_date ('& end_time ',' yyyy-mm-dd HH24:MI:SS));
    ) but now I want to query the table based on the time,

    Any help please,.

    Hello

    It is not very hard in SQL * more.

    First of all, put your query in a script file, like this one, called hour_query.sql:

    --     hour_query.sql          Show data from my_table for a given time period
    
    PROMPT     The data below shows the period from &1 to &2
    PROMPT
    
    SELECT     *
    FROM     my_table
    WHERE     accounting_start_time     <= '&2'
    AND     '&1'               <= accounting_stop_time
    ;
    

    Parameters & 1 and & 2 are beginning and end of time, for example "2011-08-12 09:00".
    Note that this does not use TO_DATE. If you have incorrect strings in columns that must be DATEs, no errors occur.

    Assuming that hour_query.sql is located on p:\some_dir\, you want another script that runs hour_query.sql 24 times, like this one, which I will call all_hours.sql:

    @p:\some_dir\hour_query  "2011-08-12 09:00:00"  "2011-08-12 10:00:00"
    @p:\some_dir\hour_query  "2011-08-12 10:00:00"  "2011-08-12 11:00:00"
    @p:\some_dir\hour_query  "2011-08-12 11:00:00"  "2011-08-12 12:00:00"
    ...
    

    (I just did 3 hours to test. You can easily make that 24 hours).

    The following code creates and then runs all_hours.sql:

    -- Turn off SQL*Plus features that interfere with raw output
    SET     FEEDBACK     OFF
    SET     PAGESIZE     0
    SET     VERIFY          OFF
    
    -- Write all_hours.sql
    SPOOL     p:\&some_dir\all_hours.sql
    
    WITH     got_start_time     AS
    (
         SELECT TO_DATE ( '2011-08-12 09:00:00'
                            , 'YYYY-MM-DD HH24:MI:SS'
                     ) AS start_time
         FROM    dual
    )
    SELECT     '@p:\some_dir\hour_query  "'
        ||  TO_CHAR ( start_time + ((LEVEL - 1) / 24)
              , 'YYYY-MM-DD HH24:MI:SS'
              )
        ||  '"  "'
        ||  TO_CHAR ( start_time + ( LEVEL      / 24)
              , 'YYYY-MM-DD HH24:MI:SS'
              )
        ||  '"'
    FROM    got_start_time
    CONNECT BY     LEVEL <= 3     -- You can make this 24, or any other number
    ;
    
    SPOOL     OFF
    
    -- Turn on SQL*Plus features turned off earlier
    SET     FEEDBACK     ON
    SET     PAGESIZE     50
    -- Except VERIFY.  leave that OFF while all_hours runs
    --SET     VERIFY          OFF
    
    -- Run all_hours.sql
    @p:\some_dir\all_hours.sql
    
    SET     VERIFY     ON
    
  • How to find the sql query by using the hash value

    Hello

    DBVERSION: 9.2.0.8

    I generated the statspack report and I want to see total request and I hash_value here.

    in v$ sql can't see all querys... .pls suggest me other ways...

    Thank you
    Srini...

    Have you seen Peter's response? There is no installation other than that would be to store the history of the IMO. Another reason that you must pass to 10g or 11g better, now.

    Aman...

  • Need help to build the SQL query

    Hi all

    I struggle to create the select statement.

    This is my expected outcome:

    Insert into tmp (aa, bb, cc, dd)

    values('CUST1','10100001',2,'10-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST1','10100002',4,'11-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200001',2,'19-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200002',6,'28-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200003',1,'28-Mar-2013');

    My result is:

    AA COUNT (1) MAX (DD) by cc dd AA AA

    CUST1 2 10100002 4 11 January 2013

    CUST2 3 10200003 1 28 MARCH 2013

    Can someone help with the select statement?

    Thank you and best regards,

    Iwan

    Just change it to the column:

    SELECT

    AA

    COUNT (aa)

    , MAX (bb) keep (last dense_rank command by JJ)

    , MAX (cc) keep (last dense_rank command by JJ)

    MAX (dd)

    OF THE TMP

    GROUP BY aa

    ORDER BY aa

    Note that by default sort order is ascending with the last NULL values.

    If in case your line-date-max will contain null value in the bb or cc columns those will be returned.

    Post edited by: Correction of chris227

    Post edited by: chris227 added note

  • How to write a SQL query?

    The employee list details empno, ename, job, DOB, retire_date. I want to
    Retirement date = dbo + 58 years... DOB display format 'dd-mon-yyyy' and retire_date 'MON-yyyy ".

    Published by: 1008586 on June 5, 2013 20:26

    Published by: 1008586 on June 5, 2013 20:30
    SELECT EMP_ID,
           emp_name,
           emp_dob,
           TO_CHAR (ADD_MONTHS (emp_dob, 58 * 12), 'MON-YYYY') emp_rtr_dt
      FROM employee;
    

    See you soon,.
    Manik.

  • How to achieve this using the sql query?

    Hello gurus,

    I have a table like this
    id    name
    1       a
    2       b
    3       c
    4       d
    5       e
    6       f
    7       g
    8       h
    9       i
    10     j
    11     k
    12     l
    13     m
    now my result should be like this
    id    name  id   name   id   name 
    1       a     6       f      11     k
    2       b     7       g     12     l
    3       c     8       h     13     m
    4       d     9       i
    5       e     10      j
    How to achieve by sql query?

    Thank you and best regards,
    friend

    Edited by: most wanted! February 22, 2012 05:55

    Hello

    Did you mean this:

    with a as
    (select 1 id ,'a' name from dual
    union all select 2 id ,'b' name from dual
    union all select 3 id ,'c' name from dual
    union all select 4 id ,'d' name from dual
    union all select 5 id ,'e' name from dual
    union all select 6 id ,'f' name from dual
    union all select 7 id ,'g' name from dual
    union all select 8 id ,'h' name from dual
    union all select 9 id ,'i' name from dual
    union all select 10 id ,'j' name from dual
    union all select 11 id ,'k' name from dual
    union all select 12 id ,'l' name from dual
    union all select 13 id ,'m' name from dual
    )
    
    select
      id_1
      ,name_1
      ,id_2
      ,name_2
      ,id_3
      ,name_3
    
    from
      (
      select
        id id_1
        ,name name_1
        ,lead(id,5) over (order by id) id_2
        ,lead(name,5) over (order by id) name_2
        ,lead(id,10) over (order by id)  id_3
        ,lead(name,10) over (order by id) name_3
        ,rownum r
      from
        a
      )
    where
      r <=5
    
    D_1                   NAME_1 ID_2                   NAME_2 ID_3                   NAME_3
    ---------------------- ------ ---------------------- ------ ---------------------- ------
    1                      a      6                      f      11                     k
    2                      b      7                      g      12                     l
    3                      c      8                      h      13                     m
    4                      d      9                      i
    5                      e      10                     j 
    

    Kind regards

    Peter

  • How to use the multiple selection list values in sql query

    Hi all
    In the search form, I have a multiselect llist tell (P3_STATUS) and I want to have a query using this element and fetch documents
    How to do a sql query based on the selection of the value of this element.
    SELECT "W"."START_DT" "Start Date",
           "W"."CAMPAIGN_CODE" "Campaign Name",  
           "W"."MKT_CHANNEL" "Channel",
           "W"."MKT_SUB_CHANNEL" "Sub Channel", 
           "W"."PROMO_CODE" "Promo Code",
           "W"."TRACKING_CODE" "Tracking Code",
           "W"."TFN" "TFN",
           "W"."STATUS" "Status",
           "W"."CAMPAIGN_CODE" "Edit"
           FROM 
             "WC_MKT_CAMPAIGN_DS" "W"
      
           WHERE 
         (MKT_CHANNEL = decode(:P1_CHANNEL,'%null%',MKT_CHANNEL,NULL,MKT_CHANNEL,:P1_CHANNEL))
       AND
    (MKT_SUB_CHANNEL= decode(:P1_SUB_CHANNEL,'%null%',MKT_SUB_CHANNEL,NULL,MKT_SUB_CHANNEL,:P1_SUB_CHANNEL))
    AND *STATUS = decode(.................*
    Please could someone help me on this?

    Thanks in advance
    Robert L

    Try to change your selection of status to

    AND INSTR(':'||:P3_STATUS||':',':'||STATUS||':') > 0
    
  • SQL query for retrieving data based on Certain model

    Hi all

    I want to retrieve all the identifiers of all the people who are permanently seated during the last hour.

    Data are expressed as below:

    -Creation of the activity Table

    CREATE TABLE activity_log

    (

    Username, NUMBER of

    Activity VARCHAR2 (30),

    StartTime VARCHAR2 (6).

    EndTime VARCHAR2 (6)

    );

    -Filling with sample data

    INSERT INTO activity_log VALUES('39','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('39','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('39','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('39','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('39','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('39','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('39','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('39','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('39','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('39','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('39','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('39','Sitting','09:56','10:00');

    INSERT INTO activity_log VALUES('39','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('39','Sitting','10:06','10:10');

    INSERT INTO activity_log VALUES('39','Sitting','10:11','10:15');

    INSERT INTO activity_log VALUES('39','Sitting','10:16','10:20');

    INSERT INTO activity_log VALUES('39','Sitting','10:21','10:25');

    INSERT INTO activity_log VALUES('39','Sitting','10:26','10:30');

    INSERT INTO activity_log VALUES('39','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('39','Standing','10:36','10:40');

    INSERT INTO activity_log VALUES('39','Standing','10:41','10:45');

    INSERT INTO activity_log VALUES('39','Walking','10:46','10:50');

    INSERT INTO activity_log VALUES('39','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('39','Walking','10:56','11:00');

    INSERT INTO activity_log VALUES('40','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('40','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('40','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('40','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('40','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('40','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('40','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('40','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('40','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('40','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('40','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('40','Walking','09:56','10:00');

    INSERT INTO activity_log VALUES('40','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('40','Standing','10:06','10:10');

    INSERT INTO activity_log VALUES('40','Standing','10:11','10:15');

    INSERT INTO activity_log VALUES('40','Walking','10:16','10:20');

    INSERT INTO activity_log VALUES('40','Walking','10:21','10:25');

    INSERT INTO activity_log VALUES('40','Walking','10:26','10:30');

    INSERT INTO activity_log VALUES('40','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('40','Sitting','10:36','10:40');

    INSERT INTO activity_log VALUES('40','Sitting','10:41','10:45');

    INSERT INTO activity_log VALUES('40','Standing','10:46','10:50');

    INSERT INTO activity_log VALUES('40','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('40','Walking','10:56','11:00');

    Based on the data, the user ID 39 must be found, since it's been sitting since 09:36-10:35, which is continuous 1 hour.

    Any guidance how to do using SQL query will be of great help and appreciation.

    Thank you very much

    Kind regards

    Bilal

    So what exactly is wrong with the request that I already gave you?

    Just to remind one untested (because of lack of insert statements) rewrite according to your new data:

    with grp as)

    Select

    username

    UserRecognizedActivityID activity

    starttime

    starttime + endetime + 1

    row_number() over (partition by order of starttime userid)

    -ROW_NUMBER() over (partition of userid, UserRecognizedActivityID order of starttime)

    RN

    of activity_log

    )

    Select

    username

    min (starttime) starttime

    max (endtime) endtime

    max (activity) activity

    GRP

    Group userid, rn

    with round (max (endtime) - min (starttime) * 24 * 60) > = 59

  • Substring of SQL query

    Hello

    In my table, I have the name used like this

    EX:
    BOSE, MR. BIRAT
    NANDY, MR. AMITABHA

    The name before the colon is the name of family and after it's first name

    Now, I want to show in the front-end server like this for both above

    MR. BOSE BIRAT
    MR. AMITABHA NANDY


    How to write a Sql query, where you can get the name before the decimal point and place at the end of the name and remove the comma from also for display purpose.

    Thank you

    Something like that?

    WITH t AS
    (SELECT 'BOSE, Mr. BIRAT' as n FROM DUAL
     UNION ALL
     SELECT 'NANDY, Mr. AMITABHA' FROM dual
    )
    SELECT n,
           regexp_replace(n, '([^,]+), (.+)*', '\2 \1')
      FROM t;
    

Maybe you are looking for

  • Satellite A660 - 1 H 5 and crack chips Sandy

    Hello I'm new to this forum. I recently bought a 1 H 5 A660. The thing within a week of purchase that I start to get this blue screen when I turn on the computer. It is about 50% of the time. Then I went to the shop where I buy the machine and discov

  • HELP "ARD" (Apple Remote Desktop)

    If you use apple remote desktop, you will have to enter the username and password for the computer you are trying to connect to? And can connect to windows computers? I'm getting a mac soon and I wanted to know this answer.

  • Pavilion 15-n259se: upgrade to ssd Amp (power consumption) or

    Hi allI have 15-n259se flag which comes with 500 GB seagate with a consumption of 5 v, Amp.451I bought a 1 TB SSD which have 5V power consumption and 1.5 Amp it will propply function is or is notNOTE will have 1 SSD has more than normal HARD driveSRY

  • Beyond Amazon S3 Iomega ix2 and Mozy backup solutions

    Hello I see that Mozy is now discontinued in the Iomega ix2 NAS. I can't use Amazon S3 for philosophical reasons. Are there other available for off-site backup solutions? I tried to learn more about EMC Atmos, but I can't find a service provider that

  • Cannot download Windows 8.1 update due to unrecognized BIOS version

    Hello I have a SVD13217PAB - aka - Sony Duo 13... I'm trying to download the 8.1 windows update of windows store but I am facing the following problems in chronological order: (1) after the median point in the update download long journey, the comput