Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want results in columns not rows

Status
Not open for further replies.

awaria

IS-IT--Management
Sep 16, 2010
100
US
SQL Server 2000 Enterprise SP3

I have a budegt table that has a row record for each budget month for each GL account. 12 rows per GL Account
I want to write a query that will return 1 row per glaccount with a column for each periodid.

Table structure is;
BudgetID, GL Acct, PeriodID, Budget Amount.

Need help with query to "pivot?" the 12 periodid and balances into columns.

Thanks,

Andrew
 
Select PeriodID,
Sum (Case When GL Acct = 1 then Budget Amount else 0 end ),
Sum (Case When GL Acct = 2 then Budget Amount else 0 end ),
Sum (Case When GL Acct = 3 then Budget Amount else 0 end ),
Sum (Case When GL Acct = 4 then Budget Amount else 0 end ),

Sum (Case When GL Acct=99999 then Budget Amount else 0 end)
From TableName
Group by PeriodID
 
PWise, thanks for the reply. I think I would want the case when to be th eperiodids to get the results to display as such;


gl acct period 1 per2 per3 . . .
111-1111 $40 $30 $45
222-2222 $20 $20 $30

Should the group by be GL Acct?

Can I simply switc out GL ACCT in the case when for periodid,
and the group by Gl Account?

Thanks for help input.

Andrew
 
That did it. It works great.

Thanks for providing the proper scripting and syntax.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top