Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将行乘以具有重复行的列_Sql_Sql Server_Pivot - Fatal编程技术网

Sql 将行乘以具有重复行的列

Sql 将行乘以具有重复行的列,sql,sql-server,pivot,Sql,Sql Server,Pivot,我知道这个问题在这里已经被很好地揭示了,但我相信我有一个具体的案例,到目前为止我还没有找到任何解决方案 我有以下结果集: Tp_Parameter Value Order_Id ------------ ----- -------- Colour Black 3824 Size S 3824 Qty 2 3824 ItemId

我知道这个问题在这里已经被很好地揭示了,但我相信我有一个具体的案例,到目前为止我还没有找到任何解决方案

我有以下结果集:

Tp_Parameter    Value       Order_Id
------------    -----       --------    
Colour          Black       3824
Size            S           3824
Qty             2           3824
ItemId          101         3824
Colour          White       3824
Size            M           3824
Qty             1           3824
ItemId          102         3824
Colour          Red         3824
Size            L           3824
Qty             4           3824
ItemId          105         3824
我正在寻找这样的结果集:

Order_Id    ItemId  Colour  Size    Qty
--------    ------  ------  ----    ---
3824        101     Black   S       2
3824        102     White   M       1
3824        105     Red     L       4
我尝试过使用pivot,但我无法处理这样一个事实,即它必须使用聚合函数,这会导致一行结果集,即MAX或MIN等


你能帮我吗?

让我假设每行上都有一个唯一的、递增的id。然后我们可以说一个特定的ItemId完成了该项的行

如果是:

select order_id,
       max(case when tp_parameter = 'ItemId' then value end) as ItemId,
       max(case when tp_parameter = 'Colour' then value end) as Colour,
       max(case when tp_parameter = 'Size' then value end) as Size,
       max(case when tp_parameter = 'Qty' then value end) as Qty
from (select t.*,
             sum(case when tp_parameter = 'ItemId' then 1 else 0 end) over (partition by order_id order by <id> desc) as item_grp
      from t
     ) t
group by item_grp, order_id;

你也可以这样试试

declare @t table(Tp_Parameter varchar(50)
,Value varchar(50),Order_Id int)

insert into @t VALUES

 ('Colour'   ,'Black',3824)
,('Size'   ,'S',3824)
,('Qty'   ,'2',3824)
,('ItemId'   ,'101',3824)
,('Colour'   ,'White',3824)
,('Size'   ,'M',3824)
,('Qty'   ,'1',3824)
,('ItemId'   ,'102',3824)
,('Colour'   ,'Red',3824)
,('Size'   ,'L',3824)
,('Qty'   ,'4',3824)
,('ItemId'   ,'105',3824)
;with CTE as
(
select *,ROW_NUMBER()over(order by (select NULL))rn
 from @t
 )
 select * 
 ,(select top 1 value from cte b 
 where tp_parameter='ItemId' and b.rn>=a.rn order by rn)MissingItemid
 from cte a

--now apply dynamic pivot so that you do not have to hard code parameter

为什么101是黑色,S,2?这些值之间的关系是什么?特定的ItemId和颜色之间没有逻辑联系。黑色可能属于这些ItemID中的任何一种。看起来您只是简单地依赖于它们当前在那里显示的顺序,但在默认顺序基于聚集索引的SQL表中,这不起作用。您至少需要一个额外的列来将行连接在一起。行上是否至少有一个id?表设计在很多方面都是错误的。即使从您的角度来看,表设计也是错误的。是的,这对我来说非常有效。键是作为列missingItemId的ItemId。非常感谢你