T-SQL, SQL Server Compact Edition, Alias For SELECT

泪湿孤枕 提交于 2019-12-13 06:45:30

问题


How can I optimize my SQL code ?

I want to add alias.

I am using SQL Server Compact Edition.

( ... ) is a SELECT query

SELECT
    *
FROM ( ... )
WHERE
    id IN
    (
        SELECT
            id
        FROM ( ... )
        GROUP BY
            id
        HAVING
            COUNT( * ) > 1
    )

回答1:


I would suggest to use that query only once. You can create a CTE of that query and then write the query as follows:

with cte
As
(....)
Select * 
    from cte 
    where id in 
        (select id from cte 
            group by id 
            having count(*) > 1)

Hope it helps




回答2:


this is another option:

SELECT * FROM
(SELECT *, COUNT(*) OVER(PARTITION BY id) ids FROM (...)) x
WHERE ids>1


来源:https://stackoverflow.com/questions/23406467/t-sql-sql-server-compact-edition-alias-for-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!