如何在SQL中使用PIVOT获得实际结果?

时间:2019-01-23 13:11:31

标签: sql sql-server tsql

我正在使用查询并获取结果。但是我不需要这个结果,我需要另一个结果。我知道查询要显示的结果是错误的。我很困惑地询问结果如何显示,以及是否还有其他方法可以实现此结果。

declare @temp table(
  ProductId int,
  Caption nvarchar(max),
  Value nvarchar(max)
)

insert into @temp values (6830,'Stone Cut','Full Cut')
insert into @temp values (6830,'Stone Cut','Single Cut')
insert into @temp values (6830,'Gem Type','Diamond')
insert into @temp values (6830,'Gem Type','Diamond')
insert into @temp values (6830,'Total Diamond Weight','0.34')

insert into @temp values (6831,'Stone Cut','Full Cut')
insert into @temp values (6831,'Stone Cut','Single Cut')
insert into @temp values (6831,'Gem Type','Diamond')
insert into @temp values (6831,'Gem Type','Diamond')
insert into @temp values (6831,'Total Diamond Weight','0.35')

select ProductId
  , (case when [StoneCut] is null then '' else [StoneCut] end) as [StoneCut]
  , (case when [GemType] is null then '' else [GemType] end) as [GemType]
  , (case when [TotalDiamondWeight] is null then '' else [TotalDiamondWeight] end) as [TotalDiamondWeight]
from
(
  select ProductId, Caption, Value
  from @temp
) x
pivot
(
  max(Value)
  for Caption in([StoneCut], [GemType], [TotalDiamondWeight])
)p;

它显示一行像

---------------------------------------------------
ProductId | StoneCut | GemType | TotalDiamondWeight
---------------------------------------------------
 6830     |Single Cut| Diamond | 0.34

但是我想要这个输出:

---------------------------------------------------
ProductId | StoneCut | GemType | TotalDiamondWeight
---------------------------------------------------
 6830     |Full Cut  | Diamond | 0.34
 6830     |Single Cut| Diamond |
 6831     |Full Cut  | Diamond | 0.34
 6831     |Single Cut| Diamond |

4 个答案:

答案 0 :(得分:2)

您只有一行,因为How to extend this image每一行总是相同的。当您使用运算符ProductId时,您将得到一行MAX()MAX()的行。但是,如果您希望有其他行,则只需区分这些行:

VALUE

答案 1 :(得分:1)

您可以通过使用Row_number()向集合添加唯一行来尝试此操作吗。

declare @temp table(
  ProductId int,
  Caption nvarchar(max),
  Value nvarchar(max)
)

insert into @temp values (6830,'Stone Cut','Full Cut')
insert into @temp values (6830,'Stone Cut','Single Cut')
insert into @temp values (6830,'Gem Type','Diamond')
insert into @temp values (6830,'Gem Type','Diamond')
insert into @temp values (6830,'Total Diamond Weight','0.34')

insert into @temp values (6831,'Stone Cut','Full Cut')
insert into @temp values (6831,'Stone Cut','Single Cut')
insert into @temp values (6831,'Gem Type','Diamond')
insert into @temp values (6831,'Gem Type','Diamond')
insert into @temp values (6831,'Total Diamond Weight','0.35')

   SELECT productid, 
       [Stone Cut], 
      [Gem Type], 
      [Total Diamond Weight]
    FROM (SELECT * 
        FROM   (SELECT t.*, 
                       Row_number() 
                         OVER ( 
                           partition BY productid,T.caption 
                           ORDER BY (SELECT 1)) RN 
               FROM   @temp t)x 
               PIVOT (Max(value) 
                     FOR caption IN([Stone Cut], 
                                    [Gem Type], 
                                    [Total Diamond Weight]))p
             ) t1 
    ORDER BY productid

您将获得所需的输出。

输出:

+-----------+------------+----------+----------------------+
| productid | Stone Cut  | Gem Type | Total Diamond Weight |
+-----------+------------+----------+----------------------+
| 6830      | Full Cut   | Diamond  | 0.34                 |
+-----------+------------+----------+----------------------+
| 6830      | Single Cut | Diamond  | NULL                 |
+-----------+------------+----------+----------------------+
| 6831      | Full Cut   | Diamond  | 0.35                 |
+-----------+------------+----------+----------------------+
| 6831      | Single Cut | Diamond  | NULL                 |
+-----------+------------+----------+----------------------+

答案 2 :(得分:0)

这能回答您的问题吗?

select * from (
select 
 ProductId, 
 StoneCut, 
 max(GemType) over (partition by ProductId) GemType,
 max(TotalDiamondWeight) over (partition by ProductId) TotalDiamondWeight
from
(
  select 
    ProductId, 
    IIF(Caption = 'StoneCut', Value, '') StoneCut,
    IIF(Caption = 'GemType', Value, '') GemType,
    IIF(Caption = 'TotalDiamondWeight', Value, '') TotalDiamondWeight
  from @temp
) t
)t2
where StoneCut != ''

Fiddle

答案 3 :(得分:0)

似乎您必须对表进行2个分组,然后将它们与UNION合并:

select
  ProductId,
  min(case when Caption = 'StoneCut' then Value end) StoneCut,
  min(case when Caption = 'GemType' then Value end) GemType,
  min(case when Caption = 'TotalDiamondWeight' then Value end) TotalDiamondWeight
from temp
group by ProductId
union 
select
  ProductId,
  max(case when Caption = 'StoneCut' then Value end) StoneCut,
  max(case when Caption = 'GemType' then Value end) GemType,
  max(case when Caption = 'TotalDiamondWeight' then Value end) TotalDiamondWeight
from temp
group by ProductId

请参见demo