Split a query in date-ranges

DB version 10g
a sample table is
my tab
(dt date,
status varchar2(2))
status in ('M','L','H') (medium,low,high)
sample data are: (date in 'dd-mm-yyy hh24Mi:ss' format)
DT STATUS
02-03-2006 08:16:00 H
02-03-2006 08:20:11 H
02-03-2006 08:22:00 H
02-03-2006 08:23:22 H
02-03-2006 08:24:33 M
02-03-2006 08:25:44 H
02-03-2006 08:26:55 L
02-03-2006 14:27:59 L
03-03-2006 00:00:00 L
I'm interested in find duration for every status:
status H
from 02-03-2006 08:16:00 to 02-03-2006 08:24:33 => duration 8 min 33 secs;
from 02-03-2006 08:25:44 to 02-03-2006 08:26:55 => duration 1 min 11 secs;
status M
from 02-03-2006 08:24:33 to 02-03-2006 08:26:55 => duration 1 min 11 secs;
status L
from 02-03-2006 08:26:55 to 03-03-2006 00:00:00 => duration 15 h ....
there are a complex query for this purpose without using a procedure that scan all records to find the chenges of status?

Not very elegant:
SQL> select * from my_tab;
DT                  ST
02-03-2006 08:16:00 H
02-03-2006 08:20:11 H
02-03-2006 08:22:00 H
02-03-2006 08:23:22 H
02-03-2006 08:24:33 M
02-03-2006 08:25:44 H
02-03-2006 08:26:55 L
02-03-2006 14:27:59 L
8 rows selected.
SQL> select status, min(dt), max(ld) from (
  2  select status, dt,
  3  case when lead(dt) over(order by dt) is null then
  4  trunc(dt) + 1 else lead(dt) over(order by dt) end ld,
  5  (row_number() over(order by dt) -
  6  row_number() over(partition by status order by dt)) gr
  7  from my_tab
  8  ) group by status, gr
  9  /
ST MIN(DT)             MAX(LD)
H  02-03-2006 08:16:00 02-03-2006 08:24:33
H  02-03-2006 08:25:44 02-03-2006 08:26:55
L  02-03-2006 08:26:55 03-03-2006 00:00:00
M  02-03-2006 08:24:33 02-03-2006 08:25:44Probably a mistake in M example - if I interpret your question right,
M has to be from 02-03-2006 08:24:33 to the next "H" e.g. 2006 08:25:44 ?
Rgds.

Similar Messages

  • Querying on date range issue?

    Hi,
    I'm looking for a solution for this issue,
    In the DB Table there is a column of type TIMESTAMP. so when i query for date range using BETWEEN keyword it returns data inserted on next day at 12.00.00 AM
    example :
    Table A
    ColumnA_
    data1
    data2
    data3
    ColumnTime_
    08/03/2010 11.55.00 AM
    08/04/2010 12.00.00 AM
    08/04/2010 12.00.01 AM
    here when I query using between (08/03/2010 and 08/04/2010) keyword on Column_Time then ,
    Ouput
    data1
    data2
    but I'm just looking for data1 in the output, any ideas?

    Between is inclusive of both boundaries:
    column between condition1 and condition2Translates into
    condition1 >= column
    and
    condition2 <= columnSo tell the code what you want ... in your case
    condition1 >= column
    and
    condition2 < columnI'm not sure if you expect the >= or not...adjust as needed.

  • SQL Query on Date Range

    Hi, I am using Oracle 10g. I want a query which gives me below output.
    Data Setup
    ========
    create table t_date_range (
    ID     number (2),
    start_date     date,
    end_date     date);
    insert into t_date_range values (1,to_date('20110101', 'YYYYMMDD'),to_date('20110331', 'YYYYMMDD'));
    insert into t_date_range values (2,to_date('20110401', 'YYYYMMDD'),to_date('20110531', 'YYYYMMDD'));
    insert into t_date_range values (3,to_date('20110701', 'YYYYMMDD'),to_date('20110731', 'YYYYMMDD'));
    insert into t_date_range values (4,to_date('20110901', 'YYYYMMDD'),to_date('20111130', 'YYYYMMDD'));
    insert into t_date_range values (5,to_date('20111201', 'YYYYMMDD'),to_date('20111231', 'YYYYMMDD'));
    commit;
    SQL> select ID, to_char(start_date,'DD-MON-YYYY') START_DATE, to_char(end_date,'DD-MON-YYYY') END_DATE from t_date_range;
    ID START_DATE END_DATE
    1 01-JAN-2011 31-MAR-2011
    2 01-APR-2011 31-MAY-2011
    3 01-JUL-2011 31-JUL-2011
    4 01-SEP-2011 25-OCT-2011
    5 26-OCT-2011 30-NOV-2011
    6 01-DEC-2011 31-DEC-2011
    6 rows selected.
    I want result in this form:
    START_DATE END_DATE
    01-JAN-2011 31-MAY-2011
    01-JUL-2011 31-JUL-2011
    01-SEP-2011 31-DEC-2011
    Means if there is a difference of exact one day between "start_date of 2nd row" and "end_date of first row" then make a single row as shows in above results set.
    Thanks!

    Hi,
    Solomon Yakobson wrote:
    Keep in mind, Franks's solution assumes date ranges do not overlap. ...That's true. If rows can overlap, then we might need to use two sub-queries, like you did: one to see if a new group starts with this row, and another to count how many new groups have already started.
    The solution you posted assumes a relationship between id and dates. If we add a row like this to the sample data:
    insert into t_date_range values (6,to_date('20101201', 'YYYYMMDD'),to_date('20121231', 'YYYYMMDD'));that overlaps all the others, then how would that solution work?
    LAG won't work, because no matter how we sort the rows, we can't be sure that overlapping rows will be consecutive.
    Here's one way to handle overlapping rows:
    WITH     got_new_grp          AS
         SELECT     id, start_date, end_date
         ,     CASE
                  WHEN  start_date > 1 + MAX (end_date) OVER ( ORDER BY  start_date
                                                                        ,             id
                                                                        ROWS BETWEEN  UNBOUNDED PRECEDING
                                                  AND      1      PRECEDING
                  THEN  1
              END     AS new_grp
         FROM     t_date_range
    ,     got_grp          AS
         SELECT  start_date, end_date
         ,     COUNT (new_grp) OVER ( ORDER BY  start_date
                                     ,           id
                             ) AS grp
         FROM    got_new_grp
    SELECT       MIN (start_date)     AS start_date
    ,       MAX (end_date)     AS end_date
    FROM       got_grp
    GROUP BY  grp
    ORDER BY  start_date
    ;This solution uses id (assumed to be unique) just to get a consistent ordering, in case 2 (or more) rows have exactly the same start_date. It does not assume any relationship between ids and either start_date or end_date.
    I'm also assuming that start_date<=end_date on each row.
    Edited by: Frank Kulash on May 11, 2011 12:54 PM
    The above refers to the solution you oriognally posted, which was:
    with t1 as (
                select  id,
                        start_date,
                        end_date,
                        case
                          when lag(end_date) over(order by id) + 1 >= start_date then 0
                          else 1
                        end start_of_group
                  from  t_date_range
         t2 as (
                select  id,
                        start_date,
                        end_date,
                        sum(start_of_group) over(order by id) grp
                  from  t1
    select  min(start_date) start_date,
            max(end_date) end_date
      from  t2
      group by grp
      order by grp
    ;

  • CUIC Hourly Interval Query Throughout Date Range (SQL Squirrel Stuff)

    I’m trying to create a specific report in CUIC using the "Contact Service Queue Activity Report by Interval” canned report.  The objective is to query intervals (by hour), throughout the whole reporting time frame.  This can be done by interval and reported per day in the canned report, but I’m trying to get the values per interval without the day displayed.  For example, I’d like a report that shows total calls per hour (8-9 am, 9-10, 10-11am etc) throughout the reporting date range. The report would look like this:
    Interval Start Time  Interval End Time     CSQ Name     Calls Presented    Handled
    8:00:00 AM     9:00:00 AM     CSQ_     41     40
    9:00:00 AM     10:00:00 AM     CSQ_     63     60
    10:00:00 AM     11:00:00 AM     CSQ_     50     50
    Instead of this:
    Interval Start Time  Interval End Time     CSQ Name     Calls Presented    Handled
    3/3/14 8:00     3/3/14 9:00     CSQ_     16     16
    3/3/14 9:00     3/3/14 10:00     CSQ_     23     21
    3/3/14 10:00     3/3/14 11:00     CSQ_     24     24
    3/4/14 8:00     3/4/14 9:00     CSQ_     13     13
    3/4/14 9:00     3/4/14 10:00     CSQ_     26     25
    3/4/14 10:00     3/4/14 11:00     CSQ_     14     14
    3/5/14 8:00     3/5/14 9:00     CSQ_     12     11
    3/5/14 9:00     3/5/14 10:00     CSQ_     14     14
    3/5/14 10:00     3/5/14 11:00     CSQ_     12     12

    Hi
    So you want the report to add up all the calls between 9-10am, e.g. if you run it for a week it would add up the calls for each day between 9 and 10 am and show a single total for that hour?
    Aaron

  • Query regarding date range

    Hi I am using below query
    select count(*) from
    select M.Login_name
    , P.IND_PROD_LOGIN_ID
    , count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
    from CITI_USER_2.CCS_CUSTOMER_MAST M
    , CITI_USER_2.CCS_CUSTOMER_PROD P
    WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
    and to_char( M.CREATE_DATE , 'DD/MM/YYYY') = '16/10/2009'
    ) where cnt = 1
    and translate(Login_name,'x0123456789','x') is null
    and i got the result as 10 records but if i try to put in the date range as below
    select count(*) from
    select M.Login_name
    , P.IND_PROD_LOGIN_ID
    , count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
    from CITI_USER_2.CCS_CUSTOMER_MAST M
    , CITI_USER_2.CCS_CUSTOMER_PROD P
    WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
    and to_char( M.CREATE_DATE , 'DD/MM/YYYY') between '16/10/2009' and '17/10/2009'
    ) where cnt = 1
    and translate(Login_name,'x0123456789','x') is null
    i got the result as 653 records
    But it should be 10 only and when i check the records it is giving me wrong result.
    Can someone highlight how to use the range between the date, i need to check the number of records between the date range from 01/05/2009 to 01/10/2009

    this is all happening because of
    to_char( M.CREATE_DATE , 'DD/MM/YYYY') between '16/10/2009' and '17/10/2009'in your second query...
    that is doing a string comparision not date comparision...
    in string comparision
    check this example...
    select * from dual where '17/10/2009' > '16/10/2010'which will result a record but you might also observe that 17 th in 2009 is less than 16th of 2010. this is because here string comparision is ocurring.
    you need to change it as....
    M.CREATE_DATE between to_date('16/10/2009','DD/MM/YYYY') and to_date('17/10/2009','DD/MM/YYYY')so your query becomes...
    select count(*) from
    select M.Login_name
    , P.IND_PROD_LOGIN_ID
    , count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
    from CITI_USER_2.CCS_CUSTOMER_MAST M
    , CITI_USER_2.CCS_CUSTOMER_PROD P
    WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
    and M.CREATE_DATE between to_date('16/10/2009','DD/MM/YYYY') and to_date('17/10/2009','DD/MM/YYYY')
    ) where cnt = 1
    and translate(Login_name,'x0123456789','x') is null check your answer and get back.
    Ravi Kumar
    Edited by: ravikumar.sv on Oct 16, 2009 3:12 PM

  • Query for date range?  DPL

    Sorry if this has been answered before, but I couldn't find the answer. I'm new to the DPL and was trying to find out how to query for objects that fall within a "date range". If I have an object that has a field "effective date", I would like to query for a list of objects that fall between a date range. Can I do this with DPL?
    thanks,
    John

    Hi,
    Yes, the DPL can be used to iterate over the entities where a given date field D falls with a specified date range. To do this, you define D as a @SecondaryKey, create a SecondaryIndex for it, and get a cursor for the specified range using one of the SecondaryIndex.entities() methods. Be sure to close the cursor.
    See the Key Ranges section in this class description:
    http://www.oracle.com/technology/documentation/berkeley-db/je/java/com/sleepycat/persist/EntityCursor.html
    Dates are treated as long values, so a range of Date keys is no different that a range of long keys. Date and long are both "simple types" according to the DPL definition.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Query for date range? JE

    Hi,
    I have seem some posts on the JE forum regarding quering for date range, but it is mostly using DPL.
    Is there any way to do that using the JE API
    Thanks,
    Mohammad

    Hi Mohammad,
    A date range query can be performed as a key range query. There's nothing special about dates except that you'll want to use a key binding that gives a meaningful sort order. If you're representing your dates in milliseconds, then a LongBinding (in com.sleepycat.bind.tuple) will work well. In general, use tuple bindings for keys, because they provide a meaningful sort order.
    To perform a range query, this FAQ has some hints:
    http://www.oracle.com/technology/products/berkeley-db/faq/je_faq.html#28
    On range searches in general, they can be done with Cursor.getSearchKeyRange or with the SortedSet.subSet and SortedMap.subMap methods, depending on whether you are using the base API or the Collections API. It is up to you which to use.
    If you use Cursor.getSearchKeyRange you'll need to call getNext to iterate through the results. You'll have to watch for the end range yourself by checking the key returned by getNext. This API does not have a way to enforce range end values automatically.
    If you use the Collections API you can call subMap or subSet and get an Iterator on the resulting collection. That iterator will enforce both the beginning and the end of the range automatically.
    Does this answer your question?
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • 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

  • SQL Query with Date Range

    I have a query that will retrieve order between selected dates. It works great but returns no record if the 2 dates are the same.
    Example:
    Orders between 9-1-2010 and 9-30-2010 retunes 35 records.
    But if I select between -9-25-2010 and 9-25-2010, so I can see all order from this 1 day, it returns 1 records, and I know there are records for that day!
    Here's my query:
    <%
    Dim rsOrders__MMColParam
    rsOrders__MMColParam = "1"
    If (Request.QueryString("datefrom") <> "") Then
      rsOrders__MMColParam = Request.QueryString("datefrom")
    End If
    %>
    <%
    Dim rsOrders__MMColParam2
    rsOrders__MMColParam2 = "1"
    If (Request.QueryString("dateto") <> "") Then
      rsOrders__MMColParam2 = Request.QueryString("dateto")
    End If
    %>
    <%
    Dim rsOrders
    Dim rsOrders_cmd
    Dim rsOrders_numRows
    Set rsOrders_cmd = Server.CreateObject ("ADODB.Command")
    rsOrders_cmd.ActiveConnection = MM_ezcaldatasource_STRING
    rsOrders_cmd.CommandText = "SELECT * FROM dbo.orders WHERE (OrderDate between ? and ?) AND Finalized  = 1"
    rsOrders_cmd.Prepared = true
    rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param1", 135, 1, -1, rsOrders__MMColParam) ' adDBTimeStamp
    rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param2", 135, 1, -1, rsOrders__MMColParam2) ' adDBTimeStamp
    Set rsOrders = rsOrders_cmd.Execute
    rsOrders_numRows = 0
    %>

    2 possible
    1) Change the column's data type from a datetime to a date if supported by your DBMS
    2) Use date math to always add 1 day to the end date. So instead of the end date of 9-25-2010 (00:00) it will be 9-26-2010 (00:00)

  • Querying multiple date ranges in one select

    Hello guys,
    DDL:
    CREATE TABLE DBO.SALES (
    TYPE VARCHAR(50)
    , REVENUE DECIMAL(19,2)
    , SALE_DATE_TIME DATETIME)
    What I'm trying to figure out is how I can pass two dates to a where clause querying this table, a start date and an end date. From this I can group up the REVENUE for each TYPE (but there's more):
    SELECT TYPE, COUNT(*) AS NBR_OF_SALES, SUM(REVENUE) AS TOTAL_REVENUE
    FROM REPORTS.DBO.SALES
    WHERE SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD
    GROUP BY TYPE
    I'd now like to add a couple of extra columns in the select that calculates the TOTAL_REVENUE equal to the time period of the two dates, but offset by 1 week, 1 month and 1 year, so that would be three columns. I'm guessing this would be possible with a
    CROSS APPLY, but I just can't get my head around it at this time. Just for clarity, the expected output would contain these columns:
    TYPE, NBR_OF_SALES, TOTAL_REVENUE, TOTAL_REVENUE_LAST_WEEK, TOTAL_REVENUE_LAST_MONTH, TOTAL_REVENUE_LAST_YEAR
    Any help is greatly appreciated.

    SELECT TYPE,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD THEN 1 ELSE 0 END) AS NBR_OF_SALES,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(WEEK,1,@STARTPERIOD) AND DATEADD(WEEK,1,@ENDPERIOD) THEN 1 ELSE 0 END) AS NBR_OF_SALES_WeekOffset,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(MONTH,1,@STARTPERIOD) AND DATEADD(MONTH,1,@ENDPERIOD) THEN 1 ELSE 0 END) AS NBR_OF_SALES_MonthOffset,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(WEEK,1,@STARTPERIOD) AND DATEADD(WEEK,1,@ENDPERIOD) THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE_WeekOffset,
    SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(MONTH,1,@STARTPERIOD) AND DATEADD(MONTH,1,@ENDPERIOD) THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE_MonthOffset
    FROM REPORTS.DBO.SALES
    WHERE SALE_DATE_TIME BETWEEN @STARTPERIOD AND DATEADD(MONTH,1,@ENDPERIOD)
    GROUP BY TYPE
    Is this what you want?
    We have three total columns per metric, one for the period, one for the period offset by a week, and one for the period offset by a month.
    We've also adjusted the where so all the data for the offset period is included.

  • Need an MDX query for Date range

    Hi there,
    My requirement is to filter sales from 1st of month to until holiday of the month. I am able to use to ":" range function for filtering, but I am not able to filter on holiday date. Becuase both filter and exists are giving set results, I need
    member expression to use range function. Please let me know ASAP.
    Thanks,
    ATRSAMS

    Hi ATRSAMS ,
    You can get the first member of the month with the OpeningPeriod function, and item(#) to have a specific member from a set .
    So if you want first member from your set : filter(.....).ITEM(0)
    Regards, David .

  • How to split the query using rowid

    can anybody pls tell me logic to spliting the sql query by using rowid through dba_extent...

    Using Parallel Execution will help if it is the DB that is the bottleneck, but if on the other hand it is your client program that is the bottleneck (and the DB is supplying the data as fast as it can) then splitting the query into rowid ranges and having multiple extract clients will have benefit.
    1) Each extract process should read DBA_EXTENTS for the given object and select the extents it should process (Ideally 1 extract process per Data File).
    2) Use the DBMS_ROWID.ROWID_CREATE to build low and high rowid ranges based on the BLOCK_ID and BLOCKS from DBA_EXTENTS. Note you will also have to look up the DATA_OBJECT_ID for the Table in question.
    Note. Because we won't know the Row Number of the last row in the last block, use the first row in the first block after the last block and then use <.
    3) Add the ROWID hint to your query.
    4) Use >= "Low Rowid" < "High Rowid" (Note. use of < rather than <=)
    This method is only safe when the table in question is not being updated, as each extract process will be running its own read consistent view, and so there is a danger that transaction updating 2 rows could have only half of the update extracted (if 1 row was in 1 extent after it had been extracted and another row was in another extent before it was extracted).
    Only 1 rowid range can be passed to the query at a time (you can supply more, but a ROWID access will no longer be performed, it will simply to a FTS).

  • Date Range  - Delete ZERO Values

    Hi All,
         Few days back I had posted Query  Regarding  Date range  where in I enter Proj# from_month, from_year, To_month and To_year.
    The output data for the project must be within this year range.
    suppose I enter proj no. 13381 and
    from_month - 05
    from_year - 2004
    To_month - 04
    from_year - 2005
    This is the Desired Output:
    yearmonth                            plan14
    200406 -
    13381 -
    100
    200407 -
    13381 -
    100
    200409 -
    13381 -
    678
    Right Now the Output what Iam getting is:
    yearmonth                            plan14
    200405 -
    13381 -
    0
    200406 -
    13381 -
    100
    200407 -
    13381 -
    100
    200408 -
    13381 -
    0
    200409 -
    13381 -
    678
    I  Just  want the NON ZERO  PLan14 values.  The PLan14 values  with '0'  should  be deleted .
    Below  is the Jist of the code.
    RANGES: S_GJAHR FOR COSP-GJAHR,
            R_YEAR  FOR COSP-GJAHR,
            R_DATE  FOR PROJ-ERDAT,
            R_MONTH FOR CKML1-POPER,
            S_OBJNR FOR COSP-OBJNR,
            S_KSTAR FOR COSP-KSTAR.
    DATA: MNT_INDX(20)     TYPE C,
          HLD_INDX(3)      TYPE N.
    DATA:  W_MTH(2) TYPE N,
           W_DATE  LIKE PROJ-ERDAT.
    DATA: BEGIN OF ITAB_OUT_TYPE,
            PERIOD(10)       TYPE C,
            PROJDEF(20)          TYPE C,
            PROJ_TITLE(20)        TYPE C,
            STATUS(20)            TYPE C,
            PROJECT_TYPE(20)      TYPE C,
            START_DATE(20)        TYPE C,
            FINISH_DATE(20)      TYPE C,
            CONTRACT(20)          TYPE C,
            CLIENT(20)            TYPE C,
            PLAN14(25)       TYPE  C,
            END OF ITAB_OUT_TYPE.
    DATA ITAB_OUT LIKE ITAB_OUT_TYPE OCCURS 10 WITH HEADER LINE.
    FIELD-SYMBOLS <FS>.
    PARAMETERS FR_MONTH(2) TYPE N OBLIGATORY.
    PARAMETERS FR_YEAR(4) TYPE N OBLIGATORY.
    PARAMETERS TO_MONTH(2) TYPE N .
    PARAMETERS TO_YEAR(4) TYPE N .
    DATA: W_TXT(20)     TYPE C.
      R_YEAR-LOW = FR_YEAR.
      R_YEAR-HIGH = TO_YEAR .
      R_YEAR-OPTION = 'BT'.
      R_YEAR-SIGN = 'I'.
    APPEND R_YEAR.
    concatenate FR_YEAR FR_MONTH  INTO R_DATE-LOW .
      concatenate TO_YEAR TO_MONTH  INTO R_DATE-HIGH.
      R_DATE-OPTION = 'BT'.
      R_DATE-SIGN = 'I'.
      APPEND R_DATE.
    SELECT * FROM COSP
                 WHERE OBJNR = PRPS-OBJNR    AND
                  GJAHR IN R_YEAR     AND
         KSTAR BETWEEN '0000400996'    AND '0000400999' AND
          VERSN = '014'                               AND
          WRTTP = '01' .
          DO 12 TIMES.
            W_MTH = SY-INDEX.
            CONCATENATE COSP-GJAHR W_MTH  INTO W_DATE.
            CHECK W_DATE IN R_DATE.
            PERFORM CONVERT-DATE1 USING W_DATE.
            MOVE HOLDDATE1  TO ITAB_OUT-PERIOD.
            HLD_INDX = SY-INDEX.
            CONCATENATE 'COSP-WKG' HLD_INDX INTO W_TXT.
           ASSIGN (W_TXT) TO <FS>.
        CLEAR ITAB_OUT-PLAN14.
        ITAB_OUT-PLAN14 = ITAB_OUT-PLAN14 + <FS>.
    APPEND ITAB_OUT.
    enddo.
    ENDSELECT.
    I need  all positive values  of  the month  that are in the range.   The Zero values  must be deleted .  How  can i do that.  PLease  do let me  know.
    Thanks in advance
    Dan

    You can do this easily just by deleting all records in one shot.
    delete ITAB_OUT where plan14 = 0.
    You can put that statement after the SELECT...ENDSELECT.
    Or you can not even add them to the internal table at all.  You can check before you APPEND to the table.
    CLEAR ITAB_OUT-PLAN14.
    ITAB_OUT-PLAN14 = ITAB_OUT-PLAN14 + <FS>.
    <b>If itab_out-plan14 > 0.
    APPEND ITAB_OUT.
    endif.</b>
    enddo.
    ENDSELECT.
    Regards,
    Rich Heilman

  • Regarding date ranges in search criteria in oracle forms

    I am using employee number,name, person type, and date ranges as search criteria in custom form.
    when I enter employee number, hit the find button, I am getting the exact info in the result block.
    Result block contains
    employee name,personid,emp numb,org,start_date.
    Similarly when I enter employee number,dept, I am getting correct values.
    My question here is, when I enter date range. Iam unable to filter data.
    When i enter START_DATE between nvl(:BLOCKNAME.START_DATE,'01-JAN-1901') and nvl(:BLOCKNAME.END_DATE,'31-DEC-4712') at where clause in the result block. I got data for the date range also.
    if I give condition in the where clause, results are taking so much time when i search with employee name,number,dept,person type.
    If i query with date, persormance is good.
    Do u any know,how to prevent START_DATE between nvl(:BLOCKNAME.START_DATE,'01-JAN-1901') and nvl(:BLOCKNAME.END_DATE,'31-DEC-4712') when we search with employee name,number,dept,person type.

    Initailly Ididnt given any code in the where clause of the result block. I got data in the result block when i search with employee name,number,person type,dept etc except date range.
    If i give date range, irrespective of the date, getting all the data.
    after that i added code to the where clause of the result block. Now i am getting data for everything.
    like, when i query with employee name,number,even date range also.
    My question here, performance.
    when I query with date range, data is coming in expected time in the result block.
    when i query with dept, taking much time since date range logic exist in the where clause of the result block.
    I need to restrict the where clause only to the data ranges. Where clause should not necessary for employee number,name,person type dept search criteria
    Tahnks for your reply

  • Query to split one row to multiple based on date range

    Hi,
    I need to split single row into multple based on date range defined in a column, start_dt and end_dt
    I have a data
    ID      From date             End_dt                measure
    1        2013-12-01         2013-12-03            1
    1        2013-12-04         2013-12-06            2
    2        2013-12-01         2013-12-02            11
    3        2013-12-03         2013-12-04          22
    I required output as
    ID      Date                      measure
    1        2013-12-01              1
    1        2013-12-02              1
    1        2013-12-03              1
    1        2013-12-04              2
    1        2013-12-05              2
    1        2013-12-06              2
    2        2013-12-01             11
    2        2013-12-02             11
    3        2013-12-03             22
    3        2013-12-04            22
    Please provide me sq, query for the same
    Amit
    Please mark as answer if helpful
    http://fascinatingsql.wordpress.com/

    Have a calendar table for example and then probably using UNION ALL from date  and stat date JOIN the Calendar table
    SELECT ID,From date  FROM tbl
    union all
    SELECT ID,End_dt FROM tbl
    with tmp(plant_date) as
       select cast('20130101' as datetime)
       union all
       select plant_date + 1
         from tmp
        where plant_date < '20131231'
    select*
      from  tmp
    option (maxrecursion 0)
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

Maybe you are looking for