SQL窗口函数,用于选择一条记录与另一条记录

SQL窗口函数,用于选择一条记录与另一条记录,sql,sql-server,tsql,Sql,Sql Server,Tsql,我正在编写一个查询,需要根据以下条件选择一个记录: 如果PrimaryLocation='Y',则选择该记录。如果PrimaryLocation='N'和locationstate=issuingstate,则选择具有匹配状态的第一条记录。如果PrimaryLocation='N'且issuingstate为NULL,则选择第一条非NULL记录。如果PrimaryLocation='N'且所有发出状态均为NULL,则选择任意记录 数据集如下所示: ID licensenumbe

我正在编写一个查询,需要根据以下条件选择一个记录: 如果PrimaryLocation='Y',则选择该记录。如果PrimaryLocation='N'和locationstate=issuingstate,则选择具有匹配状态的第一条记录。如果PrimaryLocation='N'且issuingstate为NULL,则选择第一条非NULL记录。如果PrimaryLocation='N'且所有发出状态均为NULL,则选择任意记录

数据集如下所示:

    ID      licensenumber     IssuingState       LocationName     PrimaryLocation  LocationState 
    10        555                FL                Coral             Y                   FL
    10        555                GA                Seaside           N                   FL
    20        777                FL                Jax               N                   FL
    20        777                FL                Broward           N                   FL
    30        999                FL                Broward           N                   GA
    30        999                FL                Davie             N                   FL
    40        888                NULL              Orange            N                   FL
    40        888                FL                Davie             N                   FL
    50        333                NULL              Dade              N                   FL
    50        333                NULL              Orange            N                   FL
对于ID=10,我只需要第一条记录,其中PrimaryLocation='Y'

对于ID=20,我想要任何一条记录

对于ID=30,我需要LocationName='Davie'的记录,因为LocationState=IssuingState

对于ID=40,我希望LocationName='Davie',因为发布状态不是NULL

对于ID=50,我可以选择任意一条记录

这个数据集是我使用#temp表的更大查询的一部分。我一直在对Row_Number()使用窗口函数,但在查询的LocationState=IssuingState部分遇到了特别的困难

到目前为止,我所能想到的就是:

    SELECT id, licensenumber, issuingstate, locationname, primarylocation, locationstate,
    DENSE_RANK()OVER(PARTITION BY id, licensenumber, issuingstate
                     ORDER BY primarylocation DESC)
    INTO #LICENSES
    FROM TABLE

   SELECT *, ROW_NUMBER()OVER(PARTITION BY id, licensenumber, licensetypename, issuingstate,
                              ORDER BY PRIMARYLOCATION DESC) RN
   FROM #LICENSES

您可以在
行编号
窗口功能中按的顺序排列
中提到的条件的优先级

select * from (
select t.*,row_number() over(partition by id order by 
                             case when PrimaryLocation ='Y' then 1 
                                  when PrimaryLocation ='N' and locationstate = issuingstate then 2
                                  when PrimaryLocation ='N' and issuingstate is NULL then 3
                                  else 4 end, locationName) as rnum
from tbl t
) t 
where rnum = 1                                           

您可以在
行编号
窗口功能中按
的顺序排列
中提到的条件的优先级

select * from (
select t.*,row_number() over(partition by id order by 
                             case when PrimaryLocation ='Y' then 1 
                                  when PrimaryLocation ='N' and locationstate = issuingstate then 2
                                  when PrimaryLocation ='N' and issuingstate is NULL then 3
                                  else 4 end, locationName) as rnum
from tbl t
) t 
where rnum = 1                                           

编写查询的另一种方法是:

select *
from(
select *, row_number() over (partition by id order by id,issuingstate desc) as rn
from #temp
where (case when PrimaryLocation ='Y' then 1 
                when PrimaryLocation ='N' and locationstate = issuingstate then 1
                when PrimaryLocation ='N' and issuingstate is NULL then 1
                else 0 end) = 1
) a
where rn = 1

编写查询的另一种方法是:

select *
from(
select *, row_number() over (partition by id order by id,issuingstate desc) as rn
from #temp
where (case when PrimaryLocation ='Y' then 1 
                when PrimaryLocation ='N' and locationstate = issuingstate then 1
                when PrimaryLocation ='N' and issuingstate is NULL then 1
                else 0 end) = 1
) a
where rn = 1

您正在编写查询,查询在哪里?您刚刚为where子句编写了psudo代码。在where子句中的CASE语句中执行此操作:@Eric--我添加了到目前为止的代码。您正在编写查询,where是您的查询?您刚刚为where子句编写了psudo代码。在where子句中的CASE语句中这样做:@Eric——我添加了到目前为止我提出的代码。这些是有趣的答案。我正在检查。这似乎有效。非常简单的解释。谢谢。这些都是有趣的答案。我正在检查。这似乎有效。非常简单的解释。非常感谢。