Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构建SQL列表,该列表基于其他表上没有的内容返回表_Sql_Sql Server_Tsql - Fatal编程技术网

构建SQL列表,该列表基于其他表上没有的内容返回表

构建SQL列表,该列表基于其他表上没有的内容返回表,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个查询,它为我建立了一个用户列表。我希望该用户列表排除出现在另一个查询生成的列表中的用户。例如,第一个查询: SELECT [UUID] ,[UserName] ,[FirstName] ,[LastName] ,[EmployeeNumber] ,[Inactive] ,[EmployeeType] FROM [PCA].[dbo].[Users] b Where Plant ='610' AND FirstName

我有一个查询,它为我建立了一个用户列表。我希望该用户列表排除出现在另一个查询生成的列表中的用户。例如,第一个查询:

    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) 
这是我尝试过的,但返回的行数与受伤用户的行数相同

    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')
返回null,因为在调用@Date设置@PrevCalendarYear时@Date为null

更正:

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

在上一个查询中,您从@PrevCalendar中删除了一年,因此我不确定为什么不在开始时这样做。我们还可以清理一些垃圾!=列表使用not in,而不是使用in select。。。而不是在选择。。。我们可以使用存在和不存在。我们还可以将“日历”更改为“日历”

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'
  )

请不要标记与您的问题无关的数据库。根据问题@John Conde中显示的语法添加的sql server标记、用户表和事件报告表与我尝试构建的查询相关。。。。
DECLARE @Date As DateTime;
DECLARE @PrevCalenderYear As DateTime;

SET @Date = GetDate();
SET @PrevCalenderYear = datetimefromparts(year(@Date), 1, 1, 00, 00, 00, 00);

select @PrevCalenderYear;
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'
  )