Home > database > Navigation database columns to zha get ah, feel very difficult
Navigation database columns to zha get ah, feel very difficult
Time:11-09
How to generate such a format
CodePudding user response:
In the raw data, there was no record is equal to the "wal-mart" enterprise name?
CodePudding user response:
The CREATE TABLE # T (ITEM_NAME VARCHAR (20), QTY INT, ITEM_NO VARCHAR (10), BUYER VARCHAR (10), PRICE INT, DELIVERY_DAY INT)
INSERT INTO # T SELECT 'MEMORY', 11, '001', 'AAAAAA, 999, 9 UNION ALL 12, SELECT the 'POWER', '002', 'AAAAAA, 888, 8 UNION ALL SELECT 'MEMORY', 11, '001', 'BBBBB, 777, 7 UNION ALL 12, SELECT the 'POWER', '002', 'BBBBB, 666, 6
WITH CTE_1 AS (SELECT *, DENSE_RANK () OVER (ORDER BY ITEM_NAME) AS SEQ_1 FROM # T),
CTE_2 AS (SELECT ITEM_NAME, 'AS the VALUE, the BUYER, SEQ_1, 1 AS SEQ_2 FROM CTE_1 UNION ALL SELECT the 'PRICE', CAST (PRICE AS VARCHAR (10)), the BUYER, AS SEQ_2 SEQ_1, 2 FROM CTE_1 UNION ALL SELECT 'DELIVERY_DAY, CAST (DELIVERY_DAY AS VARCHAR (10)), BUYER, SEQ_1, 3 AS SEQ_2 FROM CTE_1)
SELECT ITEM_NAME AS BUYER, [AAAAAA], [BBBBB] FROM CTE_2 The PIVOT (MAX (VALUE) FOR BUYER IN ([AAAAAA], [BBBBB])) BThe ORDER BY SEQ_1, SEQ_2
If an enterprise name is not fixed, it is dynamic,