SQL结合基于公共行值

时间:2017-11-13 20:17:56

标签: sql-server merge key sql-server-2017

我正在尝试从一个帮助台软件迁移到另一个,其中一部分是迁移我们的故障单历史数据库,我将其导出到Excel电子表格,然后导入到SQL Server数据库中。

该表的当前格式如下:

列标题:

Description: Initial ticket text  
ticketnum: Ticket number  
subject: Ticket subject header  
value: ticket comment  

如果需要:

Description: nvarchar(max)  
ticketnum: float  
subject: nvarchar(max)  
value: nvarchar(max)  

编辑:询问的示例数据:

Description            TicketNum     Subject                       Value
-------------------------------------------------------------------------
Our computer exploded   10020        CPU Explosion        Our computer exploded
Our computer exploded   10020        CPU Explosion        The computer is a dell
Virus Found             10021        Virus                We need help with a virus

我希望它成为:

Description            TicketNum     Subject              Value
-------------------------------------------------------------------------
Our computer exploded   10020        CPU Explosion        Our computer exploded; The computer is a dell
Virus Found             10021        Virus                We need help with a virus

每一行都是票证的新条目,因此值字段始终是唯一的,而每个票号的描述和值保持不变。所以我希望根据票号字段合并行,保留/合并值字段的所有数据,并且只保留主题和描述字段的一个值。我已经看到了一些接近我的场景的问题,但没有一个匹配,遗憾的是我对SQL知之甚少,并且不足以根据我所看到的来解释这个问题。

提前非常感谢你。

编辑2:修改代码,运行但不合并,产生一个空表:

declare @tmpTable table ([Description] nvarchar(max), niceid float, 
[Subject] nvarchar(max), [Value] nvarchar(max))

SELECT * INTO tmpTable FROM sheetnew$

declare @newTable table ([Description] nvarchar(max), niceid float, 
[Subject] nvarchar(max), [Value] nvarchar(max))


insert into @newTable
select distinct 
    x.[Description]
    ,x.niceID
    ,x.[Subject]
   ,[Value] = stuff((
          select '; ' + y.[Value]
          from @tmptable y
          where x.niceid = y.niceid
          for xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from @tmptable x

1 个答案:

答案 0 :(得分:0)

这是使用b<-as.POSIXct(a) head(b) [1] "2016-07-05 20:53:47.165 GMT" "2016-07-05 21:45:00.484 GMT" [3] "2016-07-05 21:45:30.030 GMT" "2016-07-05 21:45:52.105 GMT" [5] "2016-07-05 21:45:52.105 GMT" "2016-07-05 21:45:52.106 GMT"

的方法
STUFF
相关问题