Problem returning max date in 2009

Is anyone else experiencing this problem?
Create the following table and add the data....
CREATE TABLE ETL_LASTRUN
LAST_RUN DATE
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('12/31/2008 23:17:13', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('12/31/2008 23:26:31', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('12/31/2008 23:36:35', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('12/31/2008 23:46:23', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('12/31/2008 23:56:30', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('01/01/2009 00:06:36', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('01/01/2009 00:16:39', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('01/01/2009 00:26:30', 'MM/DD/YYYY HH24:MI:SS'));
Insert into ETL_LASTRUN
(LAST_RUN)
Values
(TO_DATE('01/01/2009 00:36:38', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;
Now, run the following query
SELECT MAX(to_char(last_run,'MM/DD/YYYY HH24:MI')) as last_run FROM etl_lastrun
The results I get (using 10.2.0.1.0)
last_run
=====
12/31/2008 23:56

SQL> SELECT MAX(to_char(last_run,'MM/DD/YYYY HH24:MI')) as last_run FROM etl_lastrun
  2  /
LAST_RUN
12/31/2008 23:56
1 rij is geselecteerd.I get the same expected result. Expected, because you are taking the max of the character representation of the date. And '12/31...' is greater than '01/01...'. You can see that like this:
SQL> select to_char(last_run,'MM/DD/YYYY HH24:MI')
  2    from etl_lastrun
  3   order by 1
  4  /
TO_CHAR(LAST_RUN
01/01/2009 00:06
01/01/2009 00:16
01/01/2009 00:26
01/01/2009 00:36
12/31/2008 23:17
12/31/2008 23:26
12/31/2008 23:36
12/31/2008 23:46
12/31/2008 23:56
9 rijen zijn geselecteerd.So switch the max function and the to_char display function for the desired result:
SQL> select to_char(max(last_run),'MM/DD/YYYY HH24:MI') as last_run
  2    from etl_lastrun
  3  /
LAST_RUN
01/01/2009 00:36
1 rij is geselecteerd.Regards,
Rob.

Similar Messages

  • Return max date record previous to start date

    Hello,
    Can anyone help please?
    I have two subqueries, one called tableofchildren, the other called tableofworkers. Tableofchildren identifies the date that the child started a class.
    The second query, Tableofworkers, lists workers and their involvement history with the child. A child may have had a history with multiple workers. The worker has an allocatedstartdate indicating when they first started working with the child.
    For my purpose, I need to return the workername and allocatedstartdate of the worker involved with the child most recently before or at the time the child started the course.
    I've partially accomplished this with the query below. However, due to data quality, the child might not always have a worker allocated (allocatedstartdate) earlier than or equal to the child's course start date. In this case, the query fails and doesn't return the child's record, excluding it from the dataset.
    Can anyone suggest a way of amending this query so it acts as a left outer query and returns all the child records and null vlaues where they have no worker start date allocated before or on the course start date?
    Thanks! :)
    select *
    from
    select tc.childid,
    tc.childname,
    tc.enteredcourse,
    tw.workername,
    tw.allocatedstartdate,
    row_number() over ( partition by tc.childid, tc.enteredcourse order by tw.allocatedstartdate desc) rn
    from tableofchildren tc, tableofworkers tw
    where tc.childid = tw.childid(+)
    and tc.enteredcourse >= nvl(tw.allocatedstartdate,add_months(sysdate,-10000))
    where rn = 1 desired output
    CHILDID CHILDNAME            ENTEREDCOURSEDATE         WORKERNAME           ALLOCATEDSTARTDATE       
    C1000   Johnny Rotten        01-APR-11                 Mrs Gerbil           19-AUG-10                
    C1256   Doris Dingle         12-AUG-03                 Mrs Pepsi            12-AUG-03                
    C3466   Bonny Boy            25-MAR-11                 Mrs Jones            23-FEB-11                
    C4567   Casper Ghost         21-MAR-09                                                               
    C1245   Doris Dingle         20-NOV-06             create table tableofchildren
    (ChildID varchar(6),
    ChildName varchar (20),
    EnteredCourse date,
    LeftCourse date);
    insert into tableofchildren (ChildID, ChildName, EnteredCourse, LeftCourse) values ('C1000', 'Johnny Rotten', to_date('01/04/2011','dd/mm/rrrr'), to_date('23/05/2011','dd/mm/rrrr'));
    insert into tableofchildren (ChildID, ChildName, EnteredCourse, LeftCourse) values ('C1256', 'Doris Dingle', to_date('12/08/2003','dd/mm/rrrr'), to_date('16/09/2005','dd/mm/rrrr'));
    insert into tableofchildren (ChildID, ChildName, EnteredCourse, LeftCourse) values ('C3466', 'Bonny Boy', to_date('25/03/2011','dd/mm/rrrr'), to_date('28/03/2011','dd/mm/rrrr'));
    insert into tableofchildren (ChildID, ChildName, EnteredCourse, LeftCourse) values ('C4567', 'Casper Ghost', to_date('21/03/2009','dd/mm/rrrr'), to_date('22/04/2010','dd/mm/rrrr'));
    insert into tableofchildren (ChildID, ChildName, EnteredCourse, LeftCourse) values ('C1245', 'Doris Dingle', to_date('20/11/2006','dd/mm/rrrr'), to_date('30/12/2008','dd/mm/rrrr'));
    create table tableofworkers
    (WorkerID varchar(6),
    WorkerName varchar (20),
    AllocatedStartDate date,
    AllocatedEndDate date,
    ChildID varchar(6));
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3453', 'Mrs Whatever', to_date('12/05/2009','dd/mm/rrrr'), to_date('13/06/2009','dd/mm/rrrr'), 'C1000');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3442', 'Mr Toad', to_date('14/07/2010','dd/mm/rrrr'), to_date('18/08/2010','dd/mm/rrrr'), 'C1000');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W14592', 'Mrs Gerbil', to_date('19/08/2010','dd/mm/rrrr'), NULL, 'C1000');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3442', 'Mrs Pepsi', to_date('12/08/2003','dd/mm/rrrr'), to_date('22/04/2007','dd/mm/rrrr'), 'C1256');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3490', 'Mr Tomato', to_date('12/03/2008','dd/mm/rrrr'), to_date('30/04/2009','dd/mm/rrrr'), 'C3466');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3453', 'Mrs Whatever', to_date('01/06/2009','dd/mm/rrrr'), to_date('30/04/2010','dd/mm/rrrr'), 'C3466');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3457', 'Mrs Jones', to_date('23/02/2011','dd/mm/rrrr'), null, 'C3466');
    insert into tableofworkers (WorkerID, WorkerName, AllocatedStartDate, AllocatedEndDate, ChildID) values ('W3453', 'Mrs Jobsworth', to_date('22/11/2006','dd/mm/rrrr'), null, 'C1245');
    create table OutputWanted
    (ChildID varchar(6),
    ChildName varchar (20),
    EnteredCourseDate date,
    WorkerName varchar(20),
    AllocatedStartDate date);
    insert into OutputWanted (ChildID, ChildName, EnteredCourseDate, WorkerName, AllocatedStartDate) values ('C1000', 'Johnny Rotten', to_date('01/04/2011','dd/mm/rrrr'), 'Mrs Gerbil', to_date('19/08/2010','dd/mm/rrrr'));
    insert into OutputWanted (ChildID, ChildName, EnteredCourseDate, WorkerName, AllocatedStartDate) values ('C1256', 'Doris Dingle', to_date('12/08/2003','dd/mm/rrrr'), 'Mrs Pepsi', to_date('12/08/2003','dd/mm/rrrr'));
    insert into OutputWanted (ChildID, ChildName, EnteredCourseDate, WorkerName, AllocatedStartDate) values ('C3466', 'Bonny Boy', to_date('25/03/2011','dd/mm/rrrr'), 'Mrs Jones', to_date('23/02/2011','dd/mm/rrrr'));
    insert into OutputWanted (ChildID, ChildName, EnteredCourseDate, WorkerName, AllocatedStartDate) values ('C4567', 'Casper Ghost', to_date('21/03/2009','dd/mm/rrrr'), null, null);
    insert into OutputWanted (ChildID, ChildName, EnteredCourseDate, WorkerName, AllocatedStartDate) values ('C1245', 'Doris Dingle', to_date('20/11/2006','dd/mm/rrrr'), null, null);
    Edited by: Tiny Penguin on 21-Nov-2011 07:03

    Tiny Penguin wrote:
    Hi Centinul.
    Thanks! That looks like it's working great!
    I'm confused though...aside from ANSI joins, how come this works and mine doesn't? You missed a (+) in your WHERE clause:
    where tc.childid = tw.childid(+)
    and tc.enteredcourse >= nvl(tw.allocatedstartdate,add_months(sysdate,-10000))In this case the second condition is applied AFTER the tables were outer joined filtering out the row from the entire resultset because the enteredcourse date was less than the allocatedstartdate
    It probably should have been:
    where tc.childid = tw.childid(+)
    and tc.enteredcourse >= nvl(tw.allocatedstartdate(+),add_months(sysdate,-10000))In this case the second condition was applied as part of the OUTER join between the two tables, keeping the row with the child ID of 'C1245' from the tableofchildren and discarding it from the tableofworkers (due to LEFT JOIN)
    ps...I hate ANSI joins! :DANSI joins rock :)
    Edited by: Centinul on Nov 21, 2011 10:52 AM

  • Query - Filter by a specific dynamic date - Return only the max date record

    Hello experts,
    I'm building a query. In this query I have a filter named validity date. I need to select a record where the max date is less than this filter.
    For example:
    Comp. Code | Plant | Mat. | Cost Type | Valid. Date | Value
    0000000001   00001  0001  xxxxxxxx    12.05.2008   2,00
    0000000001   00001  0001  xxxxxxxx    09.05.2008   1,00
    0000000001   00001  0001  xxxxxxxx    05.05.2008   0,50
    If the user set 13.05.2008 in the filter, the report should return:
    0000000001   00001  0001  xxxxxxxx    12.05.2008   2,00
    If the user set 11.05.2008 in the filter, the report should return:
    0000000001   00001  0001  xxxxxxxx    09.05.2008   1,00
    If the user set 08.05.2008 in the filter, the report should return:
    0000000001   00001  0001  xxxxxxxx    05.05.2008   0,50
    The problem is that the date will be dynamic and I'll need to return just one line for a combination of Company Code, Plant, Material and Cost Type. First of all I would like to know the best way to do it. Do I need to use virtual key figure? If so, anyone could tell me some steps to do it?
    Thanks in advance,
    Helder

    Not Virtual KeyFigure.
    You can achieve this with Restricted KeyFigure with the restriction with CalDay (Variable) which user input while Running the Query. Its more like a Bucket. SAP Delivered Buckets Query is in the below link. Check that query you will understand better about Bucket.
    http://help.sap.com/saphelp_nw04/helpdata/en/40/94af39a3488979e10000000a11402f/content.htm
    If not clear try to Install the BC Query and play with that you will know better.
    Thanks
    Sriram

  • Find the correct max date and then  loop to find 3 consecutive unique no.'s

    Hi, this relates to a similar query I posted however this is my problem in its entirety. here is the link: Keep Dense_Rank problem
    This is what I need to do:
    1) Select the max date in a month per unique Receiver_ID. NOTE: there are special cases such as Due_Date = 01-March-2011 in the sample data. When this occurs, the date with the greatest acc_payment_no must be chosen.
    2) Each Receiver_ID has a corresponding payment_status and acc_payment_no. Payment_status is either 9 or 4.
    3) I have a variable called v_incident_date
    4) I am focused on the Due_Dates that are within 6 months of the v_incident_date ie where Due_Date Between  add_months(v_cla_event.Incident_date, - 6) and v_incident_date5) This is the tricky part. Once I have identified the Due_Dates I am interested in and their corresponding payment_status's, I want to search for THREE CONSECUTIVE payment_Status = 9 and if this is the case return 'Y' else 'N'.
    Here is the sample data:
    CREATE TABLE acc_payment_test_a (Receiver_ID varchar2(50), Due_Date Date, acc_paymnet_no varchar2(50), payment_status varchar2(50));
    CREATE TABLE acc_payment_test_a (Receiver_ID varchar2(50), Due_Date Date, acc_paymnet_no varchar2(50), payment_status varchar2(50));
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('03-MAR-2008 00.00','DD-MON-YYYY HH24:MI'),1083,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('05-MAR-2008 00.00','DD-MON-YYYY HH24:MI'),1084,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-APR-2008 00.00','DD-MON-YYYY HH24:MI'),2762,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAY-2008 00.00','DD-MON-YYYY HH24:MI'),8211,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-JUN-2008 00.00','DD-MON-YYYY HH24:MI'),20144,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUL-2008 00.00','DD-MON-YYYY HH24:MI'),36534,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-AUG-2008 00.00','DD-MON-YYYY HH24:MI'),56661,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-SEP-2008 00.00','DD-MON-YYYY HH24:MI'),78980,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-OCT-2008 00.00','DD-MON-YYYY HH24:MI'),107595,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-NOV-2008 00.00','DD-MON-YYYY HH24:MI'),192768,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-DEC-2008 00.00','DD-MON-YYYY HH24:MI'),223835,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-JAN-2009 00.00','DD-MON-YYYY HH24:MI'),281414,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-FEB-2009 00.00','DD-MON-YYYY HH24:MI'),322990,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-MAR-2009 00.00','DD-MON-YYYY HH24:MI'),364489,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-APR-2009 00.00','DD-MON-YYYY HH24:MI'),417224,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-MAY-2009 00.00','DD-MON-YYYY HH24:MI'),466501,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUN-2009 00.00','DD-MON-YYYY HH24:MI'),523088,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUL-2009 00.00','DD-MON-YYYY HH24:MI'),559526,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-AUG-2009 00.00','DD-MON-YYYY HH24:MI'),619977,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-SEP-2009 00.00','DD-MON-YYYY HH24:MI'),680457,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-OCT-2009 00.00','DD-MON-YYYY HH24:MI'),731996,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-NOV-2009 00.00','DD-MON-YYYY HH24:MI'),789924,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-DEC-2009 00.00','DD-MON-YYYY HH24:MI'),850743,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JAN-2010 00.00','DD-MON-YYYY HH24:MI'),918113,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-FEB-2010 00.00','DD-MON-YYYY HH24:MI'),982257,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAR-2010 00.00','DD-MON-YYYY HH24:MI'),1029219,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-APR-2010 00.00','DD-MON-YYYY HH24:MI'),1103165,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAY-2010 00.00','DD-MON-YYYY HH24:MI'),1173876,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUN-2010 00.00','DD-MON-YYYY HH24:MI'),1241791,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUL-2010 00.00','DD-MON-YYYY HH24:MI'),1316343,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('02-AUG-2010 00.00','DD-MON-YYYY HH24:MI'),1384261,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-SEP-2010 00.00','DD-MON-YYYY HH24:MI'),1467071,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-OCT-2010 00.00','DD-MON-YYYY HH24:MI'),1548259,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-NOV-2010 00.00','DD-MON-YYYY HH24:MI'),1626770,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('22-NOV-2010 00.00','DD-MON-YYYY HH24:MI'),1751476,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('03-JAN-2011 00.00','DD-MON-YYYY HH24:MI'),1824718,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-FEB-2011 00.00','DD-MON-YYYY HH24:MI'),1939968,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAR-2011 00.00','DD-MON-YYYY HH24:MI'),2130326,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAR-2011 00.00','DD-MON-YYYY HH24:MI'),2126550,9);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-MAR-2011 00.00','DD-MON-YYYY HH24:MI'),2033664,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-APR-2011 00.00','DD-MON-YYYY HH24:MI'),2151353,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('03-MAY-2011 00.00','DD-MON-YYYY HH24:MI'),2251549,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUN-2011 00.00','DD-MON-YYYY HH24:MI'),2384223,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-JUL-2011 00.00','DD-MON-YYYY HH24:MI'),2504840,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_paymnet_no, payment_status)  VALUES (151823,to_date('01-AUG-2011 00.00','DD-MON-YYYY HH24:MI'),2615647,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_payment_no, payment_status)  VALUES (151823,to_date('01-SEP-2011 00.00','DD-MON-YYYY HH24:MI'),2756098,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_payment_no, payment_status)  VALUES (151823,to_date('05-SEP-2011 00.00','DD-MON-YYYY HH24:MI'),2789669,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_payment_no, payment_status)  VALUES (151823,to_date('01-OCT-2011 00.00','DD-MON-YYYY HH24:MI'),2944532,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_payment_no, payment_status)  VALUES (151823,to_date('01-NOV-2011 00.00','DD-MON-YYYY HH24:MI'),3056013,4);
    INSERT INTO acc_payment_test_a(Receiver_ID, Due_Date, acc_payment_no, payment_status)  VALUES (151824,to_date('01-DEC-2011 00.00','DD-MON-YYYY HH24:MI'),3230490,4);
    select * from acc_payment_test_aBanner:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production"
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    Edited by: 885178 on Jan 12, 2012 4:38 PM
    I added the acc_payment_no as time_stamp is not reliable

    Here is some sample data with the results I want. Note that I am creating a package and using the variables so v_cla_case_no and v_cla_event.Incident_date so there is no need to join onto the cla_case_table
    This is an example of the package and package body. Cla_case_no and name_id_no are from Cla_case and Incident_date is from cla_event.
    create or replace
    PACKAGE "FIND"
      IS
    v_cla_case                      cla_case%ROWTYPE;
    v_cla_event                     cla_event%ROWTYPE;
    v_score                          NUMBER(10);
    PROCEDURE Find_Factors(p_claim_case_no in number);
    END FIND;
    create or replace
    PACKAGE BODY "FIND" IS
    PROCEDURE Find_Factors(p_claim_case_no in number) IS
    BEGIN
      x_trace ('Find_Factors') ;
      x_trace ('Cla_Case') ;
        select      *
        into     v_cla_case
        from     cla_case
        where cla_case_no = p_claim_case_no;
      x_trace ('Cla_Event') ;
        select      *
        into     v_cla_event
        from    cla_event ce
        where ce.cla_event_no = v_cla_case.cla_event_no;
    --Here is an example of the code I use
    x_trace ('Score') ;
            Select score   
                  into v_score
                   from rbn_itc
                  where newest = 'Y'
                  and customer_no = v_cla_case.name_id_no;
    END Find_Factors;
    END FIND; 
    --DROP TABLE Cla_case
    CREATE TABLE Cla_case (Cla_case_no INT,Name_ID_no int, Incident_Date varchar2(50) );
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (2879,325309,'2008-06-28');
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (3706,227013,'2008-08-02');
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (3806,467693,'2008-08-11');
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (4346,221694,'2008-08-22');
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (4612,221694,'2008-08-29');
    INSERT INTO Cla_case (Cla_case_no ,Name_ID_no , Incident_Date ) VALUES (4870,422711,'2008-09-16');
    select * from Cla_case
    --DROP TABLE Acc_Payments
    CREATE TABLE Acc_Payments (Payment_Status INT,Receiver_ID int, Due_Date varchar2(50));
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,325309,'2008-04-29');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,325309,'2008-05-06');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,325309,'2008-06-02');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,325309,'2008-06-05');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,325309,'2008-07-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,325309,'2008-07-03');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,325309,'2008-07-28');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-05-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-06-02');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-07-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,227013,'2008-07-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-08-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-08-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-09-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-09-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,227013,'2008-10-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-06-07');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-07-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-07-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-08-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,467693,'2008-08-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-09-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-09-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-10-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-10-27');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-11-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-12-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2008-12-15');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2009-01-15');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,467693,'2009-02-16');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,221694,'2008-05-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-06-02');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-07-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-07-10');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-08-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,221694,'2008-08-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-09-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-09-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-10-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-10-27');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-11-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-11-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-12-01');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2008-12-27');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2009-01-02');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,221694,'2009-02-02');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-06-05');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,422711,'2008-06-12');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-07-07');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-07-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-08-11');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-08-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-09-09');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (4,422711,'2008-09-26');
    INSERT INTO Acc_Payments (Payment_Status ,Receiver_ID , Due_Date) VALUES (9,422711,'2008-10-09');
    Select * from Acc_Payments
    --Drop Table Result
    CREATE TABLE Result (Cla_case_no INT,Receiver_ID int, Name_ID_no int, Incident_Date varchar2(50), Desired_Missed varchar(25) );
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (2879,325309,325309,'2008-06-28','N');
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (3706,227013,227013,'2008-08-02','N');
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (3806,467693,467693,'2008-08-11','Y');
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (4346,221694,221694,'2008-08-22','Y');
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (4612,221694,221694,'2008-08-29','N');
    INSERT INTO Result (Cla_case_no ,Receiver_ID , Name_ID_no , Incident_Date, Desired_Missed ) VALUES (4870,422711,422711,'2008-09-16','Y');
    Select * from Result

  • MAX Function not returning MAX

    I have a query that is pulling in EDI 214 status codes, and want to pull in the last received status for status type "AG". To do this, I'm using the MAX function on the INSERT_DATE field of the status code AG, but the query keeps returning both AG status codes. I've tried this in a single query (Query 1) but it did not work so I also attempted it in a much smaller query to be used as a subquery, but that still did not work. Can anyone identify what the issue is with what I'm attempting to do?
    Query 1 (All Inclusive):
    SELECT BS.SHIPMENT_GID AS BUY_SHIPMENT_GID,
    AGSS.EVENTDATE AS AG_EVENT,
    D1SS.EVENTDATE AS D1_EVENT,
    BS.START_TIME AS BUY_START_TIME,
    AGSS.STATUS_CODE_GID AS AG,
    D1SS.STATUS_CODE_GID AS D1,
    BS.DOMAIN_NAME AS BUY_DOMAIN,
    MAX(AGSS.INSERT_DATE) AS AG_INSERT_DATE,
    MAX(D1SS.INSERT_DATE) AS D1_INSERT_DATE,
    BS.START_TIME,
    BS.DOMAIN_NAME,
    SHIPSTAT.STATUS_VALUE_GID
    FROM V_ROD_SHIPMENT BS
    INNER JOIN V_ROD_SS_STATUS_HISTORY AGSH
    ON (BS.SHIPMENT_GID = AGSH.SHIPMENT_GID)
    INNER JOIN V_ROD_IE_SHIPMENTSTATUS AGSS
    ON (AGSH.I_TRANSACTION_NO = AGSS.I_TRANSACTION_NO)
    INNER JOIN V_ROD_SS_STATUS_HISTORY D1SH
    ON (BS.SHIPMENT_GID = D1SH.SHIPMENT_GID)
    INNER JOIN V_ROD_SHIPMENT_STATUS SHIPSTAT
    ON (BS.SHIPMENT_GID = SHIPSTAT.SHIPMENT_GID)
    INNER JOIN V_ROD_IE_SHIPMENTSTATUS D1SS
    ON D1SH.I_TRANSACTION_NO = D1SS.I_TRANSACTION_NO
    WHERE BS.START_TIME > '18/MAY/12'
    AND BS.DOMAIN_NAME = 'UPS/CP/HDMB'
    AND AGSS.STATUS_CODE_GID = 'AG'
    AND D1SS.STATUS_CODE_GID = 'D1'
    AND (SHIPSTAT.STATUS_VALUE_GID = BS.DOMAIN_NAME
    || '.SECURE RESOURCES_ACCEPTED'
    OR SHIPSTAT.STATUS_VALUE_GID = BS.DOMAIN_NAME
    || '.SECURE RESOURCES_PICKUP NOTIFICATION')
    GROUP BY BS.SHIPMENT_GID,
    AGSS.EVENTDATE,
    D1SS.EVENTDATE,
    BS.START_TIME,
    AGSS.STATUS_CODE_GID,
    D1SS.STATUS_CODE_GID,
    BS.DOMAIN_NAME,
    SHIPSTAT.STATUS_VALUE_GID
    Query 2 (to be used as a sub-query if I cannot pull MAX insert date in previous query):
    SELECT DISTINCT BS.SHIPMENT_GID AS BUY_SHIPMENT_GID,
    AGSS.EVENTDATE AS AG_EVENT,
    AGSS.STATUS_CODE_GID AS AG,
    MAX(AGSS.INSERT_DATE) AS AG_INSERT_DATE
    FROM V_ROD_SHIPMENT BS
    INNER JOIN V_ROD_SS_STATUS_HISTORY AGSH
    ON (BS.SHIPMENT_GID = AGSH.SHIPMENT_GID)
    INNER JOIN V_ROD_IE_SHIPMENTSTATUS AGSS
    ON (AGSH.I_TRANSACTION_NO = AGSS.I_TRANSACTION_NO)
    WHERE AGSS.STATUS_CODE_GID = 'AG'
    AND BS.SHIPMENT_GID = 'UPS/CP/HDMB.HDM-1000203768'
    GROUP BY BS.SHIPMENT_GID,
    AGSS.EVENTDATE,
    AGSS.STATUS_CODE_GID
    Results of query 2 (similar issue as query 1, query doesn't return MAX insert date):
    BUY_SHIPMENT_GID     AG_EVENT     AG     AG_INSERT_DATE
    UPS/CP/HDMB.HDM-1000203768     5/25/2012 6:00:00 PM     AG     5/21/2012 3:10:36 PM
    UPS/CP/HDMB.HDM-1000203768     6/1/2012 5:00:00 PM     AG     5/20/2012 2:36:18 PM
    I appreciate any help.
    Thanks,
    -Adam

    Hi, Adam,
    Welcome to the forum!
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    Simplify the problem as much as possible. Remove all tables and columns that play no role in this problem.
    If you can show what the problem is using commonly available tables (such as those in the scott schem) then you don't have to psot any sample data; just the results and the explanation.
    Always say which version of Oracle you're using.
    See the forum FAQ {message:id=9360002}
    MAX (insert_date) returns the latest insert_date. I think, in this problem, you don't really want the latest insert_date; you want the status code that's related to the last insert_date. One way to get that is the aggregate FIRST (or LAST) function.
    Consider this query, using the scott.emp table:
    SELECT       ename
    ,       hiredate
    FROM       scott.emp
    ORDER BY  hiredate
    ,            ename
    ;Output:
    ENAME      HIREDATE
    SMITH      17-Dec-1980
    ALLEN      20-Feb-1981
    WARD       22-Feb-1981
    JONES      02-Apr-1981
    BLAKE      01-May-1981
    CLARK      09-Jun-1981
    TURNER     08-Sep-1981
    MARTIN     28-Sep-1981
    KING       17-Nov-1981
    FORD       03-Dec-1981
    JAMES      03-Dec-1981
    MILLER     23-Jan-1982
    SCOTT      19-Apr-1987
    ADAMS      23-May-1987Say we're only interested in seeing the last hiredate, and the name of the person hired on that date:
    LAST_ENAME LAST_HIREDA
    ADAMS      23-May-1987Here's how to get those results using the aggregate LAST function:
    SELECT       MIN (ename) KEEP (DENSE_RANK LAST ORDER BY hiredate) AS last_ename
    ,       MAX (hiredate)                                              AS last_hiredate
    FROM       scott.emp
    ;What if there's a tie for the latest hiredate? For example, say we're only looking at people hired before 1982. In that case, the latest hiredate is December 3, 1981, and there happen to be two people hired on that date. This query
    SELECT       MIN (ename) KEEP (DENSE_RANK LAST ORDER BY hiredate) AS last_ename
    ,       MAX (hiredate)                                              AS last_hiredate
    FROM       scott.emp
    WHERE         hiredate     < DATE '1982-01-01'
    ;produces only 1 row of output:
    LAST_ENAME LAST_HIREDA
    FORD       03-Dec-1981Why did it show FORD rather than JAMES? Because of the MIN function. When there happens to be a tie for the latest hiredate, MIN says to return the first ename (in normal sort order) of the rows that have that hiredate.
    FIRST and LAST work with GROUP BY, too.
    In the example above, we were only looking at one column related to the latest hiredate. If we neede to see several columns, it would be simpler to use the analytic ROW_NUMBER function:
    WITH     got_r_num     AS
         SELECT  emp.*
         ,     ROW_NUMBER () OVER ( ORDER BY  hiredate  DESC
                                   ,            ename
                           ) AS r_num
         FROM    scott.emp
         WHERE     hiredate     < DATE '1982-01-01'
    SELECT     *
    FROM     got_r_num
    WHERE     r_num     = 1
    I hope this answers your question.
    If not, post a more complete explanation of what you want to do. if you have to use your own tables, then post CREATE TABLE and INSERT statements for a little sample data. Post the results you want from that data, and explain how you get those results from that data.

  • Max (date) in sub-query

    I have an issue with a big view and several queries running on it where the max (date) is required in the sub-select (example below). When I run traces and explain, the perf problem is when the where cluase is evaluated, the view is pulled second time - so I basically use this view twice. The fact that the date in the underlined table is indexed did not help.
    I tried to use "ROW_NUMBER() OVER (ORDER BY date ASC) AS row_number, " rather to select the max(date) in teh sub query but I get only 1 records returned by the query instead the expected about 25,000.
    Need help on how to correct the max(date) to perform better and also to understand how to correct the ROW_NUMBER() not to limit me to 1 row.
    Thanks a lot for the help, Tom
    create view test_date as select * from user_objects;
    select * --(I have specific columns in the real query)
    from test_date t1
    where LAST_DDL_TIME in
    (select max(LAST_DDL_TIME)
    from test_date t2
    where t1.OBJECT_ID=t2.OBJECT_ID --(more clauses to limit the result but not reflecting the problem)
    and t1.OBJECT_NAME =t2.OBJECT_NAME
    and OBJECT_TYPE not in('TABLE')
    group by t2.OBJECT_ID , t2.OBJECT_NAME )

    I got the query to run - with dense ranck and row-Number I get the same results. My partition by cluse was wrong. But the perfromance is not much better.
    Any other idea on how to optimize a query that uses pattern like below and needs to return all rows from a table that have the max enter date.
    Thanks a lot, tom
    Example:
    col1 col2 col3
    1 sysdate 'I need this record'
    2 sysdate 'I need this record'
    3 sysdate 'I need this record'
    4 sysdate 'I do NOT need this record'
    5 sysdate 'I do NOT need this record'
    5 sysdate+3 'I need this record'
    5 sysdate+2 'I do NOT need this record'
    4 sysdate+3 'I need this record'
    4 sysdate+2 'I do NOT need this record'
    4 sysdate+1 'I do NOT need this record'
    My query corrently is using the following construcion:
    select col1, col2 , col3
    from test_table t
    where 1,2,3
    and col3 = (select max(col3) from test_table t2 where t2.col1 = t.col1) group by t2.col1

  • Need help regarding complex calculation using Max value and limiting data after Max date in MDX

    I am working on a bit complex calculated measure in SSAS cube script mode.
    Scenario /Data Set
    Date
    A
    B
    C
    A+B
    5/29/2014
    Null
    34
    Null
    34
    6/30/2014
    Null
    23
    45
    68
    7/15/2014
    25
    -25
    Null
    0
    8/20/2014
    -34
    Null
    Null
    -34
    9/30/2014
    25
    Null
    60
    25
    10/15/2014
    45
    -45
    Null
    0
    11/20/2014
    7
    8
    Null
    15
    a) Need to capture latest non-null value of Column C based on date
    with above example it should be 60 as of 9/30/2014
    b) Need to capture column A+B for all dates.
    c) Add values from column (A+B) only after latest date which is after 9/30/2014. 
    with above example it's last 2 rows and sum is 15
    d) Finally add value from step a and step c. which means the calc measure value should be = 75
    I need to perform all this logic in MDX. I was able to successfully get step a and b in separate calc measure, however i am not sure how to limit the scope based on certain date criteria. In this case it's, date> Max date(9/30/2014) . Also how should
    i add calculated members and regular members?
    I was able to get max value of C based on date and max date to limit the scope.
    CREATE MEMBER CURRENTCUBE.[Measures].[LatestC] AS
    TAIL( 
      NONEMPTY(
        [Date].[Date].CHILDREN*[Measures].[C]),1).ITEM(0) ,visible=1;
    CREATE MEMBER CURRENTCUBE.[Measures].[MaxDateofC] AS
    TAIL( 
      NONEMPTY(
        [Date].[Date].CHILDREN,[Measures].[C]),1).ITEM(0).MemberValue ,visible=1;
    Please help with Scope statement to limit the aggregation of A+B for dates > MaxDateofC? Also further how to add this aggregation value to LatestC calc measure?
    Thank You

    Hi Peddi,
    I gave TRUNC to both of the dates. But still the same issue. I think the problem is in returning the BolbDomain.
    return blobDomain;
    } catch (XDOException xdoe) {
    System.out.println("Exception in XDO :");
    throw new OAException("Exception in XDO : "+xdoe.getMessage());
    catch (SQLException sqle) {
    System.out.println("Exception in SQL :");
    throw new OAException("SQL Exception : "+sqle.getMessage());
    catch (OAException e) {
    System.out.println("Exception in OA :");
    throw new OAException("Unexpected Error :: " +e.getMessage());
    Thanks and Regards,
    Myvizhi

  • Select max date from a table with multiple records

    I need help writing an SQL to select max date from a table with multiple records.
    Here's the scenario. There are multiple SA_IDs repeated with various EFFDT (dates). I want to retrieve the most recent effective date so that the SA_ID is unique. Looks simple, but I can't figure this out. Please help.
    SA_ID CHAR_TYPE_CD EFFDT CHAR_VAL
    0000651005 BASE 15-AUG-07 YES
    0000651005 BASE 13-NOV-09 NO
    0010973671 BASE 20-MAR-08 YES
    0010973671 BASE 18-JUN-10 NO

    Hi,
    Welcome to the forum!
    Whenever you have a question, post a little sample data in a form that people can use to re-create the problem and test their ideas.
    For example:
    CREATE TABLE     table_x
    (     sa_id          NUMBER (10)
    ,     char_type     VARCHAR2 (10)
    ,     effdt          DATE
    ,     char_val     VARCHAR2 (10)
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0000651005, 'BASE',    TO_DATE ('15-AUG-2007', 'DD-MON-YYYY'), 'YES');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0000651005, 'BASE',    TO_DATE ('13-NOV-2009', 'DD-MON-YYYY'), 'NO');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0010973671, 'BASE',    TO_DATE ('20-MAR-2008', 'DD-MON-YYYY'), 'YES');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0010973671, 'BASE',    TO_DATE ('18-JUN-2010', 'DD-MON-YYYY'), 'NO');
    COMMIT;Also, post the results that you want from that data. I'm not certain, but I think you want these results:
    `    SA_ID LAST_EFFD
        651005 13-NOV-09
      10973671 18-JUN-10That is, the latest effdt for each distinct sa_id.
    Here's how to get those results:
    SELECT    sa_id
    ,         MAX (effdt)    AS last_effdt
    FROM      table_x
    GROUP BY  sa_id
    ;

  • Get articles with max date only

    Hello Everyone
    I've got a little problem with a sql query. I got two tables and a lot of articles. The articles are listed multiple times and got different dates. I want to select the following rows:
    p.productname
    i.amount
    i.date
    The problem is, that every product only should be listed one time (something like UNIC or DISTINCT). And it should be the product with the highest date. Is there something like MAX(date) that I can use?
    What I already have is...
    SELECT
        p.productname,
        i.amount,
        i.date
    FROM op_inventory i
    LEFT JOIN products p
    ON p.ItemID = i.fk_article
    Now, how can I solve my problem above?...
    Greets Dollique

    Hmm, thx but, that didn't solve my problem.
    I've just found a german website with a detailed explantion of the solution that mack gave me.
    I used that one and it seems to work quite fine, expects from I somehow can't use GROUP BY. Every time I use it, it returns a Executing DB error (like before)...
    I'll see if I can solve that problem...
    Thx you anyway!
    Toby
    PS:
    My code now is
    SELECT
        i.id,
        i.amount,
        i.date,
        p.productname
    FROM op_inventory i
    LEFT JOIN products p
    ON p.ItemID = i.fk_article
    <!---JOIN (
        SELECT
            id id_m,
            MAX(date) maxdate
        FROM op_inventory
        GROUP BY id_m
    ) temp
    ON i.id = temp.id_m AND i.date = temp.maxdate--->
    <cfoutput>#gi_where#</cfoutput>
    i.date = (
        SELECT MAX(o.date)
        FROM op_inventory o
        WHERE o.fk_article = i.fk_article
    <!---GROUP BY i.id--->
    And the content of #gi_where# is:
    WHERE p.category = #lb_group# AND

  • Discoverer report returns no data from a certain time

    Using Discoverer 10g,a discoverer report which returned data normally last month gets to return no data now.
    We have four same environments, and two of them has this problem, two is OK.
    And the SQL of the discoverer of the four environments are the same.
    We have no any changment of this discoverer and related EUL for more than one year....
    How should we investigate into this issue.
    For example ,a point we should notice or something...
    Could somebody give us a suggestion?
    Thank you.

    Thanks for your qiuck reply.
    1.empty table that is joined in the query.The four environments has almost the same data,it is not the cause.
    2.security issue with the data, maybe the security definitions are different from one environment to another.we are now invesgate into this cause.
    and there is a sql of the discoverer's EUL which shows no data in a enviroment(in this enviroment discoverer report gets no data), but in another enviroment data can show.
    the sql is as following.
    =============
    SELECT loc_bu.org_id
    ,loc_bu.location_id building_id
    ,loc_bu.building building_name
    ,loc_bu.location_code building_number
    ,loc_fl.location_id floor_id
    ,loc_fl.location_code floor_number
    ,loc_fl.floor floor_name
    ,loc_of.location_id office_id
    ,loc_of.location_code office_number
    ,loc_of.office office_name
    ,loc_of.suite office_suite
    ,loc_of.location_alias office_alias
    ,loc_of.assignable_area office_assignable_area
    ,loc_of.space_type_lookup_code office_space_type_code
    ,lst.meaning office_space_type
    ,loc_of.function_type_lookup_code office_function_type_code
    ,fun.meaning office_function_type
    ,(SELECT ffv.description
    FROM fnd_flex_values_vl ffv
    ,fnd_flex_value_sets ffvs
    WHERE ffv.flex_value = loc_of.attribute1
    AND ffv.flex_value_set_id = ffvs.flex_value_set_id
    AND ffvs.flex_value_set_name = 'MB_PN_ON') occupancy_exception_flag
    ,loc_of.active_start_date
    ,loc_of.active_end_date
    --ADD BY KEVIN 2008/6/26 START
    ,loc_of.attribute7 division_code_office
    ,(SELECT ffv.description
    FROM fnd_flex_values_vl ffv
    ,fnd_flex_value_sets ffvs
    WHERE ffv.flex_value = loc_of.attribute7
    AND ffv.flex_value_set_id = ffvs.flex_value_set_id
    AND ffvs.flex_value_set_name = 'MB_PN_DIVISION') division_description_office --ヌヨ-ホ・ヒオテ・
    --ADD BY KEVIN 2008/6/26 END 
    FROM pn_locations loc_bu
    ,pn_locations loc_fl
    ,pn_locations loc_of
    ,fnd_lookups fun
    ,fnd_lookups lst
    WHERE loc_bu.location_id = loc_fl.parent_location_id
    AND loc_bu.location_type_lookup_code IN ('LAND', 'BUILDING')
    AND nvl(loc_bu.attribute6, '99') <> '01'
    AND loc_fl.location_id = loc_of.parent_location_id
    AND loc_fl.location_type_lookup_code IN ('FLOOR', 'PARCEL')
    AND nvl(loc_fl.attribute6, '99') <> '01'
    AND loc_of.location_type_lookup_code IN ('OFFICE', 'SECTION')
    AND nvl(loc_of.attribute6, '99') <> '01'
    AND loc_of.function_type_lookup_code = fun.lookup_code(+)
    AND fun.lookup_type(+) = 'PN_FUNCTION_TYPE'
    AND loc_of.space_type_lookup_code = lst.lookup_code(+)
    AND lst.lookup_type(+) = decode(loc_of.location_type_lookup_code,
    'OFFICE',
    'PN_SPACE_TYPE',
    'SECTION',
    'PN_PARCEL_TYPE')
    AND nvl(loc_of.space_type_lookup_code,'99') <> '07';
    ====================
    Ps.before excute this sql, we always first excute following command.
    ====
    begin
    fnd_client_info.set_org_context(117);
    end;
    ====
    The analyst of our team is not very good at the security problem, could you help us?
    Thanks a lot.

  • Return default data if no data is returned by sql query

    Hi,
    I have a requirement.
    I have an inner query that returns no data. However I still want to return default data (say 'abc'). The below sql does not return anything inspite of using nvl . Please advise .
    select nvl( x.vencode,'abc') vencode
    from
    -- below sql returns no data!
              SELECT a.vencode vencode,
                     a.mid mid,
                     a.title title,
                      ROW_NUMBER() OVER (PARTITION BY a.vencode ORDER BY a.title) rnk
               FROM (
                     SELECT *
                     FROM    (SELECT vencode FROM vendor where login is not null and vencode = 'BKFI'
                                        and login > (sysdate - 90) )
                          CROSS JOIN
                             (SELECT mid, title, ROWNUM num FROM tcommmemos where mid is not null)
                    ) a,        
                   (SELECT * FROM (
                    SELECT v.vencode, tc.mid, tc.title
               FROM
               (select  * from vendor
                     where login is not null and vencode = 'BKFI'
                     and login > (sysdate - 90)) v,
                tcommmemosviewed tcv, tcommmemos tc
               WHERE     v.vencode = tcv.vencode
                     and tc.mid is not null
                     --AND tc.post_date > v.hiredate
                     AND tc.mid = tcv.mid)
                   ) b
                   where a.vencode = b.vencode(+)
                     and a.title   = b.title(+)
                     and a.mid     = b.mid(+)
                     and b.mid is null                               
    ) x              

    Use any aggregate function it'll always return a value even if there no data in the table
    create table r_dummy_1 (a number);
    SQL> select * from r_dummy_1;
    no rows selected
    SQL> select nvl(max(a),1) from r_dummy_1;
    NVL(MAX(A),1)
    1

  • Problem getting correct data from MS Access after doing an Update

    Hi all,
    I have a problem getting correct data after doing an update. This is the scenario
    I am selecting some(Eg: All records where Column X = �7� ) records and update a column with a particular value (SET Column X = �c� ) in all these records by going through a while loop. In the while loop I add these records to a vector too, and pass it as the return value.
    After getting this return value I go through a for loop and get each record one by one from the DB and check if my previous update has actually happened. Since No errors were caught while updating DB, I assume all records are updated properly but my record set shows one after another as if it has not been updated. But if I open the DB it is actually updated.
    This does not happen to all records, always it shows like this
    1st record     Mode = �c�
    2nd record     Mode = �7�
    3st record     Mode = �c�
    4nd record     Mode = �7�
    9th record     Mode = �c�
    10th record     Mode = �7�
    I am relatively new to java and this is someone elses code that I have to modify,So I am not sure if there some thing wrong in the code too
    //Here is the method that gets records and call to update and add to vector
    public static Vector getCanceledWorkOrders() throws CSDDBException{
    //Variable declaration
      try {
        objDBConn = DBHandler.getCSDBCon();
        strSQL  = "SELECT bal bla WHERE [Detailed Mode])=?)";
        objStmt = objDBConn.prepareStatement(strSQL);   
        objStmt.setString(1, '7');
        objWOPRs = objStmt.executeQuery();
        while (objWOPRs.next()) {
         //Add elements to a vector by getting from result set
          //updating each record as PROCESSING_CANCELLED_WO(c)
          iRetVal = WorkOrderDetailingPolicy.updateRecordStatus(objPWODP.iWorkOrderNumber, objPWODP.strPersonInformed, EMSConstants.PROCESSING_CANCELLED_WO);
          if (iRetVal == -1) {
            throw new NewException("Updating failed");
      catch (Exception e) {
        vecWONumbers = null;
        throw new CSDDBException(e.getMessage());
      }finally{
        try {
          objWOPRs.close();
          objStmt.close();
          DBHandler.releaseCSDBCon(objDBConn);
        catch (Exception ex) {}
      //return vector
    //here is the code that actually updates the records
    public static int updateRecordStatus(int iWONumber, String strPerInformed , String strStatus) throws CSDDBException{
       PreparedStatement objStmt = null;
       Connection objDBConn  = null;
       String strSQL = null;
       int iRetVal = -1;
       try{
         objDBConn  = DBHandler.getCSDBCon();
         objDBConn.setAutoCommit(false);
         strSQL = "UPDATE Table SET [Detailed Mode] = ? WHERE bla bla";
         objStmt = objDBConn.prepareStatement(strSQL);
         objStmt.setString(1, strStatus);    
         objStmt.execute();
         objDBConn.commit();
         iRetVal = 1;
       }catch(Exception e){
         iRetVal = -1;
       }finally{
         try{
           objStmt.close();
           DBHandler.releaseCSDBCon(objDBConn);
         }catch(Exception ex){}
       return iRetVal;
    //Here is the code that call the records again
      public static WorkOrderDetailingPolicy getWorkOrders(int iWorkOrderNo) throws CSDDBException{
        Connection objDBConn = null;
        PreparedStatement objStmt = null;
        ResultSet objWOPRs = null;
        WorkOrderDetailingPolicy objPWODP = null;
        String strSQL = null;
        try {
          objDBConn = DBHandler.getCSDBCon();    
          strSQL = "SELECT * FROM [Work Order Detailing] WHERE [Work Order No] = ?";
          objStmt = objDBConn.prepareStatement(strSQL);
          objStmt.setInt(1, iWorkOrderNo);
           objWOPRs = objStmt.executeQuery();
          if (objWOPRs.next()) {
            objPWODP = new WorkOrderDetailingPolicy();
            objPWODP.iWorkOrderNumber = objWOPRs.getInt("Work Order No");
            //......Get Record values
        catch (Exception e) {
          objPWODP = null;
          throw new CSDDBException(e.getMessage());
        }finally{
          try {
            objWOPRs.close();
            objStmt.close();
            DBHandler.releaseCSDBCon(objDBConn);
          catch (Exception ex) {}
        return objPWODP;
      }

    Hello,
    Can you put an example of your problem online?
    Are you sure you're not having problems with case sensitive data?
    Thanks,
    Dimitri

  • SSRS - Oracle Stored procedure returns no data but does in SQL Developer Sudio

    HI there,
    Stored procedure returns no data when executed on the report but when i execute the stored procedure in Sql Developer it returns required rows.
    Thanks for your help!

    Hi Simon,
    When i test with simple query, i get the data.
    For your convenience, my stored proc looks lyk :
    PROCEDURE pr_REPORT_data(P_STARTDATE IN DATE, P_ENDDATE IN DATE, data_rows OUT T_CURSOR) AS 
    OPEN completed_Reinstatement FOR
      SELECT 
                 value1,.......value5
      FROM table1
    WHERE
        To_Date(createdDate, 'YYYY/MM/DD') BETWEEN To_Date(P_STARTDATE, 'YYY/MM/DD') AND To_Date(P_ENDDATE, 'YYYY/MM/DD');
    END pr_REPORT_data;          
    T_CURSOR is of type cursor which is declared on the package.
    I'm assuming the problem is with date parameters, however i converted the date before passing to
    WHERE clause. 

  • Report with two Command is empty if one of the two commands returns no data

    Hi all,
    I have a report with two Commands not linked together.
    If ONLY one of the two Commands returns no data, the full report is empty (although the other Command returns data).
    I'm using Crystal Report 2008 and the CRJ 12.2.205
    Have an idea?

    Hi Ted,
    how can I solve the problem, please? It is important.
    If I can help yourself, the problem is appeared in many reports since I updated the library (the old library version 11.8.4.1094 works fine with all). I'm waiting for your answer, please.
    Thank you very much.

  • CSV spreadsheet returning "no data found"

    Hi,
    In one of the reports, I enabled CSV Output so the report can be downloaded.
    When I download it, using Excel 2000, I have no problem, I can see all the right data.
    However, one of my user, who uses Excel 2002, when saves the file and opens it, returns "no data found" on screen.
    Is this an Excel issue? Is Apex not compatible with Excel 2002? Please advise.
    Thanks!

    Do you have any items on your report page, which you use as a condition for the report? Eventually, you need to compute those items on load. If I remember correctly that was the problem I had before.
    Denes Kubicek

Maybe you are looking for

  • Airport Extreme stops screen sharing

    Hi All, I recently purchased an Airport Extreme to replace the existing D-Link WiFi router I had which was pretty unreliable (needed restarting at least daily). Everything is set up and working fine except for screen sharing to a headless mac mini I

  • Stuck at Mac OS X

    After the upgrade of 10.4.9 combo, my PowerBook G4 cant start up. I have did the archieve and install, did the repair disk, but still cant get thru the Mac OS X screen. Any idea please. thanks

  • IPhone 4 cases compatible with 4S?

    Please no stupid responses I know they are the same outside shell but I am just making sure they are the exact same shell and no new slight placement of the buttons or something. Also, are there specific cases depending on which carrier you have beca

  • Change rpool dataset name

    Can I change the dataset name for a root pool? When I built the system I modified the default dataset name to something less confusing. Now it need to change and rebuilding the system is not an option. Is this possible w/o damaging the system? Thanks

  • Ad Hoc quering

    Hi!I am a new user of Hyperion performance suit. How do I specify my queries to define new reports? I am not really aware of databases..