我正在修改下面的代码,我没有编写并交给我(相对较新的SQL),实际上它选择地址但是我只想选择具有最新“UDEFVALID_FROM”日期的地址,目前,如果客户有2个地址,则会同时选择这两个地址。
Select * FROM UDEFCust_Temp_Address ctd
WHERE ctd.UDEFVALID_FROM IS NOT NULL
AND (GETDATE() >= ctd.UDEFVALID_FROM
AND GETDATE() <= ctd.UDEFVALID_TO
OR (ctd.UDEFVALID_TO IS NULL or ctd.UDEFVALID_TO>= GETDATE() ))
在这里进一步添加完整的SQL:
IF @customer_rule = 'All'
BEGIN
insert into [etl].[lu_uletters_recipient_addrss_stg]
(cnsmr_id,cnsmr_accnt_id,cnsmr_type,addrss_rule,recipientSalutation,recipientName,
recipient_addressLine1,recipient_addressLine2,recipient_addressLine3,recipient_addressLine4,
recipient_address_pstl_cd,recipient_primaryAddresseType,moreRecipientIndicator,PostalClass,dcmnt_tmplt_shrt_nm)
select *
from
(
select t.cnsmr_id as cnsmr_id,t.cnsmr_accnt_id as cnsmr_accnt_id,
'Primary' as cnsmr_type,
'All' as addrss_rule,
ctad.UDEFSALUTATION AS recipientSalutation,
isnull(ctd.UDEFNAME,ctad.UDEFADDRESSEE) as recipientName,
COALESCE(ctd.UDEFADDRESS_addrss_ln_1_txt,ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt) as recipient_addressLine1,
CASE WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NOT NULL THEN COALESCE(ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt)
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NOT NULL THEN ctd.UDEFADDRESS_addrss_ln_3_txt
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NULL THEN NULL
END as recipient_addressLine2,
ctd.UDEFADDRESS_city_txt as recipient_addressLine3,
ctd.UDEFADDRESS_st_txt as recipient_addressLine4,
ctd.UDEFADDRESS_pstl_cd as recipient_address_pstl_cd,
'P' as recipient_primaryAddresseType,
'N' as moreRecipientIndicator,
case when ctad.UDEFFOREIGN_ADDR_IND='Y' then 'O'
else t.PostalClass end as PostalClass,
dcmnt_tmplt_shrt_nm
from #tt_cnsmr t
--inner join cnsmr_addrss cd on ( cd.cnsmr_id = t.cnsmr_id)
--inner join cnsmr_accnt_ownrs CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
inner join cnsmr_accnt CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
left outer join UDEFCust_Temp_Address ctd on ( t.cnsmr_id = ctd.cnsmr_id)
left outer join UDEFADDITIONAL_CUST_DETAILS ctad on ( t.cnsmr_id = ctad.cnsmr_id)
where t.dcmnt_tmplt_shrt_nm = @v_tmpl_code/*cao.cnsmr_accnt_ownrshp_typ_cd = 1
and cao.cnsmr_accnt_ownrshp_sft_dlt_flg = 'N'*/
and ((ctd.UDEFVALID_FROM IS NOT NULL AND (GETDATE() >= ctd.UDEFVALID_FROM AND GETDATE() <= ctd.UDEFVALID_TO))
OR (ctd.UDEFVALID_TO IS NULL AND (GETDATE() >= isnull(ctd.UDEFVALID_FROM,'1900-01-01 00:00:00'))))
union
select cao.cnsmr_id as cnsmr_id,cao.cnsmr_accnt_id as cnsmr_accnt_id,
'Non-Primary' as cnsmr_type,
'All' as addrss_rule,
ctad.UDEFSALUTATION AS recipientSalutation,
isnull(ctd.UDEFNAME,ctad.UDEFADDRESSEE) as recipientName,
COALESCE(ctd.UDEFADDRESS_addrss_ln_1_txt,ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt) as recipient_addressLine1,
CASE WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NOT NULL THEN COALESCE(ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt)
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NOT NULL THEN ctd.UDEFADDRESS_addrss_ln_3_txt
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NULL THEN NULL
END as recipient_addressLine2,
ctd.UDEFADDRESS_city_txt as recipient_addressLine3,
ctd.UDEFADDRESS_st_txt as recipient_addressLine4,
ctd.UDEFADDRESS_pstl_cd as recipient_address_pstl_cd,
'S' as recipient_primaryAddresseType,
'N' as moreRecipientIndicator,
case when ctad.UDEFFOREIGN_ADDR_IND='Y' then 'O'
else t.PostalClass end as PostalClass,
dcmnt_tmplt_shrt_nm
from #tt_cnsmr t
inner join cnsmr_accnt_ownrs CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
--inner join cnsmr_addrss cd on ( cd.cnsmr_id = cao.cnsmr_id)
left outer join UDEFCust_Temp_Address ctd on ( t.cnsmr_id = ctd.cnsmr_id)
left outer join UDEFADDITIONAL_CUST_DETAILS ctad on ( t.cnsmr_id = ctad.cnsmr_id)
where t.dcmnt_tmplt_shrt_nm = @v_tmpl_code
and cao.cnsmr_accnt_ownrshp_typ_cd = 2
and cao.cnsmr_accnt_ownrshp_sft_dlt_flg = 'N'
and ((ctd.UDEFVALID_FROM IS NOT NULL AND (GETDATE() >= ctd.UDEFVALID_FROM AND GETDATE() <= ctd.UDEFVALID_TO))
OR (ctd.UDEFVALID_TO IS NULL AND (GETDATE() >= isnull(ctd.UDEFVALID_FROM,'1900-01-01 00:00:00'))))
)stg_data
where not exists ( select 1 from [etl].[lu_uletters_recipient_addrss_stg] t
where t.cnsmr_id = stg_data.cnsmr_id and t.cnsmr_accnt_id = stg_data.cnsmr_accnt_id and t.dcmnt_tmplt_shrt_nm = @v_tmpl_code)
答案 0 :(得分:2)
在sqlServer2008 +
中尝试
; With cte as (
Select *, row_number() over (partition by CustomerId order by UDEFVALID_FROM desc) rn
FROM UDEFCust_Temp_Address ctd
WHERE ctd.UDEFVALID_FROM IS NOT NULL
AND (GETDATE() >= ctd.UDEFVALID_FROM
AND GETDATE() <= ctd.UDEFVALID_TO
OR (ctd.UDEFVALID_TO IS NULL or ctd.UDEFVALID_TO>= GETDATE() ))
)
Select * from cte where rn=1
或使用类似
的子查询select * from (
Select *, row_number() over (partition by CustomerId order by UDEFVALID_FROM desc) rn
FROM UDEFCust_Temp_Address
)
where rn=1
用子查询替换后的查询
declare @v_tmpl_code nvarchar(10);
insert into [etl].[lu_uletters_recipient_addrss_stg] (cnsmr_id,cnsmr_accnt_id,cnsmr_type,addrss_rule,recipientSalutation,recipientName
, recipient_addressLine1,recipient_addressLine2,recipient_addressLine3,recipient_addressLine4
, recipient_address_pstl_cd,recipient_primaryAddresseType,moreRecipientIndicator,PostalClass,dcmnt_tmplt_shrt_nm)
select * from ( select t.cnsmr_id as cnsmr_id,t.cnsmr_accnt_id as cnsmr_accnt_id, 'Primary' as cnsmr_type, 'All'
as addrss_rule, ctad.UDEFSALUTATION AS recipientSalutation, isnull(ctd.UDEFNAME,ctad.UDEFADDRESSEE) as recipientName
, COALESCE(ctd.UDEFADDRESS_addrss_ln_1_txt,ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt) as recipient_addressLine1
, CASE WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NOT NULL THEN COALESCE(ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt)
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NOT NULL
THEN ctd.UDEFADDRESS_addrss_ln_3_txt WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NULL
THEN NULL END as recipient_addressLine2, ctd.UDEFADDRESS_city_txt as recipient_addressLine3, ctd.UDEFADDRESS_st_txt
as recipient_addressLine4, ctd.UDEFADDRESS_pstl_cd as recipient_address_pstl_cd, 'P' as recipient_primaryAddresseType
, 'N' as moreRecipientIndicator, case when ctad.UDEFFOREIGN_ADDR_IND='Y' then 'O' else t.PostalClass
end as PostalClass, dcmnt_tmplt_shrt_nm
from #tt_cnsmr t
inner join cnsmr_addrss cd on ( cd.cnsmr_id = t.cnsmr_id)
inner join cnsmr_accnt_ownrs CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
inner join cnsmr_accnt CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
-- this is the replaced query
left outer join (Select *, row_number() over (partition by CustomerId order by UDEFVALID_FROM desc) rn FROM UDEFCust_Temp_Address) ctd on t.cnsmr_id = ctd.cnsmr_id and ctd.rn=1
left outer join UDEFADDITIONAL_CUST_DETAILS ctad on ( t.cnsmr_id = ctad.cnsmr_id)
where t.dcmnt_tmplt_shrt_nm = @v_tmpl_code and cao.cnsmr_accnt_ownrshp_typ_cd = 1
and cao.cnsmr_accnt_ownrshp_sft_dlt_flg = 'N'
and ((ctd.UDEFVALID_FROM IS NOT NULL AND (GETDATE() >= ctd.UDEFVALID_FROM
AND GETDATE() <= ctd.UDEFVALID_TO)) OR (ctd.UDEFVALID_TO IS NULL AND (GETDATE() >= isnull(ctd.UDEFVALID_FROM,'1900-01-01 00:00:00'))))
union
select cao.cnsmr_id as cnsmr_id,cao.cnsmr_accnt_id as cnsmr_accnt_id, 'Non-Primary' as cnsmr_type, 'All' as addrss_rule,
ctad.UDEFSALUTATION AS recipientSalutation, isnull(ctd.UDEFNAME,ctad.UDEFADDRESSEE) as recipientName
, COALESCE(ctd.UDEFADDRESS_addrss_ln_1_txt,ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt)
as recipient_addressLine1, CASE WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NOT NULL
THEN COALESCE(ctd.UDEFADDRESS_addrss_ln_2_txt,ctd.UDEFADDRESS_addrss_ln_3_txt)
WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NOT NULL
THEN ctd.UDEFADDRESS_addrss_ln_3_txt WHEN ctd.UDEFADDRESS_addrss_ln_1_txt IS NULL AND ctd.UDEFADDRESS_addrss_ln_2_txt IS NULL
THEN NULL END as recipient_addressLine2, ctd.UDEFADDRESS_city_txt as recipient_addressLine3, ctd.UDEFADDRESS_st_txt
as recipient_addressLine4, ctd.UDEFADDRESS_pstl_cd as recipient_address_pstl_cd, 'S' as recipient_primaryAddresseType, 'N'
as moreRecipientIndicator, case when ctad.UDEFFOREIGN_ADDR_IND='Y' then 'O' else t.PostalClass end as PostalClass, dcmnt_tmplt_shrt_nm
from #tt_cnsmr t inner join cnsmr_accnt_ownrs CAO on (t.cnsmr_id = cao.cnsmr_id and t.cnsmr_accnt_id = cao.cnsmr_accnt_id )
inner join cnsmr_addrss cd on ( cd.cnsmr_id = cao.cnsmr_id) left outer join UDEFCust_Temp_Address ctd on ( t.cnsmr_id = ctd.cnsmr_id)
left outer join UDEFADDITIONAL_CUST_DETAILS ctad on ( t.cnsmr_id = ctad.cnsmr_id) where t.dcmnt_tmplt_shrt_nm = @v_tmpl_code
and cao.cnsmr_accnt_ownrshp_typ_cd = 2 and cao.cnsmr_accnt_ownrshp_sft_dlt_flg = 'N' and ((ctd.UDEFVALID_FROM IS NOT NULL
AND (GETDATE() >= ctd.UDEFVALID_FROM AND GETDATE() <= ctd.UDEFVALID_TO)) OR (ctd.UDEFVALID_TO IS NULL
AND (GETDATE() >= isnull(ctd.UDEFVALID_FROM,'1900-01-01 00:00:00')))) )stg_data where
not exists ( select 1 from [etl].[lu_uletters_recipient_addrss_stg] t
where t.cnsmr_id = stg_data.cnsmr_id and t.cnsmr_accnt_id = stg_data.cnsmr_accnt_id and t.dcmnt_tmplt_shrt_nm = @v_tmpl_code)
答案 1 :(得分:-1)
你可以通过小组和小组实现这一目标。 order子句,如下所示(用正确的列名替换custId,根据你想要的组地址):
Select * FROM UDEFCust_Temp_Address ctd WHERE
ctd.UDEFVALID_FROM IS NOT NULL AND (GETDATE() >= ctd.UDEFVALID_FROM AND GETDATE() <= ctd.UDEFVALID_TO
OR (ctd.UDEFVALID_TO IS NULL or ctd.UDEFVALID_TO>= GETDATE() ))
group by custId order by UDEFVALID_TO desc