Trying to figure how to write a dynamic pivot sql statement. Where TEST_NAME
could have up to 12 different values (thus having 12 columns). Some of the VAL will be Int, Decimal, or Varchar data types. Most of the examples I have seen have some from of aggregate included. I am looking to for a straight value pivot.
Source Table ╔═══════════╦══════╦═══════╗ ║ TEST_NAME ║ SBNO ║ VAL ║ ╠═══════════╬══════╬═══════╣ ║ Test1 ║ 1 ║ 0.304 ║ ║ Test1 ║ 2 ║ 0.31 ║ ║ Test1 ║ 3 ║ 0.306 ║ ║ Test2 ║ 1 ║ 2.3 ║ ║ Test2 ║ 2 ║ 2.5 ║ ║ Test2 ║ 3 ║ 2.4 ║ ║ Test3 ║ 1 ║ PASS ║ ║ Test3 ║ 2 ║ PASS ║ ╚═══════════╩══════╩═══════╝ Desired Output ╔══════════════════════════╗ ║ SBNO Test1 Test2 Test3 ║ ╠══════════════════════════╣ ║ 1 0.304 2.3 PASS ║ ║ 2 0.31 2.5 PASS ║ ║ 3 0.306 2.4 NULL ║ ╚══════════════════════════╝
The answer is no: PIVOT requires aggregation.
Inside the Pivot Column dialog, select the column with the values that will populate the new columns to be created. In this case "Time" but could be any field type, including text. In the Advanced Options part, select "Don´t Aggregate" so the values will displayed without any modification.
How to pivot without aggregate function in Snowflake? An aggregate function is a mandatory function in the pivot. Hence there is no direct way of pivoting without using the aggregate function.
You always need to use an aggregate function while pivoting. Even if you don't really need any aggregation, that is, when what you see in the table is what you'll get in the result set, you still have to use an aggregate function. If there will only be one value contrinuting to each cell, then you can use MIN or MAX.
The PIVOT
function requires an aggregation to get it to work. It appears that your VAL
column is a varchar
so you will have to use either the MAX
or MIN
aggregate functions.
If the number of tests is limited, then you can hard-code the values:
select sbno, Test1, Test2, Test3 from ( select test_name, sbno, val from yourtable ) d pivot ( max(val) for test_name in (Test1, Test2, Test3) ) piv;
See SQL Fiddle with Demo.
In your OP, you stated that you will have an larger number of rows to turn into columns. If that is the case, then you can use dynamic SQL:
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ',' + QUOTENAME(TEST_NAME) from yourtable FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT sbno,' + @cols + ' from ( select test_name, sbno, val from yourtable ) x pivot ( max(val) for test_name in (' + @cols + ') ) p ' execute(@query)
See SQL Fiddle with Demo.
Both versions will give the same result:
| SBNO | TEST1 | TEST2 | TEST3 | --------------------------------- | 1 | 0.304 | 2.3 | PASS | | 2 | 0.31 | 2.5 | PASS | | 3 | 0.306 | 2.4 | (null) |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With