我有一个库存报告,其中有一个component_number,next_date,周期替换时间,到期日期列。如果下一个日期是今年,那么应该显示今年的替换项,那么next_date应该是next_date +周期时间,那么替换值应该显示(next_year,next_year +周期时间...)。 component_number直到到期日期。此递归直到<= 2060。我的SQL显示下一个日期的值。但不适用于next_date +周期时间。
示例:对于组件编号为10009的next_date是2030,循环时间= 20.expiry_date是2050。因此该值应出现在2030,2050中。报表仅运行到<= 2060。
ALTER PROCEDURE [dbo].[Asset]
@BUSINESS_STREAM NVARCHAR(MAX),
@Region NVARCHAR(MAX),
@Patch NVARCHAR(MAX),
@Scheme NVARCHAR(MAX),
@AM_REGION NVARCHAR(MAX)
AS
select * into #temp
from (
SELECT
[business_stream_code],
[business_stream_desc],
[region_service_code],
[region_service_desc],
[patch_code],
[patch_desc],
sch_code,
sch_name,
AM_Region,
[Component Number],
[Component Description],
[Component group],
Quantity,
[Last Replace Date],
DURATION,
[Scheme Cycle Time],
[Scheme unit Replacement Cost],
[Scheme Replacement Cost],
convert(int,next_date) as next_date,
expirydate,
surveyor,
comment
FROM (
(
SELECT DISTINCT hi.[business_stream_code],
hi.[business_stream_desc],
hi.[region_service_code],
hi.[region_service_desc],
hi.[patch_code],
hi.[patch_desc],
hi.[business_unit_code] AS sch_code,
hi.[business_unit_desc] AS sch_name,
RAW_CMPGENCD_2.desn AS AM_Region,
hgmprcom.cmpnt_ref AS [Component Number],
hgmprcom.CMPNT_DESN [Component Description],
hgmprcom.TYPE_COMP_1STOU AS [Component group],
hgmprcom.quantity AS Quantity,
hgmpccyc.last_date AS [Last Replace Date],
hgmpccyc.DURATION,
(hgmpccyc.DURATION) / 12 AS [Scheme Cycle Time],
COALESCE(HGMPCCYC.cost, 0) AS [Scheme unit Replacement Cost],
(hgmprcom.quantity * COALESCE(HGMPCCYC.cost, 0)) AS [Scheme Replacement Cost],
convert(VARCHAR(5), datepart(yyyy, hgmpccyc.next_date)) AS next_date,
(datepart(yyyy, hgmpccyc.next_date) + ((hgmpccyc.DURATION) / 12)) AS expirydate,
--(SELECT (hgmprcom.quantity * COALESCE(HGMPCCYC.cost, 0))
-- WHERE (datepart(yyyy, hgmpccyc.next_date) = datepart(yyyy, getdate())
-- OR (datepart(yyyy, hgmpccyc.next_date)
-- <= (datepart(yyyy, hgmpccyc.next_date) + (hgmpccyc.DURATION) / 12))
-- )
--) AS cost,
HPMCONRC.cntr_lg_name AS surveyor,
'' AS comment
FROM ql_test.[HC21].[H21_org_hierarchy] hi WITH (NOLOCK)
LEFT JOIN dataset_voids_finance dv WITH (NOLOCK)
ON dv.Court_Code = hi.[business_unit_code]
LEFT JOIN QL_test.dbo.hgmprty1 hgmprty1 WITH (NOLOCK)
ON hgmprty1.sch_id = hi.[business_unit_code]
LEFT JOIN QL_test.dbo.hgmprcom hgmprcom WITH (NOLOCK)
ON hgmprcom.prty_id = hgmprty1.prty_id
AND hgmprcom.comp_id = hgmprty1.comp_id
LEFT JOIN QL_test.dbo.hgmpccyc hgmpccyc WITH (NOLOCK)
ON hgmpccyc.comp_id = hgmprcom.comp_id
AND hgmpccyc.prty_id = hgmprcom.prty_id
AND hgmprcom.cmpnt_ref = hgmpccyc.cmpnt_ref
AND hgmpccyc.location = hgmprcom.location
LEFT JOIN QL_test.dbo.HPMCONRC AS HPMCONRC WITH (NOLOCK)
ON hgmprcom.def_CNTR_REF = HPMCONRC.CNTR_REF
AND hgmprcom.COMP_ID = HPMCONRC.COMP_ID
LEFT JOIN QL_Test.dbo.cmpgencd AS RAW_CMPGENCD_2 WITH (NOLOCK)
ON hgmprty1.oth_rep_cd2 = RAW_CMPGENCD_2.code_id
AND RAW_CMPGENCD_2.mod_id = 'HSG'
AND RAW_CMPGENCD_2.category_id = 'oth_rep2'
WHERE datepart(yyyy, hgmpccyc.next_date) <= '2060'
AND (dv.Void_Period_Code NOT IN ('VDS001', 'VDS002', 'XBLOCK'))
AND (
hi.Business_Stream_Desc IN (
SELECT val
FROM fn_split(@BUSINESS_STREAM)
)
OR @BUSINESS_STREAM = 'ALL'
)
AND (
hi.patch_desc IN (
SELECT val
FROM fn_split(@Patch)
)
OR @PATCH = 'ALL'
)
AND (
hi.[Region_Service_Desc] IN (
SELECT val
FROM fn_split(@REGION)
)
OR @REGION = 'ALL'
)
AND (
RAW_CMPGENCD_2.desn IN (
SELECT val
FROM fn_split(@AM_REGION)
)
OR @AM_REGION = 'ALL'
)
AND (
hi.business_unit_desc IN (
SELECT val
FROM fn_split(@scheme)
)
OR @scheme = 'ALL'
)
))
x )a
select * from #temp