SQL 除了运算符和 CTE 到 select 第一个查询中的所有项目,但不在第二个查询中

SQL Except Operator and with CTE to select all the items in 1st query but not in 2nd Query

我正在尝试获取下面第一个查询中的所有数据,但下面第二个查询中的数据除外。

在这里,我首先尝试使用 with ctepartition by.

select 独特的 data/distinct 数据

我尝试使用 except,但出现此错误:

Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.***

First query:

    With cte as
    (
        select 
            *, 
            row_number() over (partition by [Employee ID] order by [QTR] DESC, Wk desc) rownumber 
        from 
            tbl_HC
    )
    select * 
    from cte 
    where rownumber = 1
      and QTR = (Select max(QTR) from tbl_HC)

    Except

    --2nd query
    With cte as
    (
         select 
             *, 
             row_number() over (partition by [Employee ID] order by [QTR] DESC, Wk desc) rownumber 
         from  
             tbl_HC
    )
    select * 
    from cte 
    where rownumber = 1
        and Wk= (
        Select max(Wk) from tbl_HC
        where QTR = (Select max(QTR) from tbl_HC))`

您的查询如下所示

With cte as
    (select *, row_number() 
    over(partition by [Employee ID] order by [QTR] DESC,Wk DESC) rownumber 
    from tbl_HC
    ), cte1 as 
    (
     select *, row_number() 
    over(partition by [Employee ID] order by [QTR] DESC,Wk DESC) rownumber 
    from tbl_HC
    )
    select * from cte 
    where rownumber =1
    and QTR= (Select max(QTR) from tbl_SDS_Headcount_Manageby)
    except

    select * from cte1
    where rownumber =1
    and Wk= (
    Select max(Wk) from tbl_HC
    where QTR = (Select max(QTR) from tbl_HC))