Two different where clauses in the same row...

Posted on 2011-09-27
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

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

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

I was thinking maybe I could do an inner join against itself (on =, and where values on each.
Question by:hpdvs2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Accepted Solution

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
LVL 15

Assisted Solution

tim_cs earned 250 total points
ID: 36709699
   ,SUM(Hours) TotalHours
   ,SUM(CASE WHEN IsSupport = 1 THEN Hours ELSE 0 END) SupportHours
LVL 18

Expert Comment

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

Author Closing Comment

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

635 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