January 18, 2017 at 10:34 am
I have a business analyst problem and need help writing the below query.
1) Write a SQL query to find the sum of hours worked by each person on all Mondays
Below is the table information:
Name | Hours worked | Day | WeekNumber |
Andrew | 1 | Monday | Week1 |
Andrew | 2 | Tuesday | Week1 |
Andrew | 3 | Wednesday | Week1 |
Andrew | 4 | Thursday | Week1 |
Andrew | 5 | Friday | Week1 |
Andrew | 6 | Saturday | Week1 |
Andrew | 7 | Sunday | Week1 |
Bina | 1 | Monday | Week1 |
Bina | 2 | Tuesday | Week1 |
Bina | 3 | Wednesday | Week1 |
Bina | 4 | Thursday | Week1 |
Bina | 5 | Friday | Week1 |
Bina | 6 | Saturday | Week1 |
Bina | 7 | Sunday | Week1 |
Andrew | 1 | Monday | Week2 |
Andrew | 2 | Tuesday | Week2 |
Andrew | 3 | Wednesday | Week2 |
Andrew | 4 | Thursday | Week2 |
Andrew | 5 | Friday | Week2 |
Andrew | 6 | Saturday | Week2 |
Andrew | 7 | Sunday | Week2 |
Bina | 1 | Monday | Week2 |
Bina | 2 | Tuesday | Week2 |
Bina | 3 | Wednesday | Week2 |
Bina | 4 | Thursday | Week2 |
Bina | 5 | Friday | Week2 |
Bina | 6 | Saturday | Week2 |
Bina | 7 | Sunday | Week2 |
January 18, 2017 at 11:21 am
SELECT Name, MondayHours SUM(HoursWorked)
FROM dbo.TableName
WHERE Day = 'Monday'
GROUP BY Name
ORDER BY Name;
January 18, 2017 at 11:26 am
This looks like a homework question (You have your question number in your post). Perhaps some further reading on GROUP BY (Transact-SQL) would assist you better with your learning.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 18, 2017 at 11:39 am
Thom A - Wednesday, January 18, 2017 11:26 AMThis looks like a homework question (You have your question number in your post). Perhaps some further reading on GROUP BY (Transact-SQL) would assist you better with your learning.
Close. I saw in another post it was an interview question. I should have seen the question number in the OP.
January 18, 2017 at 1:03 pm
Thanks Ed! It is to help me prepare for an interview I have on Friday. It was a sample question to help me understand how to do it when I get to the interview and I Get a blank question and I have to solve it. I got most of my sql training from the Air Force but taught myself so do not know how to do some of the query searches correctly so want to make sure that I learn from other sql techs out there how to do it correctly. I appreciate the information as it will help me prepare so I can run searches correctly. I'm reading the posts you have on the forums and enjoy reading about sql and am always learning so thanks much!!!
January 18, 2017 at 1:05 pm
Thanks for the link on the group by question that is exactly what will help me refresh this area in my sql query! Thanks!!
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply