Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Query SQL con colonne di ritorno basate sul raggruppamento

Si prega di provare i seguenti codici. Devi usare la query PIVOT.

create table TableName (Series varchar(20),col2 varchar(10), price decimal(8,2))

insert into TableName values ('105','C50',30)
insert into TableName values ('105','C50',10)

insert into TableName values ('105','C53',20)
insert into TableName values ('105','C53',30)


SELECT Series,
       [C50],
       [C53]
FROM   (SELECT Series,
               col2,
               price
        FROM   TableName) AS SourceTable 
                   PIVOT ( SUM(price) FOR col2 IN ([C50],
                                                   [C53])
       ) AS PivotTable;