Selecting last record in a date range

Post Author: Alien8n
CA Forum: Data Connectivity and SQL
I need to extract the last record for a group and display it. Normally this would simply mean selecting the max date in the SQL but...I need the last record from within a specified date range. By using the max date it won't pull out any records for a group that has a record after my date range. ie I have 2 group items and they both have a record for each day of the weekBy leaving the date open it pulls out the last record, Friday, for each item in the group.I want to set a range saying give me the last record from Mon - Wed.What I want returned is Wed's records and if there's no records on Wednesday the next last record for that itemSo If I have...Item Date    RecordA     MON     1A     TUE      2A     FRI       3B     MON    4B     TUE      5B     WED    6B     THUR    7B     FRI       8I get A     TUE      2 B     WED    6FYI CR XI with SQL 2005

Post Author: Alien8n
CA Forum: Data Connectivity and SQL
The database actually contains sample tests by date with each sample being tested multiple times over several months.I need to be able to pull a date range from the whole dataset, but I only want the last record in each date range. Samples can be tested every week or every 2 weeks and then the last record is used to show how the sample is doing over time. So you would run the report for say 3 weeks and show the last record for each sample. This wouldn't be a problem if you were only interested in the last 3 weeks data, but if you want to look at historical records you have to be able to pull from that date range. In effect the query has to work regardless of whether you ask for Mon-Fri (using my example above) or Mon-Wed or even just Mon alone. I'll give your example above a try and see how it goes

Similar Messages

  • Sql query - Selecting last recorded values for each date in specified period

    Hello,
    Can someone please help me with my problem.
    I'm trying to get last recorded balance for each day for specific box (1 or 2) in specified period of days from ms access database using ADOTool.
    I'm trying to get that information with SQL query but so far unsuccessfully...  
    My table looks like this:
    Table name: TestTable
    Date Time Location Box Balance
    20.10.2014. 06:00:00 1 1 345
    20.10.2014. 12:00:00 1 1 7356
    20.10.2014. 18:45:00 1 1 5678
    20.10.2014. 23:54:00 1 1 9845
    20.10.2014. 06:00:02 1 2 35
    20.10.2014. 12:00:04 1 2 756
    20.10.2014. 18:45:06 1 2 578
    20.10.2014. 23:54:10 1 2 845
    21.10.2014. 06:00:00 1 1 34
    21.10.2014. 12:05:03 1 1 5789
    21.10.2014. 15:00:34 1 1 1237
    21.10.2014. 06:00:00 1 2 374
    21.10.2014. 12:05:03 1 2 54789
    21.10.2014. 15:00:34 1 2 13237
    22.10.2014. 06:00:00 1 1 8562
    22.10.2014. 10:00:00 1 1 1234
    22.10.2014. 17:03:45 1 1 3415
    22.10.2014. 22:00:00 1 1 6742
    22.10.2014. 06:00:05 1 2 562
    22.10.2014. 10:00:16 1 2 123
    22.10.2014. 17:03:50 1 2 415
    22.10.2014. 22:00:10 1 2 642
    23.10.2014. 06:00:00 1 1 9876
    23.10.2014. 09:13:00 1 1 223
    23.10.2014. 13:50:17 1 1 7768
    23.10.2014. 19:47:40 1 1 3456
    23.10.2014. 21:30:00 1 1 789
    23.10.2014. 23:57:12 1 1 25
    23.10.2014. 06:00:07 1 2 976
    23.10.2014. 09:13:45 1 2 223
    23.10.2014. 13:50:40 1 2 78
    23.10.2014. 19:47:55 1 2 346
    23.10.2014. 21:30:03 1 2 89
    23.10.2014. 23:57:18 1 2 25
    24.10.2014. 06:00:55 1 1 346
    24.10.2014. 12:30:22 1 1 8329
    24.10.2014. 23:50:19 1 1 2225
    24.10.2014. 06:01:00 1 2 3546
    24.10.2014. 12:30:26 1 2 89
    24.10.2014. 23:51:10 1 2 25
    Let's say the period is 21.10.2014. - 23.10.2014. and I want to get last recorded balance for box 1. for each day. The result should look like this:
    Date Time Location Box Balance
    21.10.2014. 15:00:34 1 1 1237
    22.10.2014. 22:00:00 1 1 6742
    23.10.2014. 23:57:12 1 1 25
    So far I've managed to write a query that gives me balance for ONLY ONE date (date with highest time in whole table), but I need balance for EVERY date in specific period.
    My incorrect code (didn't manage to implement "BETWEEN" for dates...):
    SELECT TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM TestTable
    WHERE Time=(SELECT MAX(Time)
    FROM TestTable
    WHERE Location=1 AND Box=1 );
    Tnx!
    Solved!
    Go to Solution.

    For loop
    following query keep day (here 24 in below query) Variable from ( 1 to 28-29/30/31 as per month)
    SELECT TOP 1 TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM Test Table.
    WHERE  Time=(SELECT MAX(Time) FROM TestTable WHERE Location=1 AND Box=1 )
    AND DATE = "2014-10-24";
    PBP (CLAD)
    Labview 6.1 - 2014
    KUDOS ARE WELCOMED.
    If your problem get solved then mark as solution.

  • Select is not working with date range .

    In my program doing a simple select from cdhdr table based on date range i have created but it fails .
    here is the code I am using.
    SELECT SINGLE *   FROM tvarv  INTO  wa_tvarv WHERE name EQ 'Z_SERVICE_CHANGE_DATE' .
    move wa_tvarv-low to v_date .
    move wa_tvarv-high to v_time .
    fill the date range
    *write v_date to ra_date-low  .
        move  v_date TO ra_date-low  .
    *write sy-datum to v_dateh .
        ra_date-high = sy-datum  .
        ra_date-sign = 'I' .
        ra_date-option = 'BT' .
        APPEND ra_date .
    fill the time range
        move v_time  TO  ra_time-low .
    *write sy-uzeit to v_timeh .
        ra_time-high = sy-uzeit  .
        ra_time-sign = 'I' .
        ra_time-option = 'BT' .
        APPEND ra_time .
    *Get all the records from cdhdr table for contract i.e VERKBELEG .
        *SELECT objectclas objectid FROM cdhdr INTO CORRESPONDING FIELDS OF TABLE it_cdhdr*
                                   *WHERE objectclas = 'VERKBELEG'*
                                   *AND  udate IN ra_date*
                                   *and  UTIME in ra_time .*
    above select is creating Problem .
    Help will be Appreciated .

    I wonder if you have a conflict between your date and time.  If you want all records between 3PM and 3AM the next day, that will cause a conflict.  That's because the first date should be "before" the second date, wthin a given day.
    To troubleshoot, why don't you try taking the time part out of the SQL?

  • How to select last record or first record value in a formula?

    Post Author: d111
    CA Forum: General
    I am using a query in a report that in addition to other columns, has a column of month labels like:
    There are no unique identifiers or sequence fields that can be used.   There is only 4 columns: the label and 3 percent columns.
    Jan07Feb07Mar07Apr07etc.
    I would like to show the range of months I am using in a label on the report showing the last and first records value. like: "Jan07 to Jun07".
    I can not see how to use the functions maximun or minimum since it sorts the months alphabetically.
    I would like to use a formula to just grab the first and last record.  I can't seem to find any information on this anywhere.
    Please advise and thanks.

    Post Author: bettername
    CA Forum: General
    Hmm... I suspect what you actually want is the minimum and maximum dates in the report, rather than the first and last records (even if they do happen to come out in date order). So you need to convert the text dates to real ones, and then pick the min/max values.
    If so, try these:
    Formula "ConvertToDate" (for details section) which converts your text field into a real date - suppress it once placed!:
    //Since you have 2-digit years, this conversion assumes that all your dates are >=year 2000
    numbervar the_month;if left({YourTable.YourField},3) = "Jan" then the_month:=1 elseif left({YourTable.YourField},3) = "Feb" then the_month:=2 elseif left({YourTable.YourField},3) = "Mar" then the_month:=3 elseif left({YourTable.YourField},3) = "Apr" then the_month:=4 else
    //...etc etc you fill in the rest!
    if left({YourTable.YourField},3) = "Dec" then the_month:=12;
    date(tonumber("20"+right({YourTable.YourField},2)), the_month, 1);
    Formula "Show Date Range" (for anywhere on the report) to show the min/max values of the dates;
    totext (minimum({@ConvertToDate})) + " to "+totext(maximum({@ConvertToDate}))
    If, however, you really just want the first and last records, and you want it at the top of your report as a title (eg: in the Report Header), then I can't see how it can be done without using a subreport that looks at the same dataset, but only displays the first and last records (using the formulas below).
    Stick this in the details section, and call it "HoldRecords" and suppress it:
    whileprintingrecords;stringvar first;stringvar last;if recordnumber = 1 then first:={YourTable.YourField}; //if its the first record that crystal is displaying, store it in a variableif recordnumber = count({YourTable.YourField}) then last :={YourTable.YourField} //and again, but for the last record
    Now create another formula, and put it into the report footer:
    evaluateafter({@HoldRecords});stringvar first" to "stringvar last //will display "Jan07 to Apr07"
    NOTE:  if you have the report ordered by anything, this'll throw the record numbering, and therefore the first and last records, therefore... urgh.
    Hope this helps...

  • Getting Record Count From Date Range - IDE: PLSQL Developer

    I would like to count the number of member records that fall within a specified date range based on the members effective and expiration dates and their 'elg_code'. I have posted the SQL for some sample data. What I would like to see returned is three columns of counts where the members eff_date exp_date fall within the date range specified by the SQL and have an Elg_code of ' ' (one blank space).
    So, what I would like is all members with elg_code ' ' where there eff_dt and exp_dt range falls within APR 2012, MAY 2012 & JUN 2012. So, based on the sample data I posted, Mark, where his elg_code record is ' ', his eff_dt is 1/1/2011 and his exp_dt is within APR 2012 (4/30/2012). Mark's range falls within APR 2012, but not MAY or JUNE of 2012. Marty would tally for both APR and MAY since his eff_dt is before MAY 2012 and his exp is within MAY 2012. etc..
    Based on the data below, the results should look like:
    APR MAY JUN
    4 3 2
    APR should have FRANK, MARK, MARTY, MARY,
    MAY should have FRANK, MARTY, MARY
    JUN should have FRANK and MARY
    NOAM and JEAN should not show up as their records with elg_code ' ' do not have eff_dt and exp_dt records that fall within APR-JUN 2012.
    So what I tried without success as it appears I have some kind of Cartesian issue (?), is:
    select count(m1.mbr_name) APR,
    count(m2.mbr_name) MAY,
    count(m3.mbr_name) JUN
    from mbr2 m1,
    mbr2 m2,
    mbr2 m3
    where m1.eff_dt < '01-may-2012'
    and m1.exp_dt > '01-apr-2012'
    and m1.elg_code = ' '
    and m2.eff_dt < '01-jun-2012'
    and m2.exp_dt > '01-may-2012'
    and m2.elg_code = ' '
    and m3.eff_dt < '01-jul-2012'
    and m3.exp_dt > '01-jun-2012'
    and m3.elg_code = ' '
    Below is the DML
    Thanks for any assistance!
    create table mbr2 (mbr_name varchar(10), grpid varchar(1), eff_dt date, exp_dt date, elg_code varchar(1))
    commit
    insert into mbr2 values ('MARK', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('04-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARK', 'A', to_date('05-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('MARTY', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('05-31-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARTY', 'A', to_date('06-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('FRANK', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('FRANK', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('MARY', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARY', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('JEAN', 'C', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('07-01-2011', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('JEAN', 'C', to_date('07-01-2011', 'MM-DD-YYYY'), to_date('01-01-2012', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('NOAM', 'D', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), ' ')
    commit

    Hi,
    Here's one way:
    WITH     all_months     AS
         SELECT     LEVEL                         AS month_num
         ,     ADD_MONTHS (first_month, LEVEL - 1)     AS month_begin
         ,     ADD_MONTHS (first_month, LEVEL)           AS next_month_begin
         FROM     (
                   SELECT  TO_DATE ('04-01-2012', 'MM-DD-YYYY')     AS first_month
                   ,     TO_DATE ('06-01-2012', 'MM-DD-YYYY')     AS last_month
                   FROM     dual
         CONNECT BY     level     <= 1 + MONTHS_BETWEEN (last_month, first_month)
    SELECT    COUNT (CASE WHEN month_num = 1 THEN 1 END)     AS month_1
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2
    ,       COUNT (CASE WHEN month_num = 3 THEN 1 END)     AS month_3
    FROM       all_months  a
    JOIN       mbr2        m  ON  a.month_begin       <= m.exp_dt
                            AND a.next_month_begin      >  m.eff_dt
    WHERE     m.elg_code      = ' '
    ;This assumes you know how many months will be in the output. If you want to derive that from the data, or to give the columns meaningful aliases (such as APR-2011 instead of MONTH_1), then you'll need dynamic SQL (to get separate columns for each month) or string aggregation (if you don't mind one big VARCHAR2 column, formatted to look like several columns). See {message:id=3527823}
    If you don't mind modifying the query a little every time you run it, you can hard-code the number of columns and the month names. In the main query, put exactly as many lines as you need, with the alias you want at the end. That is, instead of
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2say
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS may_2012I'm not sure exactly what the problem was with the query you posted. It looks like this site cut off parts of the query.
    One thing that is definitely a mistake is that you're comparing DATE columns to VARCHAR2 literals. Never compare a DATE to a VARCHAR2; compare DATEs to DATEs (or compare VARCHAR2s to VARCHAR2s). So instead of
    and m1.exp_dt > '01-apr-2012'you should say
    and m1.exp_dt > TO_DATE ('01-apr-2012', 'dd-mon-yyyy')Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful.
    Edited by: Frank Kulash on Jun 26, 2012 3:32 PM

  • How to select Months with in a date range

    Hi
    I am working on a report which will list out Month wise totals in each row.
    How should i select the Months with in a date range and do the sum ( User may select the date range for  N number of  years also. Example :  01/10/2000 to 01/01/2010 )
    Thanks in advance
    Thanks&regards
    Mrudula

    Hi
       Use the following code:
    data: l_date(8) value '03042007',
            l_mname   type   t247-ktx,
            l_c(25).
      select single ktx from t247
      into l_mname
      where spras = sy-langu
      and mnr = l_date+2(2).
      concatenate l_mname l_date+6(2)
      into l_c." SEPARATED BY space.
      data: g_date(10) type c. " Processing date of Report
      g_date = sy-datum.
      concatenate g_date0(4)  g_date4(2)  g_date+6(2) into g_date.
      data: g_date1(10) type c.
      g_date1 = so_verab-high.
      concatenate g_date10(4)  g_date14(2)  g_date1+6(2) into g_date1.
    Regards,
    Sreeram

  • How to pull records only for particular date range in Flex frm SAP wd table

    Hi,
    Can anyone help me with databing for datefield.
    I am using two datefields in Flex for Start Date and End Date. When I click the Execute button, it should pull only the records for that date range from SAP wd table and display in my Flex datagrid.
    Thanks,
    Sri
    Edited by: rmsridevi on May 17, 2011 4:38 PM

    Hi,
    Your query has mistakes as well. I corrected them.
    Check this two different ways were in first you can define the period (month) you want and in second you have the option to select from the drop drown list :
    SELECT T0.DocNum, T0.DocDate, T0.CardName,T0.DocTotal,T1.whsCode
    FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
    WHERE t0.docdate >= '2011.01.01' and t0.docdate <='2011.01.31'
    OR
    SELECT T0.DocNum,T0.DocDate,T0.CardName,T0.DocTotal,T1.whsCode
    FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
    WHERE t0.docdate >= [%1] and t0.docdate <= [%2]
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Select multiple date ranges?

    I am looking to select from a set of date ranges. I know that a UNION will work, but how to I get a valid set of results from multiple dates without a UNION?
    example:
    select date from test
    where date >= '01-MAR-07 0000'
    and date <= '02-MAR-07 0000'
    and date >= '04-MAR-07 0000'
    and date <= '08-MAR-07 0000'
    order by date;
    This gets zero records.
    But if I use UNION:
    select date from test
    where date >= '01-MAR-07 0000'
    and date <= '02-MAR-07 0000'
    union
    select datre from test
    and date >= '04-MAR-07 0000'
    and date <= '08-MAR-07 0000'
    order by date;
    It works, I get the records in no test table.
    How can I structure this query to work without UNION?

    true, date is taken eh?
    I am still running into an issue:
    Let's say I am using the following context:
    select date1, name from test
    where name in ('SUNDAR', 'ASUNDAR')
    and date1 >= '01-MAR-07 0000'
    and date1 <= '03-MAR-07 0000'
    or (date1 >= '05-MAR-07 0000'
    and date1 <= 07-MAR-07 0000');
    My results are wrong, it would not include the select as part of the or clause.

  • How to display top n records(sorted by no of defects) within date range

    I would like to display top n production orders(sorted by defects linked with each order) within the date range entered by the user.
    Please advice.
    I have 3 parameters to my report:
    1. n(n is no of top records to be displayed)
    2. From date
    3. To date
    Appreciate quick response.
    Regards,
    Hari

    if you use the parameters in the report by placing them in the record selection it should display the records within your date range.
    if {date field} in ?start to ?end then true

  • Select Between two date ranges from xml file

    Hi ,
    I have a column date_ with datatype VARCHAR2(150) it stores
    data as
    05/19/2010 11:23 AM
    05/20/2010 12:23 PM
    05/22/2010 11:23 AM
    05/25/2010 11:23 AM
    i have to select all the rows between 05/19/2010 and 05/22/2010 how to do that this column is in xml file

    I have a table wit two fields
    Field1 is integer and field2 is xmltype
    in the xmltype i store an xml file
    <ParentNode>
    <Node>
    <Cat>1</Cat>
    <Date>05/19/2010 11:23 AM </Date>
    </Node>
    <Node>
    <Cat>2</Cat>
    <Date>05/20/2010 12:23 PM </Date>
    </Node>
    <Node>
    <Cat>3</Cat>
    <Date>05/22/2010 11:23 AM </Date>
    </Node>
    </Parentnode>
    I am using teh below query to retrive teh result
    SELECT T.Feild1, XML.* FROM Tablename T,
    XMLTable( 'Parentnod/Node' PASSING T.Feild2 COLUMNS Cat NUMBER PATH 'Cat' ,
    DATE_ VARCHAR2(100) PATH 'Date'
    )XML where cat >1;
    now i have to do teh same to select the rows between two date range 05/19/2010 and 05/21/2010
    hope i am able to make teh question simple

  • Date range query  problem  in report

    Hi all,
    I have created a report based on query and i want to put date range selection but query giving problem.
    If i am creating select list selection then it is working fine means it will display all records on the particular date.
    But what i need is that user will enter date range as creation_date1,creation_date2 and query should return all the records between these date range. i want to pass it by creating items, i created two items and passing creation_date range to display all records but not displaying and if not passing date then should take null as default and display all records
    Here is the query:
    /* Formatted on 2006/12/10 20:01 (Formatter Plus v4.8.0) */
    SELECT tsh."SR_HEADER_ID", tsh."SALES_DEPT_NUMBER", tsh."COUNTRY",
    tsh."LOCAL_REPORT_NUMBER", tsh."ISSUE_DATE", tsh."SUBJECT",
    tsh."MACHINE_SERIAL_NUMBER", tsh."MACHINE_TYPE", tsh."MACHINE_HOURS",
    tsh."STATUS"
    FROM "TRX_SR_HEADERS" tsh, "TRX_SR_PARTS" tsp
    WHERE (tsh.status LIKE :p23_status_sp OR tsh.status IS NULL)
    AND (tsh.machine_type LIKE :p23_machine_type_sp)
    AND ( tsh.machine_serial_number LIKE
    TO_CHAR (:p23_machine_serial_number_sp)
    OR tsh.machine_serial_number IS NULL
    AND ( TO_CHAR (tsh.failure_date, 'DD-MON-YY') LIKE
    TO_CHAR (:p23_failure_date_sp)
    OR TO_CHAR (tsh.failure_date, 'DD-MON-YY') IS NULL
    AND ( TO_CHAR (tsh.creation_date, 'DD-MON-YY')
    BETWEEN TO_CHAR (:p23_creation_date_sp)
    AND TO_CHAR (:p23_creation_date_sp1)
    OR TO_CHAR (tsh.creation_date, 'DD-MON-YY') IS NULL
    AND (tsh.issue_date LIKE :p23_date_of_issue_sp OR tsh.issue_date IS NULL)
    AND (tsh.country LIKE :p23_country_sp OR tsh.country IS NULL)
    AND ( tsh.local_report_number LIKE TO_CHAR (:p23_local_rep_num_sp)
    OR tsh.local_report_number IS NULL
    AND ( tsp.part_number LIKE TO_CHAR (:p23_part_number_sp)
    OR tsp.part_number IS NULL
    AND tsh.machine_type IN (
    SELECT DISTINCT machine_type
    FROM trx_sales_dept_machine_list
    WHERE sales_department_id IN (
    SELECT DISTINCT sales_department_id
    FROM trx_user_sales_department
    WHERE UPPER (user_name) =
    UPPER ('&APP_USER.'))
    AND SYSDATE >= valid_from)
    AND tsh.sr_header_id = tsp.sr_header_id
    can any one tell me wat is wroung in this query.
    Any other way to write this?
    Thank You,
    Amit

    Hi User....
    Here is some date range SQL that my teams uses with some success:
    For date columns that do not contain NULL values, try this (note the TRUNC, it might help with your "today" problem).
    The hard coded dates allow users to leave the FROM and TO dates blank and still get sensible results (ie a blank TO date field asks for all dates in the future.
    AND TRUNC(DATE_IN_DATABASE)
    BETWEEN
    decode( :P1_DATE_FROM,
    TO_DATE('01-JAN-1900'),
    :P1_DATE_FROM)
    AND
    decode( :P1_DATE_TO,
    TO_DATE('31-DEC-3000'),:
    :P1_DATE_TO)
    For date columns that contain NULL values, try this (a little bit trickier):
    AND nvl(TRUNC(DATE_IN_DATABASE),
    decode( :P1_DATE_FROM,
    decode( :P1_DATE_TO,
    TO_DATE('30-DEC-3000'),
    NULL),
    NULL)
    BETWEEN
    decode( :P1_DATE_FROM,
    TO_DATE('01-JAN-1900'),
    :P1_DATE_FROM)
    AND
    decode( :P1_DATE_TO,
    TO_DATE('31-DEC-3000'),
    :P1_DATE_TO)
    Note the 30-DEC-3000 versus 31-DEC-3000. This trick returns the NULL dates when the FROM and TO date range items are both blank.
    I hope this helps.
    By the way, does anyone have a better way of doing this? The requirement is given a date column in a database and a FROM and a TO date item on a page,
    find all of the dates in the database between the FROM and TO dates. If the FROM date is blank, assume the user want all dates in the past (excluding NULL dates). If the TO date is blank, assume that the user wants all of the dates in the future (excluding NULL dates). If both FROM and TO dates are blank, return all of the dates in the databse (including NULL dates).
    Cheers,
    Patrick

  • Help with Date Range

    Hi,
    I have an Oracle form which allows users to enter :start_date_active(mandatory) and :end_date_active (can be null).
    I need to check in form using SQL if there already exists a record for this date range or if there is any overlapping date ranges for the value entered and disallow user from entering that.
    I have my query as follows:
    SELECT (1)
    FROM <table> xsv
    WHERE 1=1
    AND ( (:START_DATE_ACTIVE >= xsv.start_date_active) AND
    (:START_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))
    OR
         (:END_DATE_ACTIVE >= xsv.start_date_active) AND
    (:END_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))
    But, this incorrectly allows me to enter over lapping date ranges when the :end_date_active is null.
    It should work as follows :
    eg.
    start_date_active end_date_active
    01-Apr-2009 allowed
    01-Mar-2009 10-Mar-2009 allowed
    01-feb-2009 Not allowed, as it overlaps with 1st record
    10-feb-2009 05-Mar-2009 Not allowed, as it overlaps with 2nd record
    05-Mar-2009 20-mar-2009 Not allowed, as it overlaps with 2nd record
    and so on.
    How do I modify my query to deal with null end dates ?
    Many Thanks
    Shankar

    Hi, Shankar,
    Whenever you have a question, it helps to post:
    (1) The version of Oracle (and any other relevant software) you're using
    (2) A little sample data (just enough to show what the problem is) from all the relevant tables
    (3) The results you want from that data (You posted this. It would be better if it were formatted.)
    (4) Your best attempt so far (formatted) (You posted this, but since it's unformated, it's very hard to read.)
    (5) The full error message (if any), including line number
    Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
    Formatted tabular output is okay for (3). Type these 6 characters
    &#123;code&#125;
    (small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
    Use different names for the parameters and the columns. For example, if the column is called start_date_active, call the corresponding parameter :p_start_date_active. It will help to keep them straight.
    In this statement, if the column end_date_active IS NULL, you're treating it as an impossibly late date:
    (:END_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))That's good. (You should really use TO_DATE ('31-DEC-4712', 'DD-MON-YYYY'), though.)
    If it's possible for the parameter :end_date_active to be NULL, you should use NVL in the same way, to equate it with some appropriate date.
    The correct way to see if the range a to b (a <= b) overlaps with x to y (x <= y) is:
    WHERE   b >= x
    AND     a <= yIf you want an expression that's TRUE when they do not overlap, then say
    WHERE   NOT (    b >= x
                AND  a <= y
                )or, equivalently,
    WHERE   b < x
    OR      a > y

  • 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

  • Check a number range with date range.

    i have table test which is having column
    Start Range
    End Range
    Start Date
    End date
    how can i check start range and end range does not exists b/w existing record also with date range of this record.
    end date can be blank also.

    990690 wrote:
    i have table test which is having column
    Start Range
    End Range
    Start Date
    End date
    how can i check start range and end range does not exists b/w existing record also with date range of this record.
    end date can be blank also.Your question makes absolutely no sense whatsoever. We could make guesses, but then we may be wrong in our guesses.
    Please provide sufficient details for people to be able to help you. Read the FAQ: {message:id=9360002}

  • Analytic query to select next record

    Hi all,
    I would like to query the below two table match to ouput.
    1.dev_wt 2.dev_map 3.Output Result
    To help more clearance please see this image link : http://lh6.ggpht.com/_xL6eBqjW6Yo/TEqnSvlF_FI/AAAAAAAAB0U/i2sclnnaj6g/Untitled-3.jpg
    1. dev_wt
    PMS_COMP     PMS_I       PMS_PERF_D   PMS_WT   PMS_CREATION_D
    BBOARD     GICEQGROSS    04/01/2001     30     04/05/2001
    BBOARD     GICST_B       04/01/2001     5      04/05/2001
    BBOARD     SBGS_B        04/01/2001     65     04/05/2001
    BBOARD     GICEQGROSS    04/11/2001     30     04/15/2001
    BBOARD     GICST_B       04/11/2001     5      04/15/2001
    BBOARD     SBGS_B        04/11/2001     65     04/15/20012. dev_map
    GS_CODE     GS_I_CODE    GS_I_ID  MD_ID   GS_START_DT    GS_END_DT
    GICEQGROSS   CIWL        304       15     01/04/1998     31/03/2004
    GICEQGROSS   CIWL        304       2     01/04/2004      31/03/9998
    GICST_B      GICST_B     3707      15     01/04/2000     31/12/9998
    SBGS_B       SBGS_B      2231      15     01/04/1992     30/09/2003
    SBGS_B       SBGS_B      564       15     01/10/2003     31/12/9998I would like to match PMS_I = GS_CODE to retrieve GS_I_CODE and using analytic
    query to find next record.
    Because I need to select GICEQGROSS record of PMS_PERF_D date and next
    GICEQGROSS record of PMS_PERF_D from dev_wt table and put GS_WT_FR and GS_WT_TO of Output result.
    ***Date is change to YYYYMMDD format
    ***PMS_WT is devided by 100
    *3. Output Result*
    GS_I_ID    PMS_COMP     GS_I_CODE     GS_WT_FR     GS_WT_TO     GS_I_CALC
    304       BBOARD        CIWL          20010401     20010410     0.3
    3707      BBOARD        GICST_B       20010401     20010410     0.05
    5209      BBOARD        SBGS_B        20010401     20010410     0.65PMS_COMP is from dev_wt table
    GS_I_CODE is from dev_map table join with dev_wt
    GS_WT_FR is from dev_wt table of GS_START_DT
    GS_WT_TO is from dev_wt table of next record GS_START_DT where PMS_I ='GICEQGROSS'
    Now my difficulty is to select next record of PMS_PERF_D using analytic query. Below
    is my query...
    SELECT GS_I_ID, PMS_COMP, GS_I_CODE, GS_WT_FR, GS_WT_TO, GS_I_CALC
    FROM dev_wt (
    SELECT lead(PMS_PERF_D) over(partition by PMS_I order by PMS_PERF_D) as GS_WT_TO       
    FROM dev_wt where PMS_I ='GICEQGROSS')
    left join dev_map on PMS_I = GS_CODE ;Thanks
    Edited by: WinZone on Jul 24, 2010 4:46 PM
    Edited by: WinZone on Jul 24, 2010 4:50 PM

    Hi,
    This query should be fine:
    SELECT DISTINCT t2.gs_i_id, pms_comp, t2.gs_i_code,
                    TO_CHAR
                       (MIN (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i),
                        'yyyymmdd'
                       ) gs_wt_fr,
                    TO_CHAR
                       (MAX (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i) - 1,
                        'yyyymmdd'
                       ) gs_wt_to,
                    pms_wt / 100 gs_i_calc
               FROM dev_wt t1, dev_map t2
              WHERE t2.gs_code = t1.pms_iREM Same remark as odie: should be "2231" instead ...

Maybe you are looking for

  • Generating a Web Service and Oracle DATE type

    Hi, I need to generate a Web Service from an EJB model based upon entities generated from the Oracle 11g database that contain DATE types. I realize that java.sql.Date may not be a supported JAX-RPC type but I would be VERY grateful for some instruct

  • Java.lag out of memory error

    my x2 cannot work with the pdf reader i downloaded from nokia store as there is java.lag out of memory error what should i do

  • Unable to join network

    Hi everyone Please help me I can`t connect to wi-fi at all I haven`t used it for about 2 months now i found out it won`t connect to ANY network I`ve tryed it all, restore as new, reset ect And i`m very busy that i can`t make it to Apple store Please

  • How to create minute range

    Hi experts, I need to populate two column in order to get waiting time, then i need to create minute range as below:- i ) less than 30 minute ii) 30 minute - 60 minute iii) 60 minute - 90 minute iv) more than 90 minute select TO_CHAR(abs(vlh.SERVICIN

  • Compiz no longer starts...

    I hadnt upgraded in a month or so, so ran the rounds using danimoths latest...had a libwnck conflict but finally got everything back installed.  Now, compiz will not start. XGL is starting fine, just no love with compiz.  Here is the output: compiz: