我希望使用像此Data
这样的东西合并DataTable行 A Ar 1 3
A Ab 3 4
B Bl 3 5
B Bk 5 2
M Mj 4 5
到像这样的DataTable
A Ar 1 3 Ab 3 4
B Bl 3 5 Bk 5 2
M Mj 4 5 Null Null Null
是否有任何TSQL语句可以执行此操作?
答案 0 :(得分:0)
这很简单,因为它的变量数量有限。第一部分是设置临时表,因为我不知道你的列名或任何东西。
fields
=> {"0"=>{"field"=>"something", "field_type"=>"something type", "validation"=>{"enabled"=>"true", "persisted"=>"false", "another thing"=>"false"}}, "1"=>{"field"=>"something else", "field_type"=>"something else type", "validation"=>{"enabled"=>"true", "persisted"=>"false"}}}
copy = fields.reduce({}) do |acc,(k,v)|
acc[k] = {
'fields' => v['field'],
'field_type' => v['field_type'],
'validation' => v['validation'].reduce({}) {|acc,(k,v)| acc[k] = (v == "true" ) ? true : false; acc; }
}
acc
end
=> {"0"=>{"fields"=>"something", "field_type"=>"something type", "validation"=>{"enabled"=>true, "persisted"=>false, "another thing"=>false}}, "1"=>{"fields"=>"something else", "field_type"=>"something else type", "validation"=>{"enabled"=>true, "persisted"=>false}}}
答案 1 :(得分:0)
试试这个:
--create temp table for OP data sample
DECLARE @YourData AS TABLE
(
C1 VARCHAR(1) ,
C2 VARCHAR(2) ,
C3 INT ,
C4 INT
)
INSERT @YourData
( C1, C2, C3, C4 )
VALUES ( 'A', 'Ar', 1, 3 ),
( 'A', 'Ab', 3, 4 ),
( 'B', 'Bl', 3, 5 ),
( 'B', 'Bk', 5, 2 ),
( 'M', 'Mj', 4, 5 )
--final query
;WITH cte
AS ( SELECT C1 ,
C2 ,
C3 ,
C4 ,
ROW_NUMBER() OVER ( PARTITION BY C1 ORDER BY C2 ) AS RN
FROM @YourData
)
SELECT c1.C1 ,
c1.C2 ,
c1.C3 ,
c1.C4 ,
c2.C1 ,
c2.C2 ,
c2.C3 ,
c2.C4
FROM cte AS c1 LEFT JOIN cte AS c2 ON c1.C1 = c2.C1 AND c2.RN > 1
WHERE c1.rn = 1