用于查找重复

时间:2017-09-07 07:05:24

标签: sql-server

我有一个返回下面提到的输出的查询:

查询:

  SELECT
        [Customer Number],
        [Name 1],
        [Tax Number 1],
        [First telephone number],
        [Customer's credit limit],
        COUNT([Customer's credit limit]) count1
    FROM base_file
    GROUP BY
        [Customer Number],
        [Name 1],
        [Tax Number 1],
        [First telephone number],
        [Customer's credit limit]
    HAVING COUNT([Customer's credit limit]) > 1;

输出:

Customer Number Name 1  Tax Number 1    First telephone number  Customer's credit limit count1
24  Test customer 1     985743678   200000  8
29  Reliance            0   25
29  Reliance            100 25
29  Reliance            100000  25
29  Reliance            150000  25
30  Reliance Co.        985743678   200000  36
34  Test Customer 2211 3        985743678   500000  31
35  Test Customer 2211 4            1000000 17
186 test            0   21
186 test            10000000000 21
186 test            3000    21
201 XAL TEST            8000    3
211 pankaj          50000000    13
213 End Customer            10000   5
228 Datta bhai...Pune           0.01    2
228 Datta bhai...Pune           3000    2

现在,我希望有一个子查询,它只能提供具有不同信用额度的重复客户的记录。 所以,我的最后一个应该只包含记录:

29  Reliance            0
29  Reliance            100
29  Reliance            100000
29  Reliance            150000
186 test            0
186 test            10000000000
186 test            3000
228 Datta bhai...Pune           0.01
228 Datta bhai...Pune           3000

2 个答案:

答案 0 :(得分:0)

使用COUNT作为分析函数:

SELECT
    t.[Customer Number],
    t.[Name 1],
    t.[Tax Number 1],
    t.[First telephone number]
FROM
(
    SELECT [Customer Number], [Name 1], [Tax Number 1], [First telephone number],
           [Customer's credit limit],
           COUNT(*) OVER (
               PARTITION BY [Customer Number], [Name 1], [Tax Number 1],
                            [First telephone number]) cnt
    FROM base_file
) t
WHERE t.cnt > 1;

答案 1 :(得分:0)

或者......

    ;WITH    tmpBase1
                  AS ( SELECT   [Customer Number] ,
                                [Name 1] ,
                                [Tax Number 1] ,
                                [First telephone number] ,
                                [Customer's credit limit]
                       FROM     base_file
                       WHERE    EXISTS ( SELECT 1
                                         FROM   ( SELECT    [Customer Number]
                                                  FROM      base_file
                                                  GROUP BY  [Customer Number]
                                                  HAVING    COUNT([Customer Number]) > 1
                                                ) T
                                         WHERE  t.[Customer Number] = base_file.[Customer Number] )
                     ),
                tmpBase2
                  AS ( SELECT   [Customer Number] ,
                                [Customer's credit limit]
                       FROM     tmpBase1
                       GROUP BY [Customer Number] ,
                                [Customer's credit limit]
                       HAVING   COUNT(*) < 2
                     )
            SELECT  *
            FROM    tmpBase1
            WHERE   EXISTS ( SELECT 1
                             FROM   tmpBase2
                             WHERE  tmpBase1.[Customer Number] = tmpBase2.[Customer Number] )