SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Need help with code
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

helixpoint
Constraint Violating Yak Guru

276 Posts

Posted - 05/10/2013 :  14:44:27  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/13/2013 :  00:53:40  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000