Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    87

    Unanswered: 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

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT category
         , SUM(hours) AS Sumhours
      FROM daTable
     WHERE category IN ( 1 , 2 )
    GROUP
        BY category
    simple, innit

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Aug 2006
    Posts
    87
    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

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by alexiop
    ...how could i get the sum of each category?
    please run my query and inspect the results
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •