Sum Query search

  • 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 workedDayWeekNumber
    Andrew1MondayWeek1
    Andrew2TuesdayWeek1
    Andrew3WednesdayWeek1
    Andrew4ThursdayWeek1
    Andrew5FridayWeek1
    Andrew6SaturdayWeek1
    Andrew7SundayWeek1
    Bina1MondayWeek1
    Bina2TuesdayWeek1
    Bina3WednesdayWeek1
    Bina4ThursdayWeek1
    Bina5FridayWeek1
    Bina6SaturdayWeek1
    Bina7SundayWeek1
    Andrew1MondayWeek2
    Andrew2TuesdayWeek2
    Andrew3WednesdayWeek2
    Andrew4ThursdayWeek2
    Andrew5FridayWeek2
    Andrew6SaturdayWeek2
    Andrew7SundayWeek2
    Bina1MondayWeek2
    Bina2TuesdayWeek2
    Bina3WednesdayWeek2
    Bina4ThursdayWeek2
    Bina5FridayWeek2
    Bina6SaturdayWeek2
    Bina7SundayWeek2
  • You'll want to do something like this:

    SELECT Name, MondayHours SUM(HoursWorked)
      FROM dbo.TableName
      WHERE Day = 'Monday'
      GROUP BY Name
      ORDER BY Name;

  • 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

  • Thom A - Wednesday, January 18, 2017 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.

    Close.  I saw in another post it was an interview question.  I should have seen the question number in the OP.

  • 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!!!

  • 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