取消选择“不在此查询中工作”

时间:2018-04-27 11:17:38

标签: sql sql-server

不知道为什么取消选择不起作用。

select top 20 * from (
    Select  Rec_Num, 
    COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
    as 'ApprovedBy' FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
where ApprovedBy LIKE '%2459%'
deselect
select top 10 * from (
    Select  Rec_Num, 
    COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
    as 'ApprovedBy' FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
where ApprovedBy LIKE '%2459%'

任何想法我做错了什么?

下面的一个正在运作......但不是上面的......

select top 20 Rec_Num,E_No,E_Name FROM EmployeeDB
except
select top 10 Rec_Num,E_No,E_Name FROM EmployeeDB

2 个答案:

答案 0 :(得分:1)

尝试以下查询

select top 20 * from (
    Select  Rec_Num, 
    COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
    as 'ApprovedBy' FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
where ApprovedBy LIKE '%2459%'
EXCEPT 
select top 10 * from (
    Select  Rec_Num, 
    COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
    as 'ApprovedBy' FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
where ApprovedBy LIKE '%2459%'

或使用OFFSET您可以选择下面的10行,如下所示

select * from (
        Select  Rec_Num, 
        COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
        as 'ApprovedBy' FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
    where ApprovedBy LIKE '%2459%' OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

或者您可以使用row_number选择接下来的10行,如下所示

select * from (
    Select  Rec_Num, 
    COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
    as 'ApprovedBy',
    ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN
    FROM FO_Entry INNER JOIN Employees ON FO_Entry.Hq_Code = Employees.E_HQCode)  as abc
where ApprovedBy LIKE '%2459%'
AND RN>=10 AND RN<=20

答案 1 :(得分:1)

要选择第11行到第20行,请使用提取offset跳过10行,然后使用fetch first选择以下10行:

select
...
ORDER BY somecolumn
OFFSET 10 FETCH FIRST 10 ROWS ONLY