Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 22nd, 2004, 03:39 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with GROUP BY and CASE statement

I'm trying to get the query below to execute. The problem is that EL.CategoryID, apparently, has to be in the GROUP BY list or part of an aggregate function in the SELECT. Putting it in the GROUP BY list splits up records that should be combined so that isn't an option. I tried putting in a dummy MAX (EL.CategoryID) in the SELECT but that did nothing (suprisingly). I'm stumped partly because I assumed that I would only see this error if I actually tried to return an EL.CategoryID column in the SELECT which I'm not. Anyway, your thoughts would be very welcome at this point.
Code:
SELECT 
    P.ProjectID, 
    P.Name AS ProjectName,    
    (CASE 
        WHEN EL.CategoryID = '71' THEN Sum (EL.Duration)
        ELSE 0
    END) AS TotalCBHours,
    (CASE 
        WHEN EL.CategoryID = '72' THEN Sum (EL.Duration)
        ELSE 0
    END) AS TotalFPHours,
    (CASE 
        WHEN EL.CategoryID = '73' THEN Sum (EL.Duration)
        ELSE 0
    END) AS TotalNBHours,
    Sum(EL.Duration) AS TotalHours

FROM TT_Projects P INNER JOIN TT_EntryLog EL ON (P.ProjectID = EL.ProjectID AND EL.CategoryID IN ('71','72','73'))
GROUP BY P.ProjectID, P.Name
 
Old September 23rd, 2004, 03:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

move the SUM outside the CASE
Code:
SELECT 
    P.ProjectID, 
    P.Name AS ProjectName,    
    SUM(CASE WHEN EL.CategoryID = '71' THEN EL.Duration ELSE 0 END) AS TotalCBHours,
    ...
 
Old September 23rd, 2004, 06:39 AM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That worked. Thank you *very* much. Just out of curiousity, does anyone know why putting in a dummy aggregate function that includes the EL.CategoryID doesn't work? As in:
Code:
SELECT
   MAX (EL.CategoryID),
   ...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems using case statement in where clause vghiya SQL Server 2000 3 May 28th, 2007 05:12 AM
Case statement cole SQL Language 3 May 8th, 2005 03:02 PM
case statement Hudson40 Access VBA 1 February 11th, 2005 11:31 AM
Using A CASE Statement fastcorvette Access 5 December 24th, 2003 01:39 PM
case statement jakeone Beginning PHP 10 August 19th, 2003 03:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.