I have a table like this:
datetime datacenter machines
---------------------------------------------------------
2020-05-13 12:00:00.000 DC01 500
2020-05-13 12:00:00.000 DC02 100
2020-04-10 13:00:00.000 DC01 510
2020-04-10 13:00:00.000 DC02 120
2020-03-1 14:00:00.000 DC01 530
2020-03-1 14:00:00.000 DC02 140
The datetime column is a Datetime2 type, rest are VARCHAR.
I need to make a new view that will group the entries by datetime and make columns from the rows that have dc01 and dc02 datacenters and put the no. of machines in the respective rows, essentially transposing and merging the data.
In the source table there will always be 2 entries for each datetime, one for each datacenter, and when it is merged the datetime should be unique. Here is to illustrate the resulting view:
resulting_view
datetime dc01_machines dc02_machines
---------------------------------------------------------
2020-05-13 12:00:00.000 500 100
2020-04-10 13:00:00.000 510 120
2020-03-1 14:00:00.000 530 140
I have spent a while trying to come up with solutions.
In my mind I have the solution that is to do 2 separate selects, one for each datacenter, combine them with UNION and then just group them by datetime, but I'm sure this is terrible and it is not even running, there is an error about invalid syntax near GROUP. Here is the attempt:
(SELECT t1.datetime
,t1.machines as dc01_machines
,'' as dc02_machines
FROM table1 t1
WHERE datacenter = 'DC01')
UNION
(SELECT t1.datetime
,'' as dc01_machines
,t1.machines as dc02_machines
FROM table1 t1
WHERE datacenter = 'DC02')
GROUP BY datetime
Thanks, any help is appreciated!