SQL 基于公共行值合并
SQL combine based on common row value
我正在尝试从一个服务台软件迁移到另一个,其中一部分是迁移我们的工单历史数据库,我将其导出到 excel 电子表格,然后导入到 SQL服务器数据库。
目前table的格式如下:
第 headers 列:
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
每一行都是工单的新条目,因此值字段始终是唯一的,而每个工单号的描述和值保持不变。因此,我希望根据票号字段合并行,keep/merge 值字段的所有数据,并且只保留主题和描述字段的一个值。我已经看到了很多接近我的场景的问题,但是 none 匹配,遗憾的是我对 SQL 知之甚少,并且不足以根据我所看到的进行推断来解决这个问题独唱
在此先感谢您。
编辑 2:修改后的代码,运行但不合并,生成一个空 table:
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
这是使用 STUFF
的方法
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_WARNINGS ON
GO
SET ANSI_PADDING ON
GO
declare @table table ([Description] varchar(64), TicketNum int, [Subject] varchar(64), [Value] varchar(4000))
insert into @table
values
('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')
declare @newTable table ([Description] varchar(64), TicketNum int, [Subject] varchar(64), [Value] varchar(4000))
insert into @newTable
select distinct
x.[Description]
,x.TicketNum
,x.[Subject]
,[Value] = stuff((
select '; ' + y.[Value]
from @table y
where x.TicketNum = y.TicketNum
for xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from @table x
select * from @newTable
我正在尝试从一个服务台软件迁移到另一个,其中一部分是迁移我们的工单历史数据库,我将其导出到 excel 电子表格,然后导入到 SQL服务器数据库。
目前table的格式如下:
第 headers 列:
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
每一行都是工单的新条目,因此值字段始终是唯一的,而每个工单号的描述和值保持不变。因此,我希望根据票号字段合并行,keep/merge 值字段的所有数据,并且只保留主题和描述字段的一个值。我已经看到了很多接近我的场景的问题,但是 none 匹配,遗憾的是我对 SQL 知之甚少,并且不足以根据我所看到的进行推断来解决这个问题独唱
在此先感谢您。
编辑 2:修改后的代码,运行但不合并,生成一个空 table:
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
这是使用 STUFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_WARNINGS ON
GO
SET ANSI_PADDING ON
GO
declare @table table ([Description] varchar(64), TicketNum int, [Subject] varchar(64), [Value] varchar(4000))
insert into @table
values
('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')
declare @newTable table ([Description] varchar(64), TicketNum int, [Subject] varchar(64), [Value] varchar(4000))
insert into @newTable
select distinct
x.[Description]
,x.TicketNum
,x.[Subject]
,[Value] = stuff((
select '; ' + y.[Value]
from @table y
where x.TicketNum = y.TicketNum
for xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from @table x
select * from @newTable