我目前正在使用以下SQL从我们的某个数据库生成一些统计信息,但是创建的2个初始临时表使用几乎相同的SELECT语句。
是否可以重写此内容并避免使用两个几乎相同的语句?
感谢您提供的任何帮助!
------------------------------------------------------------------------------------------------------------------------------------------------------------------ QUERY
DECLARE @StartDate DATE,
@EndDate DATE
SELECT @StartDate = ('2015-08-20'),
@EndDate = DateAdd(day,1,@StartDate)
-------------------------------------------------------------------------------------------- Create Temp Tables
------------------------------------------------ #Submitted
SELECT DISTINCT(e.[EmployeeId])
INTO [#Submitted]
FROM [Activity] a
INNER JOIN [Employee] e ON e.[EmployeeId] = a.[EmployeeId]
INNER JOIN [ActivityTypeLookup] atl ON atl.[ActivityTypeID] = a.[ActivityTypeId]
WHERE e.[EmployeeId] > 1
AND e.[EmployeeTypeId] = 1
AND a.[ImpersonatorId] > 1
AND atl.[ActivityTypeID] = 22 -- 22 = Submit Command
AND a.[TimeStamp] >= @StartDate
AND a.[TimeStamp] < @EndDate
AND e.[LeavingDate] IS NULL
------------------------------------------------ #LoggedIn
SELECT DISTINCT(e.[EmployeeId])
INTO [#LoggedIn]
FROM [Activity] a
INNER JOIN [Employee] e ON e.[EmployeeId] = a.[EmployeeId]
INNER JOIN [ActivityTypeLookup] atl ON atl.[ActivityTypeID] = a.[ActivityTypeId]
WHERE e.[EmployeeId] > 1
AND e.[EmployeeTypeId] = 1
AND a.[ImpersonatorId] > 1
AND atl.[ActivityTypeID] = 1 -- 1 = Signed In
AND a.[TimeStamp] >= @StartDate
AND a.[TimeStamp] < @EndDate
AND e.[LeavingDate] IS NULL
-------------------------------------------------------------------------------------------- / Create Temp Tables
------------------------------------------------ Output Data
SELECT DISTINCT e.[EmployeeKey],
e.[FirstName],
e.[LastName],
[Description] = CASE
WHEN s.[EmployeeId] IS NOT NULL THEN 'Submitted Command'
WHEN li.[EmployeeId] IS NOT NULL THEN 'Logged In' ELSE 'Not Logged In'
END,
e.[Email]
FROM [Employee] e
INNER JOIN [Activity] a ON e.[EmployeeId]=a.[EmployeeId]
LEFT JOIN [#Submitted] s ON e.[EmployeeId]=s.[EmployeeId]
LEFT JOIN [#LoggedIn] li ON e.[EmployeeId]=li.[EmployeeId]
WHERE e.[LeavingDate] IS NULL
AND e.[EmployeeTypeId] = 1
AND a.[ImpersonatorId] > 1
ORDER BY [Description]
------------------------------------------------ Drop Temp Tables
DROP TABLE #LoggedIn
DROP TABLE #Submitted
------------------------------------------------------------------------------------------------------------------------------------------------------------------ / QUERY
答案 0 :(得分:0)
在<asp:GridView>
条款中,您可以同时过滤WHERE
,然后按照以下方式对该ID进行ActivityTypeID
.. CASE
:
WHEN
答案 1 :(得分:0)
请检查以下脚本 你可以看到你不需要临时表 对不起,如果有一些语法错误:)
SELECT DISTINCT e.[EmployeeKey],
e.[FirstName],
e.[LastName],
[Description] = CASE
WHEN atl.[ActivityTypeID] = 22 THEN 'Submitted Command'
WHEN atl.[ActivityTypeID] = 1 THEN 'Logged In'
ELSE 'Not Logged In'
END,
e.[Email]
FROM [Employee] e
INNER JOIN [Activity] a ON e.[EmployeeId]=a.[EmployeeId]
INNER JOIN [ActivityTypeLookup] atl ON atl.[ActivityTypeID] = a.[ActivityTypeId]
WHERE e.[LeavingDate] IS NULL
AND e.[EmployeeTypeId] = 1
AND a.[ImpersonatorId] > 1
and atl.[ActivityTypeID] IN (1, 22)
AND a.[TimeStamp] >= @StartDate
AND a.[TimeStamp] < @EndDate
AND e.[LeavingDate] IS NULL
ORDER BY [Description]