如何在SQL中使用PIVOT获得实际结果?
我正在使用一个查询并得到结果。但我不需要这个结果,我需要另一个结果。我知道我想要显示的结果的查询是错误的。我很困惑地询问结果是如何产生的,以及是否有其他方法可以得到这个结果如何在SQL中使用PIVOT获得实际结果?,sql,sql-server,tsql,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 va
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 |
这能回答你的问题吗
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 != ''
您可以通过使用
行编号()
向集合中添加唯一的行来尝试此操作
您将获得所需的输出
输出:
+-----------+------------+----------+----------------------+
| 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个分组,并将它们与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
请参阅您有一行,因为
ProductId
对于每一行总是相同的。当您使用运算符MAX()
时,就得到了一行MAX()
的值。但是,如果希望有其他行,则只需区分这些行:
SELECT
p.ProductId
, p.TotalDiamondWeight
, p.GemType
, p.StoneCut
FROM
(
SELECT ProductId,
Caption,
Value,
ROW_NUMBER() OVER (PARTITION BY Caption ORDER BY Value) RN
FROM @temp
) x
PIVOT
(
MAX(Value)
FOR Caption IN ([StoneCut], [GemType], [TotalDiamondWeight])
) p;
很好用。但是我有另一个问题,当产品id不同时,它会出错。你能更新你问题中的样本数据和预期输出吗。我将尝试修复查询。我刚刚更改了数据和预期输出。请看一看。我已经猜出了答案,请检查并确认您是否仍然得到答案。它正按照我的要求完美地工作着。非常感谢你。
SELECT
p.ProductId
, p.TotalDiamondWeight
, p.GemType
, p.StoneCut
FROM
(
SELECT ProductId,
Caption,
Value,
ROW_NUMBER() OVER (PARTITION BY Caption ORDER BY Value) RN
FROM @temp
) x
PIVOT
(
MAX(Value)
FOR Caption IN ([StoneCut], [GemType], [TotalDiamondWeight])
) p;