使用Case SQL Server进行分区

时间:2015-09-07 09:00:56

标签: sql-server row-number

下面的new_commsstream列计算上一行的date是否由persondid分区,并由子查询中包含date的其他几个列排序,是否大于90天,如果是,则返回1,否则返回0:

create view Motability_Dataset_Staging_cmp as 
  select 
    mdsc.PersonID,
    mdsc.AddressID,
    mdsc.Email,
    mdsc.Reportdate_month,
    mdsc.Channel,
    mdsc.CommsMedium,
    mdsc.Campaign_Name,
    mdsc.Category,
    mdsc.MRM_Campaign_code,
    mdsc.Action_id,
    mdsc.NumSents,
    mdsc.ReportDate,
    isnull(cmp.ppersonid,mdsc.PersonID) as Prev_PersonID,
    isnull(cmp.paddressid,mdsc.AddressID) as Prev_AddressID,
    isnull(cmp.pmrmcampaigncode,mdsc.MRM_Campaign_code) as Prev_MRMCampaignCode,
    isnull(cmp.pactionid,mdsc.Action_id) as Prev_ActionID,
    isnull(cmp.preportdate,mdsc.ReportDate) as Prev_ReportDate,
    isnull(cmp.commsdaysinterval,0) as Prev_CommsDays,
    isnull(cmp.newcommsstream,0) as New_CommsStream

  from Motability_Dataset_Staging as mdsc
    left join
      (select 
        cmp.row +1 as row,pcmp.row as prow,
        cmp.personid as personid,pcmp.personid as ppersonid,
        cmp.addressid as addressid,pcmp.addressid as paddressid,
        cmp.MRM_Campaign_code as mrmcampaigncode,pcmp.MRM_Campaign_code as pmrmcampaigncode,
        cmp.Action_id as actionid,pcmp.Action_id as pactionid,
        cmp.reportdate as reportdate,pcmp.reportdate as preportdate,
        datediff(day,cmp.ReportDate,pcmp.ReportDate) as commsdaysinterval,
        case when datediff(day,cmp.ReportDate,pcmp.ReportDate) <-90 then 1 else 0 end as newcommsstream
      from
        (select row_number() over(partition by personid order by personid,addressid,reportdate,mrm_campaign_code,action_id)-1 as row,personid,addressid,MRM_Campaign_code,action_id,reportdate from Motability_Dataset_Staging) cmp
        inner join (select row_number() over(partition by personid order by personid,addressid,reportdate,mrm_campaign_code,action_id) as row,personid,addressid,MRM_Campaign_code,action_id,reportdate from Motability_Dataset_Staging) pcmp on cmp.row = pcmp.row and cmp.personid=pcmp.personid
        ) cmp
    on mdsc.PersonID = cmp.personid and mdsc.AddressID = cmp.addressid and mdsc.MRM_Campaign_code=cmp.mrmcampaigncode

我正在努力按人名和new_commsstream分区,所以每次在同一personid内有1时,它会添加一个新的行号,否则返回1:

personid    new_commsstream row
1       0       1
1       0       1
1       0       1
1       1       2
1       0       2
2       0       1
3       0       1
4       0       1
5       0       1
5       1       2
5       1       3

任何想法如何实现这一目标?

感谢。

1 个答案:

答案 0 :(得分:0)

我不确定它是否有帮助,但您不需要两次为ROW_NUMBER()选择数据。 您可以按照以下方式将其放入SQL Server CTE exression 然后你可以参考它两次

;with cmp as (
    select 
        row_number() over(partition by personid order by addressid,reportdate,mrm_campaign_code,action_id) as row,
        personid,
        addressid,
        MRM_Campaign_code,
        action_id,
        reportdate 
    from Motability_Dataset_Staging
), cmp2 as (
    select
        cmp2.*, -- previous values
        cmp.*   -- 
    from cmp
    left join cmp as cmp2 -- previous
        cmp.row = cmp2.row + 1 
)
select 
mdsc.PersonID,
mdsc.AddressID,
mdsc.Email,
mdsc.Reportdate_month,
mdsc.Channel,
mdsc.CommsMedium,
mdsc.Campaign_Name,
mdsc.Category,
mdsc.MRM_Campaign_code,
mdsc.Action_id,
mdsc.NumSents,
mdsc.ReportDate,
isnull(cmp.ppersonid,mdsc.PersonID) as Prev_PersonID,
isnull(cmp.paddressid,mdsc.AddressID) as Prev_AddressID,
isnull(cmp.pmrmcampaigncode,mdsc.MRM_Campaign_code) as Prev_MRMCampaignCode,
isnull(cmp.pactionid,mdsc.Action_id) as Prev_ActionID,
isnull(cmp.preportdate,mdsc.ReportDate) as Prev_ReportDate,
isnull(cmp.commsdaysinterval,0) as Prev_CommsDays,
isnull(cmp.newcommsstream,0) as New_CommsStream
from Motability_Dataset_Staging as mdsc
inner join cmp on ......