0

I have the following data set:

Worker Hours
Johnny 8
Johnny 7
Johnny 4
Liz 7
Liz 2
Sally 9
Sally 9
Sally 3

I need a SQL Select statement to sum the results for each distinct person. Ideally my results would look like the following:

Worker Hours
Johnny 19
Liz 9
Sally 21

I'm able to do this in SQL by looping through the data set, but for this problem I'm constrained to a single SELECT statement. Thanks in advance.

  • 3
    Hint: `GROUP BY`. `SUM()`. This is very basic SQL and suggests that you should study the language. – Gordon Linoff Jan 27 '21 at 21:58
  • I'm trying, and appreciate the simplicity of the issue. But as it's for a work problem and not a school issue I was hoping for someone more knowledgeable than me to post a solution or hint to one, and if they felt inclined an explanation as to why. I did scroll through a lot of Stack Overflow questions, and the only similar one I found was with a solution posted by you: https://stackoverflow.com/questions/18218108/trying-to-sum-distinct-values-sql# . I didn't understand the partition by, however, and have been pulling my nonexistent hair out trying to understand it. Thanks! – noobiest Jan 27 '21 at 22:05
  • . . This isn't intended to be cruel. But if you don't know what `group by` is, then you are perhaps not the best person to be working on this problem at your workplace. You should take some time to learn the basics of SQL. There is nothing confusing or advanced; what you are asking is pretty much SQL 101. – Gordon Linoff Jan 27 '21 at 22:46

1 Answers1

0

Its a simple group by :

select Worker , sum(Hours) hours
from tablename
group by worker
eshirvana
  • 23,227
  • 3
  • 22
  • 38