Solved

Two different where clauses in the same row...

Posted on 2011-09-27
4
192 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
[X]
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
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

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