查询SQL Server中的重复记录?

时间:2017-05-18 11:37:55

标签: sql sql-server ms-query

我有这张桌子:

   +----------+----------+---------+-----------------------------------+
| Class A  | Class  B | Class C | Result_XML                        |
+----------+----------+---------+-----------------------------------+
| SUPER    | PREMIUM  | A       | <Array> <Ser ref="RF124" r="200”> |
| ECONOMIC | SEMI     | A       | <Array> <Ser ref="RF124" r="200”> |
| SUPER    | PREMIUM  | A       | <Array> <Ser ref="RF144" r="500”> |
| SUPER    | NA       | B       | <Array> <Ser ref="RF124" r="200”> |
| ECONOMIC | SEMI     | A       | <Array> <Ser ref="RF154" r="200”> |
| SUPER    | PREMIUM  | C       | <Array> <Ser ref="RF124" r="100”> |
+----------+----------+---------+-----------------------------------+

而且,我一直试图得到的东西如下:

+----------+---------+--------+---------+
|  ClassA  | ClassB  | ClassC | Result  |
+----------+---------+--------+---------+
| SUPER    | PREMIUM | A      | 200,500 |
| ECONOMIC | SEMI    | A      |     200 |
| SUPER    | NA      | B      |     200 |
| SUPER    | PREMIUM | C      |     100 |
+----------+---------+--------+---------+

基本上上面是来自第一个表的记录的不同列表,在最后一列中具有相同的结果,如果不同则将结果填入相同的行,如第一个示例。到目前为止,我想出了以下内容,但显然无法正常工作。提前感谢您的帮助:

SELECT DISTINCT
    ClassA, ClassB, ClassC,
    Result = (STUFF((Select Distinct ',' + E1.RESULT_XML.value('(/Array/Ser/@r)[1]', 'varchar(max)')  
                     From listtable E2 
                     Where E1.ClassA = E2.ClassA 
                       And E1.ClassB = E2.ClassB 
                       And E1.ClassC = E2.ClassC
                     FOR XML PATH(''), TYPE, ROOT).value('root[1]','nvarchar(max)'),1,1,''))
FROM 
    listtable E1 

3 个答案:

答案 0 :(得分:1)

你应该在东西查询中使用E2.ResultXml而不是E1.ResultXml。

DECLARE @T TABLE (ClassA NVARCHAR(50), ClassB NVARCHAR(50), ClassC NVARCHAR(50), ResultXML NVARCHAR(300))

INSERT @T SELECT 'SUPER','PREMIUM'  ,'A','200'--'<Array> <Ser ref="RF124" r="200”>' 
INSERT @T SELECT 'ECONOMIC','SEMI'  ,'A','200'--'<Array> <Ser ref="RF124" r="200”>' 
INSERT @T SELECT 'SUPER','PREMIUM'  ,'A','500'--'<Array> <Ser ref="RF144" r="500”>' 
INSERT @T SELECT 'SUPER','NA'       ,'B','200'--'<Array> <Ser ref="RF124" r="200”>' 
INSERT @T SELECT 'ECONOMIC','SEMI'  ,'A','200'--'<Array> <Ser ref="RF154" r="200”>' 
INSERT @T SELECT 'SUPER','PREMIUM'  ,'C','100'--'<Array> <Ser ref="RF124" r="100”>' 

SELECT * FROM @T


SELECT 
    ClassA,ClassB,ClassC,
    Result = (STUFF((Select  DISTINCT ',' + E2.RESULTXML  
                     From @T E2 
                     Where E1.ClassA = E2.ClassA 
                       And E1.ClassB = E2.ClassB 
                       And E1.ClassC = E2.ClassC
                     FOR XML PATH('')),1,1,''))
FROM 
    @T E1 
GROUP BY
    ClassA, ClassB, ClassC

产量---&GT;

ClassA      ClassB     ClassC  Result
ECONOMIC    SEMI       A       200
SUPER       NA         B       200
SUPER       PREMIUM    A       200,500
SUPER       PREMIUM    C       100

答案 1 :(得分:0)

;with cte( ClassA  , ClassB , ClassC , Result_XML)
AS
(
SELECT 'SUPER'    , 'PREMIUM'  , 'A'       , '<Array> <Ser ref="RF124" r="200”>' Union all 
SELECT 'ECONOMIC' , 'SEMI'     , 'A'       , '<Array> <Ser ref="RF124" r="200”>' Union all 
SELECT 'SUPER'    , 'PREMIUM'  , 'A'       , '<Array> <Ser ref="RF144" r="500”>' Union all 
SELECT 'SUPER'    , 'NA'       , 'B'       , '<Array> <Ser ref="RF124" r="200”>' Union all 
SELECT 'ECONOMIC' , 'SEMI'     , 'A'       , '<Array> <Ser ref="RF154" r="200”>' Union all 
SELECT 'SUPER'    , 'PREMIUM'  , 'C'       , '<Array> <Ser ref="RF124" r="100”>' 
)

,Result AS (
    SELECT ClassA
        ,ClassB
        ,ClassC
        ,CAST(REPLACE(RIGHT(Result_XML, 5), '”>', '') AS INT) AS Result_XML
    FROM cte
    )

SELECT DISTINCT ClassA
    ,ClassB
    ,ClassC
    ,STUFF((
            SELECT DISTINCT ', ' + CAST(i.Result_XML AS VARCHAR(10))
            FROM Result i
            WHERE i.ClassA = o.ClassA
                AND i.ClassB = o.ClassB
                AND i.ClassC = o.ClassC
            FOR XML PATH('')
            ), 1, 1, '') AS Result
FROM Result o
ORDER BY ClassC
    ,Result DESC

输出

+----------+---------+--------+---------+
|  ClassA  | ClassB  | ClassC | Result  |
+----------+---------+--------+---------+
| SUPER    | PREMIUM | A      | 200,500 |
| ECONOMIC | SEMI    | A      |     200 |
| SUPER    | NA      | B      |     200 |
| SUPER    | PREMIUM | C      |     100 |
+----------+---------+--------+---------+

答案 2 :(得分:0)

我有一个问题的解决方案,但我相信它可以更有效地解决。

SELECT
    ClassA
    ,ClassB
    ,ClassC
    ,Result = (STUFF((SELECT DISTINCT
            ',' + CAST(E1.Result AS XML).value('(/Array/Ser/@r)[1]', 'varchar(max)')
        FROM [OrchestratorNotifications].[dbo].[Kuko] E2
        WHERE E1.ClassA = E2.ClassA
        AND E1.ClassB = E2.ClassB
        AND E1.ClassC = E2.ClassC
        FOR XML PATH (''), TYPE, ROOT)
    .value('root[1]', 'nvarchar(max)'), 1, 1, '')) INTO #tmp1
FROM [OrchestratorNotifications].[dbo].[Kuko] E1
ORDER BY ClassA
    ,ClassB
    ,ClassC

SELECT ClassA
    ,ClassB
    ,ClassC
    ,Result FROM (
SELECT *,ROW_NUMBER() OVER( PARTITION BY ClassA,ClassB,ClassC ORDER BY ClassA,ClassB,ClassC,w DESC) AS r1 FROM( 
SELECT
    ClassA
    ,ClassB
    ,ClassC
    ,o.Result,
    o.w, 
    ROW_NUMBER() OVER( PARTITION BY ClassA,ClassB,ClassC,o.w  ORDER BY ClassA,ClassB,ClassC,o.w) AS rownum
FROM #tmp1 a
CROSS APPLY (SELECT
        CASE
            WHEN a.Result = b.Result THEN a.Result
            ELSE a.Result + ',' + b.Result
        END Result,
            CASE
            WHEN CAST( a.Result AS INT) = CAST( b.Result AS INT) THEN  CAST( a.Result AS INT) 
            ELSE CAST( a.Result AS INT)  + CAST( b.Result AS INT) 
        END w
    FROM #tmp1 b
    WHERE a.ClassA = b.ClassA
    AND a.ClassB = b.ClassB
    AND a.ClassC = b.ClassC) o
    )y 
    where y.rownum=1
    )d 
    WHERE d.r1=1
    ORDER BY d.ClassC

DROP TABLE #tmp1