编号以逗号分隔的行中的重复名称

时间:2015-05-11 17:39:50

标签: sql sql-server

我陷入了疑问,不知道该怎么做。我需要在逗号分隔的行中为每个重复的名称编号

Sql Query Result

例如在附图中,第7行有两次咪达唑仑。所以在这种情况下,名字应该是咪达唑仑(1),第二名应该是咪达唑仑(2),依此类推。这是否可能以某种方式使用SQL查询?

这是一个生成虚拟数据库和数据的查询:

USE [master]
GO
/****** Object:  Database [DummyDataBase]    Script Date: 10/5/2015 12:42:30 PM ******/
CREATE DATABASE [DummyDataBase]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'DummyDataBase', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\DummyDataBase.mdf' , SIZE = 30720KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'DummyDataBase_log', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\DummyDataBase_log.ldf' , SIZE = 16576KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [DummyDataBase] SET COMPATIBILITY_LEVEL = 110
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [DummyDataBase].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [DummyDataBase] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [DummyDataBase] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [DummyDataBase] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [DummyDataBase] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [DummyDataBase] SET ARITHABORT OFF 
GO
ALTER DATABASE [DummyDataBase] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [DummyDataBase] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [DummyDataBase] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [DummyDataBase] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [DummyDataBase] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [DummyDataBase] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [DummyDataBase] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [DummyDataBase] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [DummyDataBase] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [DummyDataBase] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [DummyDataBase] SET  DISABLE_BROKER 
GO
ALTER DATABASE [DummyDataBase] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [DummyDataBase] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [DummyDataBase] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [DummyDataBase] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [DummyDataBase] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [DummyDataBase] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [DummyDataBase] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [DummyDataBase] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [DummyDataBase] SET  MULTI_USER 
GO
ALTER DATABASE [DummyDataBase] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [DummyDataBase] SET DB_CHAINING OFF 
GO
ALTER DATABASE [DummyDataBase] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [DummyDataBase] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO
USE [DummyDataBase]
GO

GO
/****** Object:  Table [dbo].[TestTable]    Script Date: 10/5/2015 12:42:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TestTable](

    [DrugName] [text] NULL,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
INSERT [dbo].[TestTable] ([DrugName]) VALUES ('Midazolam, Ranitidine, Midazolam, Propofol, Cephazolin, Lignocaine, Propofol, Propofol, Fentanyl, Fentanyl, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Nitrous Ox, Oxygen, Nitrous Ox, Isoflurane, Oxygen, Nitrous Ox, Plasmalyte, Plasmalyte, Plasmalyte, Plasmalyte, Isoflurane, Oxygen, Isoflurane')
INSERT [dbo].[TestTable] ([DrugName]) VALUES ('Midazolam, Ranitidine, Midazolam, Propofol, Cephazolin, Lignocaine, Propofol, Propofol, Fentanyl, Fentanyl, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Isoflurane, Oxygen, Nitrous Ox, Oxygen, Nitrous Ox, Isoflurane, Oxygen, Nitrous Ox, Plasmalyte, Plasmalyte, Plasmalyte, Plasmalyte, Isoflurane, Oxygen, Isoflurane')

USE [master]
GO
ALTER DATABASE [DummyDataBase] SET  READ_WRITE 
GO

1 个答案:

答案 0 :(得分:2)

最终,将值列表存储在单个字段中会产生令人头疼的问题。即使您不希望以规范化方式存储值,解决方案是将列出的值拆分为单独的行。我使用XML功能将字符串拆分为单独的行,然后应用ROW_NUMBER()COUNT()来根据需要更改名称。最后再次使用XML功能重建列表。如果您希望保留列表中名称的顺序,则必须添加编号以便在ORDER BY中使用:

;WITH  cte AS (SELECT RTRIM(LTRIM(Split.a.value('.', 'VARCHAR(100)'))) AS Txt,ID    
               FROM  (SELECT CAST ('<M>' + REPLACE(DrugName, ',', '</M><M>') + '</M>' AS XML) AS DATA
                            ,ID
                      FROM #test
                     ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a))
      ,cte2 AS (SELECT *,CAST(ROW_NUMBER() OVER(PARTITION BY ID,Txt ORDER BY ID,Txt)AS VARCHAR(50)) AS RN
                        ,COUNT(*) OVER(PARTITION BY ID,Txt) AS Dup_CT
                FROM cte
                )
SELECT DISTINCT ID,STUFF((SELECT DISTINCT ',' +   CASE WHEN Dup_CT > 1 THEN Txt+'('+RN+')' ELSE Txt END
                                    FROM cte2 a
                                    WHERE a.ID = b.ID
                                    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)') 
                                    ,1,1,'')

FROM  cte2 b

注意:我假设您有一个ID字段以及每个字符串,如果不是,您需要为此解决方案添加一个字段,以便它可以在分离后区分行。

演示:SQL Fiddle