Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Dynamic pivoting

By Trevor Makoni,

--create table master.dbo.Invoice([Department] varchar(50), [Year] int, [Month] int, Amount int);

exec dbo.dynamicPivot

@tablename = '[master].[dbo].[Invoice]',

@pivotColumn = '[month]',

@groupBy = '[Department],[Year]',

@aggregateColumns = '[Amount]',

@aggregation = 'SUM([Amount])',

@execute = 1;

go

Running this results in the below:

Total article views: 4630 | Views in the last 30 days: 3
 
Related Articles
FORUM

converting negative amounts into positive amount

converting negative amounts into positive amount

FORUM

amount difference between 2 dats

amount difference between 2 dats

FORUM

how to find max amount

how to find max amount

FORUM

Insert query to distribute given amount

Insert rows dynamically to distribute given Amount

FORUM

Limit buffer pool memory amount by database

Limit buffer pool memory amount by database

Tags
pivot    
 
Contribute