Sql server 将一行与前12个值进行比较

Sql server 将一行与前12个值进行比较,sql-server,sas,Sql Server,Sas,我试图将一个值与前12个月/12行进行比较。如果在过去12个月内该值为“是”,则第12行为“是”。同样,对于每一行(比较前12个值) 我是按照下面的方法做的。它可以工作,但我必须对多个列执行,所以代码将非常冗长 对于n个列,有什么方法可以在简单的步骤中完成吗 select *, --Identify customers who have applied for principal relief within the past 12 months lag(PrincipalReliefFlag,

我试图将一个值与前12个月/12行进行比较。如果在过去12个月内该值为“是”,则第12行为“是”。同样,对于每一行(比较前12个值)

我是按照下面的方法做的。它可以工作,但我必须对多个列执行,所以代码将非常冗长

对于n个列,有什么方法可以在简单的步骤中完成吗

select *,
--Identify customers who have applied for principal relief within the past 12 months
 lag(PrincipalReliefFlag,1)  over(partition by id,productcode order by snapshot_date) PRflg1
,lag(PrincipalReliefFlag,2)  over(partition by id,productcode order by snapshot_date) PRflg2
,lag(PrincipalReliefFlag,3)  over(partition by id,productcode order by snapshot_date) PRflg3
,lag(PrincipalReliefFlag,4)  over(partition by id,productcode order by snapshot_date) PRflg4
,lag(PrincipalReliefFlag,5)  over(partition by id,productcode order by snapshot_date) PRflg5
,lag(PrincipalReliefFlag,6)  over(partition by id,productcode order by snapshot_date) PRflg6
,lag(PrincipalReliefFlag,7)  over(partition by id,productcode order by snapshot_date) PRflg7
,lag(PrincipalReliefFlag,8)  over(partition by id,productcode order by snapshot_date) PRflg8
,lag(PrincipalReliefFlag,9)  over(partition by id,productcode order by snapshot_date) PRflg9
,lag(PrincipalReliefFlag,10) over(partition by id,productcode order by snapshot_date) PRflg10
,lag(PrincipalReliefFlag,11) over(partition by id,productcode order by snapshot_date) PRflg11
into #test
from #temp

select *
,case when (PrincipalReliefFlag='Y' or PRflg1='Y' or PRflg2='Y' or PRflg3='Y' or PRflg4='Y' or PRflg5='Y' or PRflg6='Y' or PRflg7='Y' or PRflg8='Y' or PRflg9='Y' or PRflg10='Y' or  PRflg11='Y')
    then  'Y' 
    when (PrincipalReliefFlag='N' 
                                and (PRflg1='N' or PRflg1 is null)  
                                and (PRflg2='N' or PRflg2 is null) 
                                and (PRflg3='N' or PRflg3 is null) 
                                and (PRflg4='N'  or PRflg4 is null)
                                and (PRflg5='N' or PRflg5 is null)
                                and (PRflg6='N' or  PRflg6 is null)
                                and (PRflg7='N' or PRflg7 is null)
                                and (PRflg8='N' or  PRflg8  is null)
                                and (PRflg9='N' or PRflg9 is null)
                                and (PRflg10='N' or PRflg10 is null)
                                and (PRflg11='N' or PRflg11 is null) )
    then 'N'
    end PrincipalRelief_applied_in_last12m
from #test
期待简单的步骤。也尝试使用联接,但未能成功。

这就是您想要的吗

select t.*,
       max(PrincipalReliefFlag) over (partition by id, productcode
                                      order by snapshot_date
                                      rows between 11 preceding and current row
                                     ) as PrincipalReliefFlag_12
from #temp t;
这将取12个月期间的最大值——如果该标志仅取
'Y'
'N'
的值,则会起作用。这是您想要的吗

select t.*,
       max(PrincipalReliefFlag) over (partition by id, productcode
                                      order by snapshot_date
                                      rows between 11 preceding and current row
                                     ) as PrincipalReliefFlag_12
from #temp t;

这是12个月期间的最大值——如果该标志仅采用
'Y'
'N'
的值,那么它将起作用。为什么MySQL被标记为无效的MySQL代码,这与SQL Server(MSSQL)/Sybase有关。。由于
#
在这些RDM中是有效的语法。示例数据和所需结果将非常有用。我想知道
快照_日期在“`id,productcode'”组中是否总是唯一的,或者是否可以绑定。。比如@GordonLinoff已经说过一个例子会非常有用。。查看为什么MySQL被标记为无效的MySQL代码,这与SQL Server(MSSQL)/Sybase有关。。由于
#
在这些RDM中是有效的语法。示例数据和所需结果将非常有用。我想知道
快照_日期在“`id,productcode'”组中是否总是唯一的,或者是否可以绑定。。比如@GordonLinoff已经说过一个例子会非常有用。。请参阅快速问题,最大函数如何处理字符值?Ramu在这个问题上写得很好。“快速问题,最大函数如何处理字符值?”@Ramu从软件角度看,字符集基本上就是(预定义的)十六进制/二进制集?内部
MAX()/MIN()
在字符上使用时应使用到十六进制或到二进制的转换谢谢Raymond快速提问,MAX函数如何处理字符值?Ramu在这个问题上写得很好。“快速提问,MAX函数如何处理字符值?”@Ramu你知道从软件的角度来看,字符集基本上就是(预先)定义的十六进制/二进制集吗?在字符上使用时,内部
MAX()/MIN()
应使用到十六进制或到二进制的转换谢谢Raymond