General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old September 20th, 2004, 04:55 AM
lordzoster lordzoster is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 lordzoster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old September 28th, 2004, 01:48 AM
michlmann michlmann is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 56 michlmann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > Join two sum queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway