我有一个MS Access查询,我试图转换为SQL Server,因为我更喜欢这个平台,并发现它更实用。我有一个问题,Access将根据一周中的某一天询问您的号码,因为查询正在回顾。我已将该功能添加到SQL Server中(并且更多地基于我们的需求)但我面临的问题是有时结果可能会休息几天。我可以在Access中获得相同的结果,但我必须更改我为day变量输入的数字。两个代码都在下面。
谢谢,
SQL SERVER CODE:
DECLARE @daynumber as int
SET @daynumber = CASE
WHEN DATENAME(dw,GETDATE()) = 'Monday'
THEN 56
WHEN DATENAME(dw,GETDATE()) = 'Tuesday'
THEN 57
WHEN DATENAME(dw,GETDATE()) = 'Wednesday'
THEN 58
WHEN DATENAME(dw,GETDATE()) = 'Thursday'
THEN 59
WHEN DATENAME(dw,GETDATE()) = 'Friday'
THEN 60
END;
SELECT P1.PLANT_CODE,
P1.STOCK_CATEGORY,
P1.MATERIAL,
P1.DISTRIBUTION_VERSION_CODE,
P1.PERIOD_CODE,
P1.REQUIREMENTS_DATE,
P1.VERSION_IND_FLAG,
P1.TECHNICAL_INDEX,
P1.SIZE_LITERAL,
P1.PLANNED_QTY,
P1.WITHDRAWN_QUANTITY,
P1.[REM_PLAN],
P2.FUTURE_FCST,
SUM(I1.ON_HAND_QUANTITY) AS 'TOTAL_OH_INV'
FROM
(SELECT P.PLANT_CODE,
P.STOCK_CATEGORY,
P.MATERIAL,
P.DISTRIBUTION_VERSION_CODE,
P.PERIOD_CODE,
P.REQUIREMENTS_DATE,
P.PLANNED_QTY,
P.VERSION_IND_FLAG,
P.SIZE_LITERAL,
P.WITHDRAWN_QUANTITY,
P.TECHNICAL_INDEX,
P.PLANNED_QTY - P.WITHDRAWN_QUANTITY AS 'REM_PLAN'
FROM VW_PLANNED_REQMNTS_TXT P
WHERE P.PLANT_CODE IN ('6040','6041')
AND P.STOCK_CATEGORY IN ('A60385000','A60385003')
AND P.DISTRIBUTION_VERSION_CODE IN ('00','01','ZU','Z2')
AND P.REQUIREMENTS_DATE < GETDATE() - @daynumber
AND P.PLANNED_QTY > 0) AS P1
LEFT OUTER JOIN
(SELECT
SUM(P.PLANNED_QTY) AS 'FUTURE_FCST',
P.MATERIAL,
P.TECHNICAL_INDEX
FROM VW_PLANNED_REQMNTS_TXT P
WHERE P.REQUIREMENTS_DATE > GETDATE()
AND P.PLANNED_QTY > 0
AND P.STOCK_CATEGORY IN ('A60385000','A60385003')
GROUP BY P.MATERIAL,
P.TECHNICAL_INDEX,
P.SIZE_LITERAL) AS P2
ON P1.MATERIAL = P2.MATERIAL
AND P1.TECHNICAL_INDEX = P2.TECHNICAL_INDEX
LEFT OUTER JOIN
(SELECT I.ON_HAND_QUANTITY,
I.TECHNICAL_INDEX,
I.MATERIAL,
I.STOCK_CATEGORY,
I.PLANT_CODE
FROM VW_INVENTORY I
WHERE I.STOCK_CATEGORY IN ('A60385000','A60385003')
GROUP BY I.TECHNICAL_INDEX,
I.MATERIAL,
I.STOCK_CATEGORY,
I.ON_HAND_QUANTITY,
I.PLANT_CODE) AS I1
ON P1.MATERIAL = I1.MATERIAL
AND P1.TECHNICAL_INDEX = I1.TECHNICAL_INDEX
AND P1.PLANT_CODE = I1.PLANT_CODE
GROUP BY P1.PLANT_CODE,
P1.STOCK_CATEGORY,
P1.MATERIAL,
P1.DISTRIBUTION_VERSION_CODE,
P1.PERIOD_CODE,
P1.REQUIREMENTS_DATE,
P1.PLANNED_QTY,
P1.VERSION_IND_FLAG,
P1.WITHDRAWN_QUANTITY,
P1.SIZE_LITERAL,
P1.TECHNICAL_INDEX,
P1.[REM_PLAN],
P2.FUTURE_FCST
ORDER BY P1.MATERIAL,
P1.TECHNICAL_INDEX;
访问代码:
SELECT dbo_vw_planned_reqmnts_txt.plant_code,
dbo_vw_planned_reqmnts_txt.stock_category,
dbo_vw_planned_reqmnts_txt.material,
dbo_vw_planned_reqmnts_txt.distribution_version_code,
dbo_vw_planned_reqmnts_txt.period_code,
dbo_vw_planned_reqmnts_txt.requirements_date,
dbo_vw_planned_reqmnts_txt.planned_qty AS [REM PLAN QTY],
dbo_vw_planned_reqmnts_txt.version_ind_flag,
dbo_vw_planned_reqmnts_txt.size_literal,
dbo_vw_planned_reqmnts_txt.withdrawn_quantity,
( [planned_qty] + [withdrawn_quantity] ) AS [Original Fcst Qty]
FROM dbo_vw_planned_reqmnts_txt
GROUP BY dbo_vw_planned_reqmnts_txt.plant_code,
dbo_vw_planned_reqmnts_txt.stock_category,
dbo_vw_planned_reqmnts_txt.material,
dbo_vw_planned_reqmnts_txt.distribution_version_code,
dbo_vw_planned_reqmnts_txt.period_code,
dbo_vw_planned_reqmnts_txt.requirements_date,
dbo_vw_planned_reqmnts_txt.planned_qty,
dbo_vw_planned_reqmnts_txt.version_ind_flag,
dbo_vw_planned_reqmnts_txt.size_literal,
dbo_vw_planned_reqmnts_txt.withdrawn_quantity,
( [planned_qty] + [withdrawn_quantity] ),
dbo_vw_planned_reqmnts_txt.technical_index
HAVING ( ( ( dbo_vw_planned_reqmnts_txt.plant_code ) = "6040"
OR ( dbo_vw_planned_reqmnts_txt.plant_code ) = "6041" )
AND ( ( dbo_vw_planned_reqmnts_txt.stock_category ) = "a60385000"
OR ( dbo_vw_planned_reqmnts_txt.stock_category ) = "a60385003" )
AND ( ( dbo_vw_planned_reqmnts_txt.distribution_version_code ) = "00"
OR
( dbo_vw_planned_reqmnts_txt.distribution_version_code ) = "01"
OR
( dbo_vw_planned_reqmnts_txt.distribution_version_code ) = "zu"
OR
( dbo_vw_planned_reqmnts_txt.distribution_version_code ) = "z2" )
AND ( ( dbo_vw_planned_reqmnts_txt.requirements_date ) < Now()
- [m: 56 t:57 w:58 th: 59 f:60] )
AND ( ( dbo_vw_planned_reqmnts_txt.planned_qty ) > 0 ) )
ORDER BY dbo_vw_planned_reqmnts_txt.plant_code,
dbo_vw_planned_reqmnts_txt.stock_category,
dbo_vw_planned_reqmnts_txt.material,
dbo_vw_planned_reqmnts_txt.requirements_date,
dbo_vw_planned_reqmnts_txt.technical_index;