Home Forums Programming General Need Help on SQL query script on sum RE: Need Help on SQL query script on sum

  • pongcs86 (6/28/2013)


    Hey guys,

    New to the site, new to writing SQL queries and have no background in this type of thing.

    But I've been googling my way to victory for the most part.

    I'm struggling on probably a very simple problem.

    I'd like to Sum the contents of a column, if a different value in the row is the same as in another... That was worded horribly - how about an illustration:

    Input Table:

    1. Unit ID Value

    2. 1 20

    3. 1 30

    4. 1 20

    5. 1 30

    6. 1 10

    7. 2 51

    8. 3 50

    9. 4 20

    10. 4 30

    11. 4 10

    12. 4 5

    13. 4 50

    14. 5 5

    15. 5 50

    16. 6 5

    17. 6 3

    18. 6 50

    19. 7 35

    20. 7 4

    So basically I'm looking for an output when I run the script that shows

    aggregated of Value must >= 50 after sum up with ID.The flag indicator will tag with 'Y' if 1 unit >= 50

    Expected output :

    1. ID Value Indicator flag

    2. 1 110(20+30+20+30+10) N

    3. 2 51 Y

    4. 3 50 Y

    5. 4 115(20+30+10+5+50) N

    6. 5 55 (50+5) N

    7. 6 58 (50+8) N

    And various other simplistic queries, but at this point I'm willing to admit that I haven't a clue what I'm doing.

    Any help would be appreciated,

    Thanks!

    I think that you are attempting to sum the value by ID, and then populate the Indicator Flag based on whether any single row for the ID contains a value >= 50. If I am correct, your expected output doesn't match that. ID 4 sums to 115 and has a single row = 50 --- so that should show a flag of "Y" I think?

    EDIT - or do you only expect a flag of "Y" if there is only a single row for the ID and it has a value >= 50?