I have a request with a bunch of Leftjoins.
created a request to output “direct” data, displays:
| Order number | Attribute | Value | | 9213 | Format | 20x20 | | 9213 | Quantity | 4 | | 9213 | Material | Leather | | 9214 | Format | 80x80 | | 9214 | Quantity | 1 | | 9214 | Material | Tree |
I need to display as follows:
| Order number | Format | Quantity | Material | | 9213 | 20x20 | 4 | Leather | | 9214 | 80x80 | 1 | Tree |
I do not understand how to properly describe Pivot, all examples are too simple, and I have a request of 5 joyans and 6 conditions. Can you please show and describe this example ..
Answer 1, Authority 100%
Here you have a working example:
create table #Temp_Table ([[Order number] int, [attribute] nvarchar (250), [Value] Nvarchar ( 250)); INSERT #Temp_Table Values. (9213, N'Format ', N'20x20'), (9213, N'Colomacy ', N'4'), (9213, N'Material ', N'KZH'), (9214, N'Format ', N'80x80'), (9214, N'Kolism ', N'1'), (9214, n'material ', n'devo'); With Temp As. (Select * from #Temp_Table) - Insert your request here, as the colleague pointed out - Below, in fact, all you need SELECT [order number], [format], [quantity], [Material] From Temp. Pivot. ( MAX ([Value]) for [attribute] In ([format], [quantity], [Material]) ) AS PVT. Drop Table #Temp_Table
Documentation: Pivot and Unpivot .