我编写了一个查询,目前已对该查询进行了硬编码以按部门进行过滤。整个查询都按照我想要的方式工作,我可以复制/粘贴该查询数十次,并将所有结果写入临时表,但是我想要一个更简单,更动态的解决方案。有什么方法可以使我的Forecast表中的FullDepartmentFacilityName的不同结果循环吗?我在这段代码中还有其他一些领域也需要相同的值进行硬编码。
'ABC.ALLXYZ'是我要硬编码的内容
================================================ =====
SELECT *
FROM
[clc].[ForecastTable]
WHERE
FullDepartmentFacilityName = 'ABC.ALLXYZ'
and
DateColumn = CAST(GETDATE() AS DATE)
and
(
(ROUND (PopCountForecast, 0) > (SELECT MAX(ACT.PopCountActual)
FROM
clc.ActualTable ACT
JOIN [dim].[DepartmentTable] DTD
ON DTD.DeptID = ACT.DeptId
JOIN [dim].[FacilityTable] FD
ON FD.FclID = ACT.FclID
WHERE
ACT.DateColumn >= DATEADD(DAY, -28, CAST(GETDATE() AS DATE))
and DATEPART(WEEKDAY, ACT.DateColumn) = DATEPART(WEEKDAY, GETDATE())
and CONCAT(FD.FclName,'.All',DTD.DeptName) = 'ABC.ALLXYZ'))
or
(ROUND (PopCountForecast, 0) < (SELECT MIN(ACT.PopCountActual)
FROM
clc.ActualTable ACT
JOIN [dim].[DepartmentTable] DTD
ON DTD.DeptID = ACT.DeptID
JOIN [dim].[FacilityTable] FD
ON FD.FclID = ACT.FclID
WHERE
ACT.DateColumn >= DATEADD(DAY, -28, CAST(GETDATE() AS DATE))
and DATEPART(WEEKDAY, ACT.DateColumn) = DATEPART(WEEKDAY, GETDATE())
and CONCAT(FD.FclName,'.All',DTD.DeptName) = 'ABC.ALLXYZ'))
)
Table Structure
clc.ForecastTable - this is the main table for my query. this table has one column with a concatenated facility/department name I am calling FullDepartmentFacilityName. This table has forecasted Population Counts by facility/department key by day.
Example:
FullDepartmentFacilityName PopCountForecast Date
ABC.ALLAAA 10 7/16/19
ABC.ALLBBB 5 7/16/19
ABC.ALLCCC 8 7/16/19
BCA.ALLAAA 9 7/16/19
BCA.ALLBBB 4 7/16/19
BCA.ALLCCC 9 7/16/19
"dim.DepartmentTable" - this has all department IDs and their corresponding names
"dim.FacilityTable" - this has all facility IDs and their corresponding names
"clc.ActualTable" - this table contains real data and has a facility column and department column but not a concatenated facility/department column. This is why I created one in my query.
答案 0 :(得分:0)
您尝试过CROSS APPLY
吗?您不提供表结构,所以我只能猜测表结构是什么。
SELECT *
FROM (
SELECT DISTINCT FullDepartmentFacilityName
FROM Forecast
) d
CROSS APPLY (
SELECT *
FROM [clc].[ForecastTable]
WHERE FullDepartmentFacilityName = d.FullDepartmentFacilityName
and DateColumn = CAST(GETDATE() AS DATE)
and (
(ROUND (PopCountForecast, 0) > (
SELECT MAX(ACT.PopCountActual)
FROM clc.ActualTable ACT
JOIN [dim].[DepartmentTable] DTD ON DTD.DeptID = ACT.DeptId
JOIN [dim].[FacilityTable] FD ON FD.FclID = ACT.FclID
WHERE ACT.DateColumn >= DATEADD(DAY, -28, CAST(GETDATE() AS DATE))
and DATEPART(WEEKDAY, ACT.DateColumn) = DATEPART(WEEKDAY, GETDATE())
and CONCAT(FD.FclName,'.All',DTD.DeptName) = 'ABC.ALLXYZ')
) or (
ROUND (PopCountForecast, 0) < (
SELECT MIN(ACT.PopCountActual)
FROM clc.ActualTable ACT
JOIN [dim].[DepartmentTable] DTD ON DTD.DeptID = ACT.DeptID
JOIN [dim].[FacilityTable] FD ON FD.FclID = ACT.FclID
WHERE ACT.DateColumn >= DATEADD(DAY, -28, CAST(GETDATE() AS DATE))
and DATEPART(WEEKDAY, ACT.DateColumn) = DATEPART(WEEKDAY, GETDATE())
and CONCAT(FD.FclName,'.All',DTD.DeptName) = 'ABC.ALLXYZ')
)
)
) c