I have a table where measurement results are stored like...
╔═════════╦═══════╗
║ TEST_ID ║ VAL ║
╠═════════╬═══════╣
║ 1 ║ 0.304 ║
║ 1 ║ 0.31 ║
║ 1 ║ 0.306 ║
║ 1 ║ 0.302 ║
║ 2 ║ 2.3 ║
║ 2 ║ 2.5 ║
║ 2 ║ 2.4 ║
║ 2 ║ 2.9 ║
║ 3 ║ 9.8 ║
║ 3 ║ 4.4 ║
║ 3 ║ 5.1 ║
║ 3 ║ 5.2 ║
╚═════════╩═══════╝
Now there will be an undetermined number of tests, but if that helps, I know that there always be four results for a test. Also, this is a result set of a query, all measurements have their unique ID, but they don't have a date/order field. What I would like to see in the result set is
╔═════════╦═══════╦═══════╦═══════╦═══════╗
║ TEST_ID ║ VAL1 ║ VAL2 ║ VAL3 ║ VAL4 ║
╠═════════╬═══════╬═══════╬═══════╬═══════╣
║ 1 ║ 0.304 ║ 0.31 ║ 0.306 ║ 0.302 ║
║ 2 ║ 2.3 ║ 2.5 ║ 2.9 ║ 2.4 ║
║ 3 ║ 9.8 ║ 4.4 ║ 5.1 ║ 5.2 ║
╚═════════╩═══════╩═══════╩═══════╩═══════╝
I examined PIVOT, but it doesn't help for two reasons:
- It requires aggregation and I don't want to aggregate anything.
- I don't know the number and ID of all the tests (in reality we are talking about hundreds of tests and practically any possible outcome between -10 and 10.)
So my question is if its even doable in (T)SQL and if it is, how to do it.
p.s. No, this question is neither a duplicate of
TSQL Pivot without aggregate function
nor a duplicate of Efficiently convert rows to columns in sql server
nor a duplicate of SQL Server dynamic PIVOT query?
Both questions have answers for cases where the header value set is present in the source table and finite. In my case neither of those premises are true.