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 > Microsoft SQL Server > combining two queries

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-09, 13:02
alexiop alexiop is offline
Registered User
 
Join Date: Aug 2006
Posts: 86
combining two queries

Hi everybody..

have this first query

select sum(hours) as Sumhours1
where category=1

second query now with category=2

select sum(hours) as Sumhours2
where category=1

Is it possible to combine this query into one

thanks
Reply With Quote
  #2 (permalink)  
Old 07-03-09, 13:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Code:
SELECT category
     , SUM(hours) AS Sumhours
  FROM daTable
 WHERE category IN ( 1 , 2 )
GROUP
    BY category
simple, innit

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-03-09, 13:43
alexiop alexiop is offline
Registered User
 
Join Date: Aug 2006
Posts: 86
Quote:
Originally Posted by r937
Code:
SELECT category
     , SUM(hours) AS Sumhours
  FROM daTable
 WHERE category IN ( 1 , 2 )
GROUP
    BY category
simple, innit

Sorry for not explaining clearly but the two queries should be combined into one and your solution is right. but on that how could i get the sum of each category? like sum(hours) as sumhours1 where category=1 and sum(hours) where category=2

thanks again
Reply With Quote
  #4 (permalink)  
Old 07-03-09, 14:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by alexiop
...how could i get the sum of each category?
please run my query and inspect the results
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-03-09, 15:52
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,307
alexiop:

The query that r937 gave you may or may not do what you want. That is something that only you can decide.

The query that 937 gave does what you've requested. Take a look at the queries output.

Note that the WHERE clause in this code is something that is necessary to meet your request, but it could be what is keeping you from getting the results that you want.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools
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