Solved

Two different where clauses in the same row...

Posted on 2011-09-27
4
188 Views
Last Modified: 2012-05-12
Table WorkLog = id, projectName, IsSupport, hours, logNotes

I need the following result

"Project"       - Distinct Project Name,
"Total Hours"   - sum of hours for the project name,
"Support Hours" - sum of hours for the project name where IsSupport = 1


So,
1, 'www.site...', 0, 10, 'asdfasdfasdf'
2, 'www.site...', 0, 10, 'qwerqwerqwer'
3, 'www.site...', 1, 10, 'zxcvzxcvzxcv'
4, 'www.other..', 0, 20, '123412341234'

would return this:
Project            Total Hours      Support Hours
'www.site...'      30            10
'www.other..'      20            0

I was thinking maybe I could do an inner join against itself (on wl1.id = wl2.id), and where values on each.
0
Comment
Question by:hpdvs2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 250 total points
ID: 36709695
SELECT Project, SUM(Hours) AS TotalHours, SUM(CASE WHEN isSupport = 1 THEN Hours ELSE 0 END) AS SupportHours
GROUP BY Project
0
 
LVL 15

Assisted Solution

by:tim_cs
tim_cs earned 250 total points
ID: 36709699
SELECT
   Project
   ,SUM(Hours) TotalHours
   ,SUM(CASE WHEN IsSupport = 1 THEN Hours ELSE 0 END) SupportHours
FROM
   WorkLog
GROUP BY
   Project
0
 
LVL 18

Expert Comment

by:deighton
ID: 36709712
SELECT Project, SUM(Hours) AS TotalHours, SUM(CASE WHEN isSupport = 1 THEN Hours ELSE 0 END) AS SupportHours
from yourTable
GROUP BY Project
0
 
LVL 8

Author Closing Comment

by:hpdvs2
ID: 36709742
Thanks,  I've never used a CASE command in a function call before.  Most useful.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2016 Integration Service connecting to 2012 3 31
SQL Pivot with row total 5 26
job schedule 8 16
SQL - Simple Pivot query 8 12
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question