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 2005 Forums
 Transact-SQL (2005)
 Group By on Condition

Author  Topic 

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2012-03-24 : 19:47:28
Hi I need to write a query as the following.
(What I want is to simplify it)

Select f1,f2,....sum(f19) as f19, sum(f20) as f20
from t where f15 is null
group by f1,f2,.... f18
Union
Select f1,f2,....f19, sum(f20) as f20
from t where f15 is not null
group by f1,f2,.... f19


Can I do this in 1 statement, rather than 2 lengthy statemts & a union?

Note: The Column Names are lengthy and the query is so long, so I put f1,f2,... instead of real names.

Srinika

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-24 : 21:12:18
if you're using sql 2008 you can use GROUPING SETS

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2012-03-25 : 10:06:16
Visakh, thanks for the reply.
Sorry, its sql 2005. :(


Srinika
Go to Top of Page
   

- Advertisement -