0
SELECT .... ColumnNames ...
INTO [FOUND_DUPLICATES] 
FROM [FIND_DUPLICATES] AS FD
WHERE FD.[Contract No] IN 
    (SELECT [Contract No],
            [Vehicle Identity number (VIN)],
            COUNT(*) AS Anzahl 
    FROM FIND_DUPLICATES
    GROUP BY 
            [Contract No],
            [Vehicle Identity number (VIN)]
    HAVING COUNT(*) >1)

在这里你可以看到我想要的:)

查找重复项并将它们复制到另一个表。但是使用这段代码我得到一个错误:

当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

用于查找重复项的 SELECT 语句运行良好。但是我无法使用 Select Into 仅选择重复项并将其复制到新表中。

我希望你能理解我想要什么,任何人都可以帮助我:)

//编辑:我使用 SQL Server 2008 R2

4

3 回答 3

0

你可以使用这个:

SELECT  .... ColumnNames ...

INTO [FOUND_DUPLICATES] 
FROM [FIND_DUPLICATES] AS FD
WHERE FD.[Contract No] IN
(
  SELECT 
    [Contract No]
  FROM FIND_DUPLICATES
  GROUP BY 
     [Contract No],
     [Vehicle Identity number (VIN)]
  HAVING COUNT(*) >1
)

或使用相关子查询:

SELECT .... ColumnNames ...

INTO [FOUND_DUPLICATES] 
FROM [FIND_DUPLICATES] AS FD
WHERE EXISTS 
(
    SELECT 1
    FROM FIND_DUPLICATES FD1
    WHERE FD.[Contract No] = FD1.[Contract No]
    GROUP BY 
        FD1.[Contract No],
        FD1.[Vehicle Identity number (VIN)]
    HAVING COUNT(*) >1
)
于 2014-04-02T13:50:59.240 回答
0

添加另一个子查询应该有效吗?

Select  .... ColumnNames ...

INTO [FOUND_DUPLICATES] 
    FROM [FIND_DUPLICATES]
    AS FD
    where FD.[Contract No] 
       IN (  Select [Contract No] from (Select 
        [Contract No],
        [Vehicle Identity number (VIN)],
        COUNT(*) AS Anzahl 
    from FIND_DUPLICATES
    group by 
        [Contract No],
        [Vehicle Identity number (VIN)]
    having count(*) >1)x)
于 2014-04-02T13:51:01.853 回答
0
Select  .... ColumnNames ...

INTO [FOUND_DUPLICATES] 
    FROM [FIND_DUPLICATES]
    AS FD
    where FD.[Contract No] 
       IN (
       select [Contract No] from (Select --inner sub query 
        [Contract No],
        [Vehicle Identity number (VIN)],
        COUNT(*) AS Anzahl 
    from FIND_DUPLICATES
    group by 
        [Contract No],
        [Vehicle Identity number (VIN)]
    having count(*) >1) )
于 2014-04-02T13:56:43.083 回答