We currently got 100 staging tables which are loaded everyday.
I’m struggling to write a query – which give me sort PIVOT result – showing number of records inserted into staging tables by Day.
Name of tables Day 1 Day 2 Day 3 Day 4 Day 5
--------------------------------------------------------------
AAAAA 100 50 30 60 90
Regards
You need a few different steps.
DATEDIFF(DAY, <timestamp>, getDate())will get you the number of days ago for a record.Using CASE inside a SUM, you can then do something like…
That does require the code repeating itself for every table you are querying. But I think you’re stuck doing that in one way or another no matter what.
An alternative would be to put a trigger on each table, and as data is inserted/deleted, update a tracking table with the new details. That involves writing 100 triggers instead of 100 unions.
Without writing Dynamic SQL, I’m not aware of any other fundamentally different ways of doing this to aboid writing a snippet of code 100 times…