Results 1 to 10 of 10

Thread: Query Needed

  1. #1
    Join Date
    Jan 2008
    Posts
    23

    Unanswered: Query Needed

    consider i have these tables:
    tbl_requests
    id
    description
    owner

    tbl_owners
    id
    name

    tbl_RequestStatus
    id
    description

    am looking for output look like this:
    OwnerName New Completed
    x 2 5
    y 4 3

    table requests holds number of requests where each request has an owner, table owners holds the owners names, table status holds values for a request status for example : new, in progress and completed, the query am looking for should show each owner with the number of request he/she is handling grouped by the status, so we can say that owner X has 2 new requests and 5 completed requests.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    what have you tried so far?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jan 2008
    Posts
    23
    i tried the following that still needs to be completed:
    select Owners.Name, Status.Description from Requests
    inner join Owners on Requests.Owner = Owners.ID
    inner join Status on Requests.Status = Status.ID

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    are you familiar with GROUP BY and COUNT...

    http://technet.microsoft.com/en-us/l.../ms177673.aspx
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Jan 2008
    Posts
    23
    my case is not only about group by it is also about sub queries but am not sure if it is possible to be done with one query, take a look at this query:

    select Owners.Name, Status.Description, count(Status.ID) as Total from Requests
    inner join Owners on Requests.Owner = Owners.ID
    inner join Status on Requests.Status = Status.ID
    group by Owners.Name, Status.Description

    the result will be :

    Name---Desc-----Total
    John----new--------3
    Natalie--new--- ----7
    John----completed--4


    actually am looking to have the following result :
    Name------New------Completed
    John--------3----------4
    Natalie------7----------0

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you don't need any subquery, you need conditional totals

    for example:

    COUNT(CASE WHEN Status.Description='New' THEN 937 END) AS New

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

  7. #7
    Join Date
    Jan 2008
    Posts
    23
    ok ok wait a second this an advanced thing and new for me, can u show me how will the query look like ???

  8. #8
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    he just did.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  9. #9
    Join Date
    Jan 2008
    Posts
    23
    thnx guys it solved, seems i still need to improve my skills with SQL, thnx anyway...

  10. #10
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    it might be seem like i was leading you around by the nose, but i was trying to help you actually learn something instead of just giving you an answer. otherwise you will just be back tomorrow for another answer.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

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