If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > selection with unit

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-01-07, 11:18
annamaria annamaria is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Smile selection with unit

I have this query:

select
code, date, sum(amount)
from tablemovnew
group by code,date
unit
select
code, date, sum(amount)
from tablemovold
group by code,date

The result yields two records for each code: one records has the total of the amounts of the first table, and the second record has the total of the amounts of the second table.
Now I would like to obtain a single record for every code, with the sum of the two sums (of amounts).
I tried by making a selection in which I nest the afore mentioned query, but
unsuccessfully.
What can I do?
Anna - Verona (Italy)
Reply With Quote
  #2 (permalink)  
Old 08-01-07, 11:46
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Simple, use a common table expression:

with t1 (code, date, summation) as (
select
code, date, sum(amount)
from tablemovnew
group by code,date
UNION
select
code, date, sum(amount)
from tablemovold
group by code,date
) select code,date,sum(summation) from t1 group by code,date

Andy
Reply With Quote
  #3 (permalink)  
Old 08-01-07, 11:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
note: should probably be UNION ALL instead of UNION
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 08-01-07, 15:56
annamaria annamaria is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Quote:
Originally Posted by ARWinner
Simple, use a common table expression:
with t1 (code, date, summation) as (
select
code, date, sum(amount)
from tablemovnew
group by code,date
UNION
select
code, date, sum(amount)
from tablemovold
group by code,date
) select code,date,sum(summation) from t1 group by code,date
Andy
Excuse me (I'm not at the office at the moment and I don't have QMF here at home), shall I write exactly what you indicated? I mean shall I begin the query with 'with t1 (code,.....'
Thank you.
Anna - Verona (Italy)
Reply With Quote
  #5 (permalink)  
Old 08-01-07, 16:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, WITH is the first word in the query

alternatively (since the CTE occurs only once), you can do this --
Code:
select code, date, sum(summation) as overall_sum
  from (
       select code, date, sum(amount) as summation
       from tablemovnew
       group by code, date
       UNION
       select code, date, sum(amount)
       from tablemovold
       group by code, date
       ) as t1
group 
    by code, date
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 08-02-07, 03:52
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by annamaria
I have this query:
The result yields two records for each code: one records has the total of the amounts of the first table, and the second record has the total of the amounts of the second table.
Now I would like to obtain a single record for every code, with the sum of the two sums (of amounts).
I tried by making a selection in which I nest the afore mentioned query, but
unsuccessfully.
Andy already gave you one solution. I just would like to add this:

Everything in SQL are tables. That's the most basic principle.

The result of your two queries are tables where each table has a single row. Unioning both tables via UNION [ALL] also yields a table. Thus, your question comes down to how to combine/sum values from a column of a table. As you know, you can use the SUM() column function (aka aggregate function) for that.

Btw, besides CTEs you can also use subselects for that:
Code:
SELECT t.code, t.date, SUM(t.amount)
FROM   ( SELECT code, date, SUM(amount)
         FROM   tablemovnew
         GROUP BY code, date
         UNION ALL
         SELECT code, date, SUM(amount)
         FROM   tablemovold
         GROUP BY code, date ) AS t(code, date, amount)
GROUP BY t.code, t.date
You can see that the subselect in the FROM clause is exactly your query. I just wrapped another SELECT statement around it to apply the additional aggregation step.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 08-02-07, 04:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
gee, stolze, that looks a lot like post #5
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 08-03-07, 03:52
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Err... right. That happens when you don't read all the answers first. ;-)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On