Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Need help with code

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2013-05-10 : 14:44:27
Is there a better way to do this Group by?


SELECT r.id,
CASE
WHEN t.funcareaid IS NULL THEN '<All>'
ELSE f.name
END fname,
Sum(COALESCE(p.govvalue, 0)
+ COALESCE(p.ctrvalue, 0)) hours,
r.idnum,
r.recordnum,
r.title
FROM tbltrkrecord r
LEFT JOIN tbltrkteamest e
ON e.recordid = r.id
LEFT JOIN tbltrkestphase p
ON p.teamestid = e.id
LEFT JOIN tbltrkteam t
ON t.id = e.teamid
LEFT JOIN tbltrkfuncarea f
ON f.id = t.funcareaid
WHERE r.recordnum = 'BOF-C13-0002'
GROUP BY r.id,
CASE
WHEN t.funcareaid IS NULL THEN '<All>'
ELSE f.name
END

Dave
Helixpoint Web Development
http://www.helixpoint.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-13 : 00:53:40
Nope. Only think you can do is wrap the inner query inside a dervied table and then use alias fname directly in GROUP BY outside, but I dont think performance wise it will make any better

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -