Greetings everyone I have 12 data points identified as "TagIndex". The code I am attempting to use is

SELECT HistoricalData.DateAndTime, round(HistoricalData.Val,2)

, CASE WHEN (HistoricalData.TagIndex) = '0' THEN (HistoricalData.Val/10) END AS 'PTL-3 Temp'

, CASE WHEN (HistoricalData.TagIndex) = '1' THEN (HistoricalData.Val/10) END AS 'PTL-2 Temp'

, CASE WHEN (HistoricalData.TagIndex) = '2' THEN (HistoricalData.Val/10) END AS 'PCW 4 Temp'

, CASE WHEN (HistoricalData.TagIndex) = '3' THEN (HistoricalData.Val) END AS 'PCW 4 PSI'

, CASE WHEN (HistoricalData.TagIndex) = '4' THEN (HistoricalData.Val) END AS 'PCW 2 PSI'

, CASE WHEN (HistoricalData.TagIndex) = '5' THEN (HistoricalData.Val) END AS 'PCW 2 Temp'

, CASE WHEN (HistoricalData.TagIndex) = '6' THEN (HistoricalData.Val/10) END AS 'PTL-2 Hum'

, CASE WHEN (HistoricalData.TagIndex) = '7' THEN (HistoricalData.Val/10) END AS 'PTL-3 Hum'

, CASE WHEN (HistoricalData.TagIndex) = '8' THEN (HistoricalData.Val) END AS 'CDA PSI'

, CASE WHEN (HistoricalData.TagIndex) = '9' THEN (HistoricalData.Val/100 END AS 'City pH'

, CASE WHEN (HistoricalData.TagIndex) = '12' THEN (HistoricalData.Val/10 END AS 'CWS A Temp'

, CASE WHEN (HistoricalData.TagIndex) = '10' THEN (HistoricalData.Val) END AS 'CWS B Temp'

FROM rsview.dbo.HistoricalData HistoricalData

WHERE (HistoricalData.DateAndTime>?) AND (HistoricalData.DateAndTime<?) AND (HistoricalData.Val <>0)

The code runs but the results are not usable. It seems each TagIndex number records at different times, seconds apart. So instead of nice rows of data (maybe 10,000 records) I have over 200K lines filled mostly with air.

I put a picture of it here: https://boatcoating.shutterfly.com/pictures/14

I seems to me I need to run this so I have a timestamp column and a data column for each TagIndex number.

Is there a good way to change my code to obtain a timestamp and data for each of the TagIndex numbers?