SQL Server ANSI SQL to convert lines in columns

ve7v8dk2  于 2023-03-28  发布在  其他
关注(0)|答案(2)|浏览(126)

I am trying to convert lines in columns based on data below:

Select EvolutionOrder, Age from Product where ProductID = 1 

EvolutionOrder Age
-------------- ----
             1 0012
             2 1324
             3 2536
             4 3700

Select EvolutionOrder, Age from Product where ProductID = 2 

EvolutionOrder Age
-------------- ----
             1 QUEE
             1 HIVE

I Need The resulta as:

Product Age1 Age2 Age3 Age4 Age5
------- ---- ---- ---- ---- ----
      1 0012 1324 2536 3700 NULL
      2 QUEE HIVE NULL NULL NULL

I tried to use PIVOT, but did not worked because aggregate function for Age Column vanish with values

SELECT ProductID,
    [1] AS Age1,
    [2] AS Age2,
    [3] AS age3,
    [4] AS age4
FROM Product
PIVOT (MAX(Age) FOR EvolutionOrder IN ([1], [2], [3], [4])) piv;

and I got THAT:

ProductID Age1 Age2 age3 age4
--------- ---- ---- ---- ----
1         0012 1324 2536 3700
2         QUEE NULL NULL NULL  --> HIVE Vanished

The other way I got that is running the query below:

with vFX1 as (Select Age Age1 from Product  
                where ProductID = 1 and EvolutionOrder = 1)
   , vFX2 as (Select Age Age2 from Product  
                where ProductID = 1 and EvolutionOrder = 2)
   , vFX3 as (Select Age Age3 from Product  
                where ProductID = 1 and EvolutionOrder = 3)
   , vFX4 as (Select Age Age4 from Product  
                where ProductID = 1 and EvolutionOrder = 4)
   , vFX5 as (Select Age Age5 from Product  
                where ProductID = 1 and EvolutionOrder = 5)
select * from vFX1 Left outer Join vFX2 on 1=1
                   Left outer Join vFX3 on 1=1
                   Left outer Join vFX4 on 1=1
                   Left outer Join vFX5 on 1=1

I did not like this solution because I need to run over same table many times and vanish data too.

How could I do that with just one single ANSI sql smartest query?

Below the script to create data:

Create table Product
(ProductID      Int
,EvolutionOrder Int
,Age            Char(4))

Insert into Product values (1,1,'0012');
Insert into Product values (1,2,'1324');
Insert into Product values (1,3,'2536');
Insert into Product values (1,4,'3700');
Insert into Product values (2,1,'QUEE');
Insert into Product values (2,1,'HIVE');

Thank you

Note: Updated due to help gotten

vohkndzv

vohkndzv1#

The syntax is slightly different, take a look:

Select [1] Age1, [2] Age2, [3] Age3, [4] Age4, [5] Age5
from Product
PIVOT (MAX(Age) for EvolutionOrder in ([1],[2],[3],[4],[5])) P

Don't be afraid of aggregates. If there is only one value then MIN or MAX would be equal to that value.

k3bvogb1

k3bvogb12#

It is an issue with not selecting columns properly. You can absolutely use aggregates on CHAR or VARCHAR columns.

CREATE TABLE Product (
    ProductID int,
    EvolutionOrder int,
    Age char(4)
)

INSERT INTO Product
    VALUES (1, 1, '0012');
INSERT INTO Product
    VALUES (1, 2, '1324');
INSERT INTO Product
    VALUES (1, 3, '2536');
INSERT INTO Product
    VALUES (1, 4, '3700');

SELECT
    ProductID,
    [1] AS Age1,
    [2] AS Age2,
    [3] AS age3,
    [4] AS age4
FROM Product
PIVOT (MAX(Age) FOR EvolutionOrder IN ([1], [2], [3], [4])) piv;

DROP TABLE Product

相关问题