消除串联字符串中的重复项

时间:2018-05-11 14:37:33

标签: sql-server tsql

我有一个工作解决方案,用于将多个记录连接到一个字段,但是我想从连接字段中删除重复项,以及按另一个字段对值进行排序。

这就是我所拥有的:

CREATE VIEW vwImageDescriptions AS
    SELECT i.ItemId, STUFF(ImageDescriptions.line,1,2,'') AS ImageDescriptions
    FROM InventoryItems i
    CROSS APPLY (
        SELECT DISTINCT CAST((select CASE When p.Description = '' or p.Description is null Then '' Else '; ' + p.Description END
        FROM Photos p
        WHERE p.ItemId = i.ItemId
        ORDER BY p.Sequence
        FOR XML PATH('')) AS nvarchar(max)) line
    ) ImageDescriptions
go

select i.ItemName, id.ImageDescriptions
FROM InventoryItems i join vwImageDescriptions id on i.ItemId = id.ItemId
where id.ImageDescriptions like '%pla%'

这是我的结果: enter image description here

我想要的是没有重复的图像描述,所以,例如,我想在每一行中只看到“我的图像”一次,而不是两次。如您所见,我在代码中使用了DISTINCT,但这似乎没有用。

这是一个非常简化的DDL来查看问题:

DROP TABLE IF EXISTS [InventoryItems]
DROP TABLE IF EXISTS Photos
GO
CREATE TABLE [dbo].[InventoryItems](
    [ItemId] [int] NOT NULL,
    [ItemName] [varchar](100) NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Photos](
    [ItemId] [int] NOT NULL,
    [Description] [varchar](175) NULL,
    [Sequence] [int] NOT NULL
) ON [PRIMARY]
GO

INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(311,'11" Round Vegetable Bowl');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(312,'13" Oval Serving Platter');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(313,'19" Oval Serving Platter');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(314,'Creamer');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(315,'Gravy Boat with Attached Underplate');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(317,'Round Butter Dish and Lid');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(318,'Sugar Bowl and Lid');
INSERT INTO [InventoryItems]([ItemId],[ItemName]) VALUES(319,'Vegetable Server Bowl and Lid');

INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(311,'',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(312,'Replacements.com image',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(313,'Replacements.com image',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(313,'Outer platter is the 19"',2)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(313,'Outer platter is the 19"',3)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(313,'Outer platter is the 19"',4)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(313,'Another image',5)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(314,'Replacements.com image',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(315,'From replacements.com',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(315,'My image',2)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(315,'My image',3)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(315,'My image',4)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(316,'My image',1)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(316,'My image',2)
INSERT INTO [Photos]([ItemId],[Description],[Sequence])VALUES(316,'From replacements.com',3)

1 个答案:

答案 0 :(得分:0)

我认为这可以简化一下。据我了解所需的结果,这应该产生你想要的东西。非常感谢表格和样本数据。

select i.ItemId
    , ImageDescriptions = isnull(STUFF((select ',' + isnull(p.Description, '')
            from Photos p
            where p.ItemId = i.ItemId
            group by isnull(p.Description, '')
            order by min(p.Sequence)
            for xml path('')), 1, 1, ''), '')
FROM InventoryItems i
group by i.ItemId

- 编辑 -

就像我说的那样,CROSS APPLY是这里不需要的复杂层。但这应该做同样的事情。

select i.ItemId
    , ImageDescriptions = isnull(x.asdf, '')
FROM InventoryItems i
cross apply
(
    select asdf = STUFF((select ',' + isnull(p.Description, '')
            from Photos p
            where p.ItemId = i.ItemId
            group by isnull(p.Description, '')
            order by min(p.Sequence)
            for xml path('')), 1, 1, '')
) x