Home » SQL & PL/SQL » SQL & PL/SQL » Show the total of records with last record (11.2.0.1.0)
Show the total of records with last record [message #638140] Thu, 04 June 2015 03:40 Go to next message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Hi All,

I have the following tables:

create table TRY_date_detail
  (
  item_date date,
  item_order number
  );

create table TRY_date_master
  (
  master_date date,
  date_amount number
  );

insert all
  INTO TRY_date_detail values (to_date('01-01-2011','DD-MM-YYYY'), 1)
  INTO TRY_date_detail values (to_date('01-01-2011','DD-MM-YYYY'), 2)
  INTO TRY_date_detail values (to_date('01-01-2011','DD-MM-YYYY'), 3)
  INTO TRY_date_detail values (to_date('01-01-2011','DD-MM-YYYY'), 4)
  INTO TRY_date_detail values (to_date('01-01-2011','DD-MM-YYYY'), 5)
    INTO TRY_date_master values (to_date('01-01-2011','DD-MM-YYYY'), 6432)
    INTO TRY_date_master values (to_date('01-01-2011','DD-MM-YYYY'), 1111)
  INTO TRY_date_detail values (to_date('11-08-2012','DD-MM-YYYY'), 11)
  INTO TRY_date_detail values (to_date('11-08-2012','DD-MM-YYYY'), 12)
  INTO TRY_date_detail values (to_date('11-08-2012','DD-MM-YYYY'), 13)
  INTO TRY_date_detail values (to_date('11-08-2012','DD-MM-YYYY'), 14)
    INTO TRY_date_master values (to_date('11-08-2012','DD-MM-YYYY'), 8913)
  INTO TRY_date_detail values (to_date('09-03-2014','DD-MM-YYYY'), 21)
  INTO TRY_date_detail values (to_date('09-03-2014','DD-MM-YYYY'), 22)
  INTO TRY_date_detail values (to_date('09-03-2014','DD-MM-YYYY'), 23)
    INTO TRY_date_master values (to_date('09-03-2014','DD-MM-YYYY'), 1234)
  INTO TRY_date_detail values (to_date('11-03-2014','DD-MM-YYYY'), 33)
select * from dual;
 

-- My query is:
SELECT ITEM_DATE, ITEM_ORDER, NULL DATE_AMOUNT
  FROM TRY_date_detail
  WHERE ITEM_DATE IN (SELECT DISTINCT master_date FROM TRY_date_master)
UNION
SELECT MASTER_DATE, NULL, SUM(DATE_AMOUNT)
  FROM TRY_DATE_MASTER
  GROUP BY MASTER_DATE;

 

--My query result:
ITEM_DATE           ITEM_ORDER    DATE_AMOUNT
01-Jan-11 12:00:00 AM    1    (null)
01-Jan-11 12:00:00 AM    2    (null)
01-Jan-11 12:00:00 AM    3    (null)
01-Jan-11 12:00:00 AM    4    (null)
01-Jan-11 12:00:00 AM    5    (null)
01-Jan-11 12:00:00 AM    (null)    7543
11-Aug-12 12:00:00 AM    11    (null)
11-Aug-12 12:00:00 AM    12    (null)
11-Aug-12 12:00:00 AM    13    (null)
11-Aug-12 12:00:00 AM    14    (null)
11-Aug-12 12:00:00 AM    (null)    8913
09-Mar-14 12:00:00 AM    21    (null)
09-Mar-14 12:00:00 AM    22    (null)
09-Mar-14 12:00:00 AM    23    (null)
09-Mar-14 12:00:00 AM    (null)    1234

 

-- What I need is to display the Date_amount with the last line in each date as follows:

ITEM_DATE            ITEM_ORDER    DATE_AMOUNT
01-Jan-11 12:00:00 AM    1    (null)
01-Jan-11 12:00:00 AM    2    (null)
01-Jan-11 12:00:00 AM    3    (null)
01-Jan-11 12:00:00 AM    4    (null)
01-Jan-11 12:00:00 AM    5    7543   
11-Aug-12 12:00:00 AM    11    (null)
11-Aug-12 12:00:00 AM    12    (null)
11-Aug-12 12:00:00 AM    13    (null)
11-Aug-12 12:00:00 AM    14    8913   
09-Mar-14 12:00:00 AM    21    (null)
09-Mar-14 12:00:00 AM    22    (null)
09-Mar-14 12:00:00 AM    23    1234



Thanks,
Ferro

[Updated on: Thu, 04 June 2015 03:42]

Report message to a moderator

Re: Show the total of records with last record [message #638142 is a reply to message #638140] Thu, 04 June 2015 03:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select item_date, item_order,
  2         case when item_order = max(item_order) over (partition by item_date) then
  3            (select sum(date_amount) from TRY_date_master m where m.master_date=d.item_date)
  4         end date_amount
  5  from TRY_date_detail d
  6  order by item_date, item_order
  7  /
ITEM_DATE           ITEM_ORDER DATE_AMOUNT
------------------- ---------- -----------
01/01/2011 00:00:00          1
01/01/2011 00:00:00          2
01/01/2011 00:00:00          3
01/01/2011 00:00:00          4
01/01/2011 00:00:00          5        7543
11/08/2012 00:00:00         11
11/08/2012 00:00:00         12
11/08/2012 00:00:00         13
11/08/2012 00:00:00         14        8913
09/03/2014 00:00:00         21
09/03/2014 00:00:00         22
09/03/2014 00:00:00         23        1234
11/03/2014 00:00:00         33

13 rows selected.

Re: Show the total of records with last record [message #638143 is a reply to message #638140] Thu, 04 June 2015 04:10 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
SQL> WITH t1 AS
  2    (SELECT MASTER_DATE,
  3      SUM(DATE_AMOUNT) amt
  4    FROM TRY_DATE_MASTER
  5    GROUP BY MASTER_DATE
  6    ),
  7    t2 AS
  8    (SELECT a.*,
  9      row_number() over(partition BY item_date order by item_order DESC) rn
 10    FROM TRY_date_detail A
 11    ORDER BY item_order
 12    )
 13  SELECT t2.item_date,
 14    t2.item_order,
 15    DECODE(t2.rn, 1, t1.amt, NULL)
 16  FROM t1,
 17    t2
 18  WHERE t1.master_date=t2.item_date
 19  /

ITEM_DATE ITEM_ORDER DECODE(T2.RN,1,T1.AMT,NULL)
--------- ---------- ---------------------------
01-JAN-11          1
01-JAN-11          2
01-JAN-11          3
01-JAN-11          4
01-JAN-11          5                        7543
11-AUG-12         11
11-AUG-12         12
11-AUG-12         13
11-AUG-12         14                        8913
09-MAR-14         21
09-MAR-14         22
09-MAR-14         23                        1234

12 rows selected.

SQL>
Re: Show the total of records with last record [message #638145 is a reply to message #638142] Thu, 04 June 2015 04:13 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel,

I think your output is wrong since the join on the date column is missing. That's why there is an extra row for unmatched date.
Re: Show the total of records with last record [message #638146 is a reply to message #638145] Thu, 04 June 2015 04:35 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
While the OPs expected output skips the 11 Mar data it's not clear from the description whether this is intentional or an oversight. OP will have to confirm which is correct.
Re: Show the total of records with last record [message #638147 is a reply to message #638146] Thu, 04 June 2015 04:39 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That said, a lack of data for 11 Mar in the master table makes me think Lalit has got it right here.
Re: Show the total of records with last record [message #638148 is a reply to message #638145] Thu, 04 June 2015 04:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Lalit Kumar B wrote on Thu, 04 June 2015 11:13
Michel,

I think your output is wrong since the join on the date column is missing. That's why there is an extra row for unmatched date.


I don't understand, there is "where m.master_date=d.item_date", is this not a join on the date column?
The extra row come from the fact there is one row with this in detail and not in master (I think an error in the test case).

If erroneous lines (those without master) must not be retrieved then just add a where condition to remove them from the result.

Re: Show the total of records with last record [message #638151 is a reply to message #638148] Thu, 04 June 2015 05:35 Go to previous message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Thanks you Michel and Lalit, in fact the extra line (detail without master) is not needed. I mean the line is correct and should be member of the detail table, however it is not needed in the result as per Lalit.

[Updated on: Thu, 04 June 2015 07:20]

Report message to a moderator

Previous Topic: compare columns in two tables and trigger something when the condn fails
Next Topic: How to get recursive result
Goto Forum:
  


Current Time: Fri Apr 26 19:02:48 CDT 2024