Select into Table from Table2 where column in (Subquery)
Posted
技术标签:
【中文标题】Select into Table from Table2 where column in (Subquery)【英文标题】: 【发布时间】:2014-04-02 13:44:27 【问题描述】: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
【问题讨论】:
嗯...我的缩进有点错误,但这应该有助于提高可读性。 【参考方案1】:添加另一个子查询应该可以吗?
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)
【讨论】:
【参考方案2】:你可以用这个:
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
)
【讨论】:
【参考方案3】: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) )
【讨论】:
以上是关于Select into Table from Table2 where column in (Subquery)的主要内容,如果未能解决你的问题,请参考以下文章
Select into Table from Table2 where column in (Subquery)
“With”命令比使用临时表慢 Select * into #table1 from
select into from 和 insert into select 的用法和区别
oracle数据库表复制insert into select from跟create table as select * from 两种表复制语句区别