我有一个查询可以构建一个用户列表。我希望该用户列表排除出现在另一个查询生成列表中的用户。例如,第一个查询:
SELECT [UUID]
,[UserName]
,[FirstName]
,[LastName]
,[EmployeeNumber]
,[Inactive]
,[EmployeeType]
FROM [PCA].[dbo].[Users] b Where Plant ='610' AND FirstName != 'Ei'AND FirstName != 'Recovery' AND LastName != 'Production' AND LastName != 'Lab' AND LastName != 'Kiln' AND FirstName != 'Pulp'AND FirstName != 'Roll'AND FirstName != 'Valdosta' AND FirstName != 'Guard'AND FirstName != 'Paper'AND FirstName != 'Power'AND FirstName != 'Powerhouse' AND FirstName != 'E&I' AND LastName != 'Operator' AND LastName != 'Maintenance' AND FirstName != 'Maintenance' AND LastName != 'Tender' AND FirstName != 'Accounting' AND Inactive = 0 And EmployeeType != 'S' AND EmployeeType != 'C'AND EmployeeType != '' AND FirstName <> LastName AND b.UserName in(Select a.USLanID from [HDData].[dbo].[tblUsers] a)
建立145个小时付费用户的列表。现在,第二个列表中有七个用户在查询中描述的指定时间范围内受到了伤害。我希望第一个列表中的所有用户都没有出现在第二个查询列表中。所以那些在第二个查询中受伤的人不应该出现在第一个列表中。在完成所有操作后,我的第一个列表中应该有138个用户。我怎么能这样做我尝试使用“不在”条款,但他们仍然出现。下面是我的第二个列表,它也有一个UUID列名称Employee:
SELECT * FROM [IncidentReporting].[dbo].[IncidentReports] WHERE Classification IN ('RE','FA') AND IncidentDate between DATEADD(Year, -1, '2017-01-01 00:00:00') AND '2017-02-09 00:00:00' AND ForceClosed = 0 AND IncidentType != 'C' AND ApprovalStatus = 'A'
这是我尝试过的,但也会在受伤的用户身上返回相同数量的行。
DECLARE @Date As DateTime
DECLARE @PrevCalenderYear As DateTime
SET @PrevCalenderYear = datetimefromparts(year(@Date), 1, 1, 00, 00, 00, 00)
SET @Date = GetDate()
SELECT [UUID]
,[UserName]
,[FirstName]
,[LastName]
,[EmployeeNumber]
,[Inactive]
,[EmployeeType]
FROM [PCA].[dbo].[Users] b Where Plant ='610' AND FirstName != 'Ei'AND FirstName != 'Recovery' AND LastName != 'Production' AND LastName != 'Lab' AND LastName != 'Kiln' AND FirstName != 'Pulp'AND FirstName != 'Roll'AND FirstName != 'Valdosta' AND FirstName != 'Guard'AND FirstName != 'Paper'AND FirstName != 'Power'AND FirstName != 'Powerhouse' AND FirstName != 'E&I' AND LastName != 'Operator' AND LastName != 'Maintenance' AND FirstName != 'Maintenance' AND LastName != 'Tender' AND FirstName != 'Accounting' AND Inactive = 0 And EmployeeType != 'S' AND EmployeeType != 'C'AND EmployeeType != '' AND FirstName <> LastName AND b.UserName in(Select a.USLanID from [HDData].[dbo].[tblUsers] a) AND b.UUID NOT IN (SELECT c.Employee FROM [IncidentReporting].[dbo].[IncidentReports] c WHERE Classification IN ('RE','FA') AND IncidentDate between DATEADD(Year, -1, @PrevCalenderYear) AND @Date AND ForceClosed = 0 AND IncidentType != 'C' AND ApprovalStatus = 'A')
答案 0 :(得分:1)
DECLARE @Date As DateTime
DECLARE @PrevCalenderYear As DateTime
SET @PrevCalenderYear = datetimefromparts(year(@Date), 1, 1, 00, 00, 00, 00)
SET @Date = GetDate()
select @PrevCalenderYear
返回null
,因为当您调用@Date
设置null
时,@PrevCalenderYear
为DECLARE @Date As DateTime;
DECLARE @PrevCalenderYear As DateTime;
SET @Date = GetDate();
SET @PrevCalenderYear = datetimefromparts(year(@Date), 1, 1, 00, 00, 00, 00);
select @PrevCalenderYear;
。
更正:
2017-01-01 00:00:00
返回:@PrevCalender
在上一次查询中,您从!=
删除了一年,因此我不确定为什么我们不会在开始时这样做。我们还可以使用not in()
清除部分in (select ...)
列表,而不是使用not in select (...)
和exists()
,我们可以使用not exists()
和declare @Date datetime, @PrevCalendarYear datetime;
set @Date = GetDate();
/*2016-01-01*/
set @PrevCalendarYear = dateadd(year , datediff(year , 0, getdate())-1, 0)
select
[uuid]
, [UserName]
, [FirstName]
, [LastName]
, [EmployeeNumber]
, [Inactive]
, [EmployeeType]
from [pca].[dbo].[Users] b
where 1=1
and Plant = '610'
and Inactive = 0
and FirstName <> LastName
and EmployeeType not in ('','C','S')
and FirstName not in (
'Accounting' ,'E&I' ,'Ei' ,'Guard' ,'Maintenance' ,'Paper'
,'Power' ,'Powerhouse' ,'Pulp' ,'Recovery' ,'Roll' ,'Valdosta'
)
and LastName not in (
'Kiln' ,'Lab' ,'Maintenance' ,'Operator' ,'Production' ,'Tender'
)
and exists (
select 1
from [hddata].[dbo].[tblUsers] a
where a.uslanid=b.[UserName]
)
and not exists (
select 1
from [IncidentReporting].[dbo].[IncidentReports] c
where c.Employee = b.[uuid]
and c.Classification in ('re', 'fa')
--and IncidentDate between
--dateadd(Year, - 1, @PrevCalendarYear) /*2016-01-01*/ and @Date
--and IncidentDate >= dateadd(year , datediff(year , 0, getdate())-1, 0)
--and IncidentDate <= getdate()
and c.IncidentDate >= @PrevCalendarYear
and c.IncidentDate <= @Date
and c.ForceClosed = 0
and c.IncidentType != 'C'
and c.ApprovalStatus = 'A'
)
。 (我们也可以更改&#39;日历&#39;日历&#39;。)
create table c as select arc as node_id, state from a;