SQL基于所有列复制

时间:2018-07-20 16:51:34

标签: sql sql-server

SELECT FirstName, LastName, MobileNo, COUNT(1) as CNT
FROM CUSTOMER
GROUP BY FirstName, LastName, MobileNo;

类似的事情将基于CustomerFirstNameLastName产生表MobileNo的重复。但是,我想基于所有列(未知)产生一个重复列表。我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以使用校验和(*)

sqlfiddle.com /#!18 / 0a33d / 4

例如。

CREATE TABLE TEST_DATA
  ( Field1 VARCHAR(10),
    Field2 VARCHAR(10)
  );

INSERT INTO TEST_DATA VALUES ('1','1');
INSERT INTO TEST_DATA VALUES ('1','1');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('3','3');

SELECT TD1_CS.*
  FROM (SELECT TD1.*,
               CHECKSUM(*) CS1
          FROM TEST_DATA TD1
        ) TD1_CS
 INNER
  JOIN (SELECT CHECKSUM(*) CS2
          FROM TEST_DATA TD2
         GROUP 
            BY CHECKSUM(*)
        HAVING COUNT(*) > 1
       ) TD2_CS
    ON TD1_CS.CS1 = TD2_CS.CS2

答案 1 :(得分:0)

SELECT  FirstName, LastName, MobileNo, COUNT(*)
FROM    CUSTOMER
GROUP BY FirstName, LastName, MobileNo
HAVING    COUNT(*) > 1