Split dates into different shifts.

Hello all,
I'm trying to setup a query to get my date ranges into 8 hour shifts which start at 23:00, 07:00, and 15:00.
create table t (start_date, end_date)
insert into t values (to_date('11-21-12 21:02:15', 'MM-dd-yy HH24:MI:ss'), to_date('11-21-12 21:02:51', 'MM-dd-yy HH24:MI:ss'));
insert into t values (to_date('11-21-12 21:09:06', 'MM-dd-yy HH24:MI:ss'), to_date('11-24-12 23:30:18', 'MM-dd-yy HH24:MI:ss'));
insert into t values (to_date('11-24-12 23:35:28', 'MM-dd-yy HH24:MI:ss'), to_date('11-24-12 23:49:34', 'MM-dd-yy HH24:MI:ss'));Desired output:
START_DATE        | END_DATE
11-24-12 23:35:28 | 11-24-12 23:49:34
11-24-12 23:00:00 | 11-24-12 23:30:18
11-24-12 15:00:00 | 11-24-12 23:00:00
11-24-12 07:00:00 | 11-24-12 15:00:00
11-23-12 23:00:00 | 11-24-12 07:00:00
11-23-12 15:00:00 | 11-23-12 23:00:00
11-23-12 07:00:00 | 11-23-12 15:00:00
11-22-12 23:00:00 | 11-23-12 07:00:00
11-22-12 15:00:00 | 11-22-12 23:00:00
11-22-12 07:00:00 | 11-22-12 15:00:00
11-21-12 23:00:00 | 11-22-12 07:00:00
11-21-12 21:09:06 | 11-21-12 23:00:00
11-21-12 21:02:15 | 11-21-12 21:02:51Version:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
Is this possible?
Thanks in advance!

Hi,
Tarianz wrote:
... How would I go about adding another column into this?
I've been messing around with the query trying to add it myself but have been unsuccessful.
Similar to:
*ID is the order the dates were inserted into the table.
ID| START_DATE        | END_DATE
3| 11-24-12 23:35:28 | 11-24-12 23:49:34
2| 11-24-12 23:00:00 | 11-24-12 23:30:18
2| 11-24-12 15:00:00 | 11-24-12 23:00:00 ...
That sounds like a job for the analytic ROW_NUMBER function.
ROW_NUMBER () OVER (ORDER BY x, y, z)     AS t_id   -- id is an Oracle keywordwhere x, y and z are the columns or expressions that indicate the order you want. You can have any number of such expressions (1 or more), and you can use <b>DESC</b>ending order if you want to.
If the combination (x, y, z) is not unique, that is, if 2 or more rows were added at the same time, then ROW_NUMBER will give them distinct numbers, but there's no telling which number will be lower.
You need to do this when first using table t, before the rows are split into shifts. In the query I suggested, you could add the expression above in sub-query got_shift_num.
So if the rows were INSERTed into the table in the same order as start_date, then you could do this:
WITH     got_base_date     AS
     SELECT     TRUNC (SYSDATE) + (7 / 24)     AS base_date
     FROM     dual
,     got_shift_num     AS
     SELECT     t.start_date, t.end_date
     ,     b.base_date
     ,     FLOOR ( (t.start_date - b.base_date)
                * 3
                )          AS start_shift_num
     ,     FLOOR ( (t.end_date - b.base_date)
                * 3
                )          AS end_shift_num
     ,     ROW_NUMBER () OVER (ORDER BY t.start_date)     -- ***** NEW *****
                       AS t_id                         -- ***** NEW *****
     FROM    got_base_date  b
     CROSS JOIN            t
,     cntr     AS
     SELECT  LEVEL - 1     AS n
     FROM     (
              SELECT  MAX ( end_shift_num
                            - start_shift_num
                    )     AS max_shift_cnt
              FROM    got_shift_num
     CONNECT BY     LEVEL <= 1 + max_shift_cnt
SELECT       s.t_id                              -- ***** NEW *****
,       GREATEST ( s.start_date
               , s.base_date + ( ( s.start_shift_num
                              + c.n
                       / 3
             )     AS shift_start_date
,       LEAST    ( s.end_date
               , s.base_date + ( ( s.start_shift_num
                              + c.n
                         + 1
                       / 3
             )     AS end_start_date
,       start_date, end_date          -- For debugging
FROM       got_shift_num  s
JOIN       cntr           c  ON c.n  <= s.end_shift_num - s.start_shift_num
ORDER BY  shift_start_date  DESC
;This is exactly what I posted yesterday, only with 3 lines, marked "***** NEW *****", added.
You could modify Solomon's suggestion, using ROW_NUMBER in sub-query a.

Similar Messages

  • Split data into different fields in TR

    I have a flat file with space (multiple spaces between different fields) as a delimiter. The problem is, file is coming from 3rd party and they don't want to change the separator as comma or tab delimited CSV file. I have to load data in ODS (BW 3x).
    Now I am thinking to load line by line and then split data into different objects in Transfer rules.
    The Records looks like:
    *009785499 ssss BC sssss 2988 ssss 244 sss 772 sss  200
    *000000033 ssss AB ssss        0  ssss   0 ssss 0 ssss 0
    *000004533 ssss EE ssss        8  ssss   3 ssss 2 ssss 4
    s = space
    Now I want data to split like:
    Field1 = 009785499
    Field2 = BC
    Field3 = 2988
    Field4 = 244
    Field5 = 772
    Field6 = 200
    After 1st line load, go to 2nd line and split the data as above and so on. Could you help me with the code pleaseu2026?
    Is it a good design to load data? Any other idea?
    I appreciate your helps..

    Hi,
    Not sure how efficient this is, but you can try an approach on the lines of this link /people/sap.user72/blog/2006/05/27/long-texts-in-sap-bw-modeling
    Make your transfer structure in this format. Say the length of each line is 200 characters. Make the first field of the structure of length 200. That is, the length of Field1 in the Trans Struc will be 200.
    The second field can be the length of Field2 as you need in your ODS, and similarly for Field3 to Field6. Load it as a CSV file. Since there are no commas, the entire line will enter into the first field of the Trans Structure. This can be broken up into individual fields in the Transfer Rules.
    Now, in your Start Routine of transfer rules, write code like this (similar to the ex in the blog):
    Field-symbols <fs> type transfer-structure.
    Loop at Datapak assigning <fs>.
        split <fs>-Field1 at 'ssss' into <fs>-field1 <fs>-field2 <fs>-field3....
        modify datapak from <fs>
    endloop.
    Now you can assign Field1 of Trans Struc to Field1 of Comm Struc, Field2 of Trans Struc to Field2 of Comm Struc and so on.
    Hope it helps!
    Edited by: Suhas Karnik on Jun 17, 2008 10:28 PM

  • Export transnational data into different client

    Dear All Expertise's,
    For our Management Audit purpose we have needed urgently copy all FI transnational data into different client in particular year.
    As an example:
    Source Client:   888
    Target client: 700
    Company: 1000
    Year: 2013
    FI Documents type: All
    Please help me to get the solution use form your great advice.
    SCC8/SCC7 can give us copy all records client to client. But we need only specific company/periods.
    Thanks and Best regards
    Bishnu
    05.05

    I post the VI. Thanks!
    Attachments:
    j k thermocouple.vi ‏151 KB

  • SegregatingTable data into different bucket with equal numebr of

    Hi Guys,
    I wanted to process table data in chunks not all the rows at a time. How can we achieve this in oracle.
    Example :
    I have one table EMP which has ten thousands rows(10,000) rows. Now these ten thousands rows are joined with other tables to process data and it takes time. So I wanted to pass one thousands rows at a time as an input to the procedures so that processing should happen only with 1 thousands rows at a time. This table does not have any primary key. So it there any method in oracle using that I can segregating the tables data into different buckets with equal number of rows.
    I have used DBMS_PARALLEL_EXECUTE but its taking lots of time. Any help will be appreciated.

    I have one table EMP which has ten thousands rows(10,000) rows. Now these ten thousands rows are joined with other tables to process data and it takes time.
    OK... So this is your actual problem. And the solution you are trying to come up with does not sound promising. So lets take a step back and re-think our strategy here. First thing would be how about we see some code? Show us your code that you say is running slow. And we can take it from there. The number 10,000 is a very small number. And if that is causing performance issue then you are in some big trouble.
    And also it wouldn't hurt if you could read this Re: 3. How to  improve the performance of my query? / My query is running slow.

  • How to display rows of data into different columns?

    I'm new to SQL and currently this is what I'm trying to do: 
    Display multiple rows of data into different columns within the same row
    I have a table like this:
        CREATE TABLE TRIPLEG(
            T#              NUMBER(10)      NOT NULL,
            LEG#            NUMBER(2)       NOT NULL,
            DEPARTURE       VARCHAR(30)     NOT NULL,
            DESTINATION     VARCHAR(30)     NOT NULL,
            CONSTRAINT TRIPLEG_PKEY PRIMARY KEY (T#, LEG#),
            CONSTRAINT TRIPLEG_UNIQUE UNIQUE(T#, DEPARTURE, DESTINATION),
            CONSTRAINT TRIPLEG_FKEY1 FOREIGN KEY (T#) REFERENCES TRIP(T#) );
        INSERT INTO TRIPLEG VALUES( 1, 1, 'Sydney', 'Melbourne');
        INSERT INTO TRIPLEG VALUES( 1, 2, 'Melbourne', 'Adelaide');
    The result should be something like this:
    > T#  | ORIGIN  | DESTINATION1  |  DESTINATION2 
    > 1   | SYDNEY  | MELBORUNE     | ADELAIDE
    The query should include the `COUNT(T#) < 3` since I only need to display the records less than 3. How can I achieve the results that I want using relational views???
    Thanks!!!

    T#
    LEG#
    DEPARTURE
    DESTINATION
    1
    1
    Sydney
    Melbourne
    1
    2
    Melbourne
    Adelaide
    1
    3
    Adelaide
    India
    1
    4
    India
    Dubai
    2
    1
    India
    UAE
    2
    2
    UAE
    Germany
    2
    3
    Germany
    USA
    On 11gr2, you may use this :
      SELECT t#,
             REGEXP_REPLACE (
                LISTAGG (departure || '->' || destination, ' ')
                   WITHIN GROUP (ORDER BY t#, leg#),
                '([^ ]+) \1+',
                '\1')
        FROM tripleg
        where leg#<=3
    GROUP BY t#;
    Output:
    1 Sydney->Melbourne->Adelaide->India
    2 India->UAE->Germany->USA
    Cheers,
    Manik.

  • Can we send the data into different data target from single datasource how

    Hai
    can we send the data into different data target from single datasource how ?

    Hi,
    Create the transformation for each target and connect through DTP if you are in BI 7.0
    If it is BW 3.5 create transfer rules and load it Info source and then different update rules for different targets then load it using IP.
    If you are speaking about loading data from R3 data source to multiple data sources in BI
    Then follow the below step.
    1)create init IP's and run the IP's with different selection to different DSO(With same selection it is not possible).
    2)Then you will have different delta queues for the same data source in RSA7
    3)Your delta loads will run fine to different data sources from same DS in R3.
    Hope this helps
    Regards,
    Venkatesh

  • HT1349 why when i import a comperlation cd into my itunes library  it splits it into different parts

    hello when i import a cd in to my itunes  library from my collection  it spliys it into differant albums ect   acample my epic cd x3  is split into about 20 alboms

    You either put in a blank CD as roaminggnome suggests, or something mechanical went wrong with your player, damaging the CD.  Check for scratches.
    iTunes has no facility for erasing a CD.  You would have to deliberately do that with a disk or DC burning utility.

  • Split data of 1st shift and 2nd shift

    Hi,
    I have an SSRS report that displayed the production data. It consist of data from 1st-Shift and 2nd-Shift . My SSRS has a parameter with FromDate and Todate, Time. the default date is today's date. My requirements is to
    obtain the data of 1st shift and 2nd shift on the same day and displayed into one report with separate column for 1st shift and 2ns shift. The 1st shift is always start at 6:00AM and end at 2:00PM while the 2nd shift start at 2:00PM and end at 10:00PM
    Btw, This report is run every hour. THe objective is to gather how the production perform.
    Any help is very much appreciated. Thanks
    Sample DDL
    Create table #sample
    (ID nvarchar(5), DateandTime datetime, qty int)
    Insert into #sample(ID, dateandtime,qty) values('XXXX','2014-05-30 00:33:18.000',1)
    Insert into #sample(ID, dateandtime,qty) values('TTTT','2014-05-30 05:20:57.000',1)
    Insert into #sample(ID, dateandtime,qty) values('XXXX','2014-05-29 23:09:33.000',1)
    Insert into #sample(ID, dateandtime,qty) values('XXXX','2014-05-30 03:45:08.000',1)
    Insert into #sample(ID, dateandtime,qty) values('TTTT','2014-05-30 04:53:55.000',1)
    Insert into #sample(ID, dateandtime,qty) values('XXXX','2014-05-30 06:00:06.000',1)
    Insert into #sample(ID, dateandtime,qty) values('XXXX','2014-05-30 06:15:41.000',1)
    Insert into #sample(ID, dateandtime,qty) values('TTTT','2014-05-30 06:07:08.000',1)
    Declare @timezoneOffset int, @D1 datetime, @D2 datetime, @Stime nvarchar(8), @ETime nvarchar(8),@FromDatetime datetime, @ToDatetime datetime
    Set @timezoneOffset=8
    Set @D1='2014-05-30'
    Set @D2='2014-05-30'
    set @Stime='06:00 AM'
    set @Etime='02:00 PM'
    Set @FromDatetime= @D1 +''+@Stime
    Set @ToDatetime= @D2 +''+@Etime
    SELECT *
    FROM #sample
    WHERE DATEADD(HOUR,convert(int,@timezoneOffset), dateandtime) BETWEEN (@Fromdatetime) AND (@ToDatetime)

    May I ask you assistance to fix this error.
    i wanted this code to have a timezoneoffset. 
    Also , i would like to ask if this is acceptable  in between  6:00 and 16:00, if more than 16:00 (this is 4:00PM) this would be now under shift 2. 
    case when DATEPART(hour,DateandTime)
    between 6 and
    16 then 1
    else 2 end
    as shiftID,
    I tried trid this but got an error:
    CASE WHEN DATEADD(HOUR,convert(int,@timezoneOffset), DateandTime)
    between 6 and
    16 then 1
    else 2 end
    as shiftID2

  • Split dates into date range windows

    Hi,
    I have a peculiar requirement to create windows for different dates.
    For example I have two date ranges (two rows in the table column
    Start Date End Date
    01-Jan 28-Feb
    15-Jan 14-Feb (this starts between the previous date range i.e. 01-Jan to 28-Feb)
    I have to split dates in a way so that it creates different windows with start and end date i.e.
    Start Date End Date
    01-Jan 15-Jan (15Jan because it comes before 28 Feb)
    16-Jan 14-Feb ( 14Feb as it comes before 28 Feb)
    15-Feb 28-Feb
    Is there any sql function which can be used to achieve this? or any help you can provide? Thanks.
    -Salman

    Welcome to the forum!
    Unfortunately you posted to the wrong forum. This forum is for sql developer only.
    Repost you question in the SQL and PL/SQL forum and you will get help there.
    Please mark this question ANSWERED to people will follow up in the other forum.

  • How to break range of dates into different months in mm/yyyy format

    See the following eg:
    MaterialNo         Validfrom         ValidTo            RecordNo                    Value
    A100                01.01.2007        13.05.2007       1000                           1150/-
                             14.05.2007       24.08.2007        1001                           1550/-
    Now I want to break these dates into mm/yyyy format based on a condition. If the date(day) 15th lies in the range of dates
    then it should pick that Value.
    The output should be in this format.
    MaterialNo         Validfrom         ValidTo                    Date                         RecordNo                    Value
    A100                01.01.2007        13.05.2007            012007                            1000                           1150/-
                                                                                    022007                            1000                           1150/-
                                                                                    032007                            1000                           1150/-
                                                                                    042007                            1000                           1150/-
    A100               14.05.2007       24.08.2007            052007                               1001                           1550/-
                                                                                    062007                               1001                           1550/-
                                                                                    072007                               1001                           1550/-
                                                                                    082007                               1001                           1550/-

    Hi Shiva,
    In your Report Program, you can define the field of type sy-datum. And then fill the fields with the date. And then you can extract the rest of it manually by this small and simple lines of code. Once you have the date extracting logic then you can output the date in any manner according to your wish.
    data : lv_date  TYPE sy-datum.
    data : lv_month TYPE c LENGTH 2.
    data : lv_year  TYPE c LENGTH 4.
    data : lv_month_year TYPE c LENGTH 6.
    lv_date = sy-datum.
    lv_month = lv_date+4(2).
    lv_year  = lv_date+0(4).
    CONCATENATE lv_month lv_year INTO lv_month_year.
    Hope this helps.
    Thanks,
    Samantak.

  • Split data to different table cell

    Hey, there.
    Does anyone knows how can i split data in table?
    My table is
    1480x2140
    870x1660
    1580x2360
    And i need
    1480
    2140
    870
    1660
    1580
    2360

    In 3 steps:
    1. Convert table to text
    2. Replace x with tab
    3. Convert text to table with tab as column separator
    Uwe

  • Regarding transfering the data into different cubes

    Hi all,
    The relation between infosource and cube is one to many.So by applying update rules we can transfer the the data from infosource to different cubes.So please explain a scenario where in same data is stored in different cubes
    thanks in advance
    regards
    karna

    Hi
    First you wont get any scenario where you have to store the same data in different cubes. There is no point of storing same data in multiple cubes(Wasting DB space) . The one to many scenario could be applied as USERPV said, you have data from a single source system  which contains multiple country sales info. Now you want to divide this data based on countries and store in respective cubes, you have to write ABAP code in transfer rule to do this. You can report individual cubes or you can  report on all cubes once using multiprovider.
    Hope this will clear your doubt.
    regards
    vin

  • Split period into different date ranges

    Hi,
    Can someone let me know if there is any standard Function module that can split up the periods? For example I have a total period 01.01.2008 to 31.12.2008 and I have a split period of 01.04.2008 to 30.04.2008.
    Now my resultant period should be,
    1. 01.01.2008 to 31.03.2008
    2. 01.04.2008 to 30.04.2008
    3. 01.05.2008 to 31.12.2008
    Thanks,
    Prasath N

    Are there no programmers around your site?  This really is tremendously basic.
    PARAMETERS: p_dat TYPE d.
    CONSTANTS: c_start_of_all_periods TYPE d VALUE '20080101',
               c_end_of_all_periods   TYPE d VALUE '20081231'.
    DATA: start_of_first_period TYPE d,
          end_of_first_period   TYPE d,
          start_of_split_period TYPE d,
          end_of_split_period   TYPE d,
          start_of_2nd_period   TYPE d,
          end_of_2nd_period     TYPE d.
    IF p_dat LT c_start_of_all_periods OR p_dat GT c_end_of_all_periods.
      WRITE: / 'Outside range'.
      EXIT.
    ENDIF.
    start_of_split_period = p_dat.
    start_of_first_period = c_start_of_all_periods.
    end_of_2nd_period = c_end_of_all_periods.
    * Get first day of supplied month
    start_of_split_period+6(2) = '01'.
    * Get day before the first day of supplied month
    end_of_first_period = start_of_split_period - 1.
    start_of_2nd_period = start_of_split_period.
    start_of_2nd_period+6(2) = '28'. " Get 28th of supplied month
    ADD 4 TO start_of_2nd_period. " Get 4 days later - this will be in next month
    start_of_2nd_period+6(2) = '01'. " Get first day of next month
    end_of_split_period = start_of_2nd_period - 1. " Get day before start of 2nd period
    WRITE: / start_of_first_period,
             end_of_first_period,
             start_of_split_period,
             end_of_split_period,
             start_of_2nd_period,
             end_of_2nd_period.
    If you are a programmer, I hope this sample is sufficient to show how to deal with all kinds of date handling coding problems.
    matt

  • How can split data into multiple sheets using an excel template

    Hi all,
    I'm using BIP 10.1.3.4.1 upgrated with the latest rollup patch, 9546699.
    I followed the example inside the article "Real Excel Templates 1.5" (on the Tim Dexter's blog)
    http://blogs.oracle.com/xmlpublisher/2010/05/real_excel_templates_i.html
    and I built my report directly from an excel template containing only one sheet (plus the XDO_METADATA sheet), and for one sheet it worked fine !!!
    Now I need to add more sheets to the template.... and I have two big questions:
    1. if I need to create a second sheet which contains almost the same data (with a different layout), I saw that it is not possible to map a value to more than 1 cell....so if I map a value in the first sheet, I can't map it in the second sheet (because it's already used in the first one).
    How can I map same values into two or different execll
    2. if I need to create a second sheet which contains another dataset (completely different from the dataset in the first sheet), I can I do, considering that every BIP report is based on ONLY a data source.
    Tim wrote: The most impressive, for me at least, is the sheet 'bursting'. You can split your hierarchical data across multiple sheets and dynamically name those sheets
    but I didn't yet understand how this bursting it's possible.....considering what I said above.....
    Surely I've a lack of knowledge.....but anybody can help me to understanding the "multiple excel sheets" black hole ?
    I'll appreciate any kind of help
    Thanks in advance
    Alex

    You can find working solution here http://xlspe.com

  • Splitting date into year and month

    Hi,
       I am getting it_final-bedat (dd.mm.yyyy).I need to split month as well as year in separate fields
    in the internal table.tell some ideas.

    hi badri , try this program , hope this is u r requirement
    data : BEGIN OF itab OCCURS 0 ,
           pernr like  pa0001-pernr,
           begda like  pa0002-begda,
          END OF itab .
    data : BEGIN OF itab2 OCCURS 0 ,
           pernr like  pa0001-pernr,
           d_date type c LENGTH 2  ,
           d_month type c length 2,
           d_year  type c length 4 ,
          END OF itab2 .
    select pernr begda from pa0001 into CORRESPONDING FIELDS OF TABLE itab WHERE pernr eq 1000.
    loop at itab WHERE pernr eq 1000 .
    clear itab2[].
        itab2-pernr = itab-pernr.
        itab2-d_year = itab-begda+0(4).
        itab2-d_month = itab-begda+4(2).
        itab2-d_date = itab-begda+6(2).
        append itab2.
        write :/ itab2-pernr,
                 itab2-d_date,
                 itab2-d_month,
                 itab2-d_year.
        endloop .
    regards
    chinnaiya

Maybe you are looking for

  • SAP MM Functional Developer ~ Dublin OH

    Start Date: IMMEDIATELY! Positions Available: 1 Employee Type: Contractor - 1099 Hrs/Wk.:  40 Hrs. Duration: 9 Months Pay Rate:  $80 - $88/Hr. DOE Work Status: Green Card or US Citizen Highly Desirable H1B Sponsorship is not available at this time. C

  • How can I overlap two clips in final cut

      I am shooting a music video and I want the actor to appear in two spots in the same shot. I shot some test footage today and I set the camera in one spot for both shots. Shot 1 I told him to stand on the right side of the frame for awhile, cut it a

  • How to Activate T.code LBK1

    Hi guru's, While opening SAP Std T.code LBK1 , systerm is giving error ' transaction doesnot exists ', I know there are some badis we need to implement, but need some help regarding implementation. I want to activate this transaction..for log book en

  • How do I auto-calculate the number of appearances of a particular value in multiple fields?

    Hi folks!  Some assistance with this matter would be greatly appreciated. I have a form where I need to have calculated the number of appearances of certain values in a dropdown list.  The total number of each would then be calculated and displayed i

  • Cross-Platform Issues?

    I am using contribute on a PC (I'm the administrator) and another office wants a copy of the software but they use a Mac. Am I going to have any issues with this? Thanks.