Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2003
    Posts
    37

    Question Unanswered: sum two columns for each name/month (was "SQL Query - Need Help!")

    Can someone Please help me with what seems to me a "complex" query??

    I have the following columns/data - See Original Data.jpg

    This is what I need the SQL query(s) to do. I need it to
    1) Select the distinct names AND months
    2) Count the number of times that a name AND a month appeared together
    3) Sum totalsold for each name/month
    4) sum employeescore for each name/month

    So it would look like - After Query.jpg
    Attached Thumbnails Attached Thumbnails Original Data.JPG   After Query.JPG  

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Homework assignment?

    What have you tried so far?

    Hint: using an aggregate query for requirements #2, #3, and #4 will automatically give you a distinct selection of name and month to satisfy requirement #1.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    it's a new semester but do the kids go back to class on Jan 2nd? seems a little soon. I never went to the first day of class unless I did not know the professor and I was thinking of dropping it.

    even if class has started, do you usually get homework on the first day? most kids do not have their books yet.
    “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.

  4. #4
    Join Date
    Jul 2003
    Posts
    37

    not a student

    no i am not a student ... i am a full time employee at a company and that is not real data but the concept is the same. my experience with sql was moderate four years ago when I actually created databases but as you would imagine all that goes out the window when you don't use it for awhile ...

    i'm writing a macro in excel and am working with it as if it was a database for a roadblock i encountered with a project so help writing this complex query would be appreciated. i can get the first one i asked for but for the life of me cannot get them all together

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Its a large topic, but not complicated. Read up on aggregate queries and functions in Books Online.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  6. #6
    Join Date
    Jul 2003
    Posts
    37

    yes

    have been.
    any help with any of the questions 2-4 should help me figure out the others

  7. #7
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Oh come on now. I don't believe you have read anything on aggregate queries. This is pitifully easy.

    select count(*), sum(c), sum(d) from table group by a, b

    Plus, apparently you were shown how to do this before: http://www.dbforums.com/showthread.php?t=1201902
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  8. #8
    Join Date
    Jul 2003
    Posts
    37

    ..

    yes i have had problems with count in the past ... but that isn't giving me what i'm looking for . i'm trying to count two columns name and month. so you see john and august both come up together twice dave and august come up together once. i've tried

    select count(name,month) from .... which isn't giving me a value correctly. ideas?

  9. #9
    Join Date
    Jul 2003
    Posts
    37
    alright someone else helped me and now it works beautifully group by was the key i was using distinct which was throwing off everything else

  10. #10
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    So, you didn't even bother to read my pseudo-code illustrating the use of GROUP BY? You just waited until somebody came around and wrote the query for you, eh?
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  11. #11
    Join Date
    Jan 2004
    Location
    In a large office with bad lighting
    Posts
    1,040
    Quote Originally Posted by blindman
    So, you didn't even bother to read my pseudo-code illustrating the use of GROUP BY? You just waited until somebody came around and wrote the query for you, eh?
    You can lead a horse to water ...

    -- This is all just a Figment of my Imagination --

Posting Permissions

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