如何优化我的SQL代码?
我想添加别名。
我正在使用SQL Server Compact Edition。
(...)是SELECT
查询
SELECT
*
FROM ( ... )
WHERE
id IN
(
SELECT
id
FROM ( ... )
GROUP BY
id
HAVING
COUNT( * ) > 1
)
答案 0 :(得分:0)
我建议只使用该查询一次。您可以创建该查询的CTE,然后按如下方式编写查询:
with cte
As
(....)
Select *
from cte
where id in
(select id from cte
group by id
having count(*) > 1)
希望有所帮助
答案 1 :(得分:0)
这是另一种选择:
SELECT * FROM
(SELECT *, COUNT(*) OVER(PARTITION BY id) ids FROM (...)) x
WHERE ids>1