开发者

SQL server code (UPDATE)

开发者 https://www.devze.com 2023-04-12 12:25 出处:网络
i ha开发者_运维知识库ve a problem of how to update data from table A to table B andmake in horizontal from vertical..example is

i ha开发者_运维知识库ve a problem of how to update data from table A to table B and make in horizontal from vertical..example is

Table A                -----------        Table B

ID -----   Item        -----------      ID ------Item

11---      iPhone4     -----------      11 ------iPhone4,iPhone4s,iPhone5   
11---      iPhone4s   
11---      iPhone5 

mean that from 3 Rows become 1 rows. Please help,really noob in this case.. Thanks


Try with this..

Select ID,        
Left(Item,Len(Item)-1) As Items 
From(
    Select distinct T2.ID,
    (Select 
    T1.Item + ',' AS [text()]             
    From TableA T1                          
    where T1.ID = T2.ID
    For XML PATH ('')) [item]       
    From dbo.TableA T2  
    ) t 


Try this..Use GROUP_CONCAT to merge in one row the rows you want to merge.

SELECT ID,GROUP_CONCAT(item)
  FROM yourtable
 GROUP BY ID;
0

精彩评论

暂无评论...
验证码 换一张
取 消