question

stevep avatar image
stevep asked

Sql query question

I have a table for database growth . The columns are servername, dbname, dbsize, loaddate A job runs weekly that inserts a new row for each servername,dbname with the size for that week. My question is what would be the sql to produce one row for each servername and dbname that includes the sizes for each weeks ? Output should look like servername dbname week1_size week2_size week3_size week4_size Thanks
tsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Phil Factor avatar image
Phil Factor answered
I'm assuming that Week 4 is this week etc. Select ServerName, dbName, max(Case when datediff(week,LoadDate,GetDate())=3 then dbsize else 0 end) as [Week 1], max(Case when datediff(week,LoadDate,GetDate())=2 then dbsize else 0 end) as [Week 2], max(Case when datediff(week,LoadDate,GetDate())=1 then dbsize else 0 end) as [Week 3], max(Case when datediff(week,LoadDate,GetDate())=0 then dbsize else 0 end) as [Week 4] from YourData Group by ServerName, DBName
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.