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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

helixpoint
Constraint Violating Yak Guru

291 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
52326 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  
 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.04 seconds. Powered By: Snitz Forums 2000