| |
|
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.
|
 |

08-01-07, 11:18
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 51
|
|
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)
|
|

08-01-07, 11:46
|
|
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
|
|

08-01-07, 11:49
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
|
|
note: should probably be UNION ALL instead of UNION
|
|

08-01-07, 15:56
|
|
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)
|
|

08-01-07, 16:00
|
|
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
|
|

08-02-07, 03:52
|
|
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
|
|

08-02-07, 04:59
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
gee, stolze, that looks a lot like post #5
|
|

08-03-07, 03:52
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|