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 2000 Forums
 Transact-SQL (2000)
 Select with Case & Group by to Aggregate and De-Normalize

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-03-16 : 08:33:45
Tom writes "I wish to create a workfile from a normalized database that both SUMs data and conditionally creates aggregate (SUM) columns within one Select statement.

(ie. SELECT CustomerID, AdjustmentType, SUM(AdjustmentAmount) as Total_adjustments From ADJUSTMENTS
Group By CustomerID), AdjustmentType)

I would like to "flatten" out the resulting set with columns for type 1, 2, 3, 4, and other with a CASE Statement imbedded in the SELECT.

Something Like....

SUM(CASE AdjustmentType WHEN 1 THEN AdjustmentAmount ELSE ?) AS Credit
SUM(CASE AdjustmentType WHEN 2 THEN AdjustmentAmount ELSE ?) AS Debit
SUM(CASE AdjustmentType ELSE..... AS Other

or to create summary colums my months like:

SUM(CASE mnth WHEN 1 THEN sales ELSE NULL END) AS jan,
etc

Is this possible? Or am I dreaming?

Thanks
Tom"

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-16 : 08:38:13
yes that's how you do it ... did you try it?

- Jeff
Go to Top of Page
   

- Advertisement -