|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Join two sum queries
hallo
i've seen similar topics like this but i didn't find what i was looking for. i have several queries summing up values with different criteria from the same table, how can i join them? q1: SELECT pk, SUM(hours) FROM table WHERE year = '2003' GROUP BY pk q2: SELECT pk, SUM(hours) FROM table WHERE year = '2004' GROUP BY pk note: table and pk/hours field are the same in both queries, pk values can vary from year to year (in 2004 some may have been added, some deleted). How can i deal up with a join n:m between the two queries? |
|
#2
|
|||
|
|||
|
Just join them like anything else, but you should name the calculated columns:
q1: SELECT pk, SUM(hours) AS hours ... q2: SELECT pk, SUM(hours) AS hours ... Now simply join them: SELECT q1.pk, q1.hours AS hours2003, q2.hours AS hours2004 FROM q1, q2 WHERE q1.pk = q2.pk and replace q1,q2 in the from clause by subselects and give them some name to which you can refer: Code:
SELECT a.pk, a.hours AS hours2003, b.hours AS hours2004 FROM
(SELECT pk, SUM(hours) AS hours FROM ... WHERE ...) a,
(SELECT pk, SUM(hours) AS hours FROM ... WHERE ...) b
WHERE a.pk=b.pk
Bye Michlmann |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > Join two sum queries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|