按日期在SQL Server中获取首次出现

时间:2019-03-14 03:09:36

标签: sql sql-server

我在SQL Server中有如下表:

DECLARE @ProposalTemp AS TABLE (Flag1 BIT, Flag2 BIT, Eff_From DATE)

INSERT INTO @ProposalTemp VALUES
(0, 0,  '2019-03-01'), 
(1, 0,  '2019-03-01') ,
(0, 0, '2019-03-03') ,
(1, 1,  '2019-03-05') ,
(1, 0, '2019-03-07') ,
(1, 1, '2019-03-09'); 

示例:从表05/03/2019起,表1中的Flag1为“ 1”。从2019年3月9日起,Flag2为“ 1”。

我想在更改为active(1)且不对两列(flag1,flag2)都更改回'0'时获取日期

所需的输出是

Flag1_Efff_Date    Flag2_Efff_Date
   '2019-03-05'         '2019-03-09'  

这是另一个示例

INSERT INTO @ProposalTemp VALUES 
(0, 0, '2019-03-01'), 
(1, 0, '2019-03-01') , 
(0, 0, '2019-03-03') , 
(1, 1, '2019-03-05') , 
(1, 1, '2019-03-07') , 
(1, 1, '2019-03-09'); 

期望的输出将是

   Flag1_Efff_Date    Flag2_Efff_Date
   '2019-03-05'         '2019-03-05' 

我已经尝试了一点。.仍然不知道如何编写此逻辑。

SELECT 
    Flag1, Flag2, Eff_From
FROM   
    (SELECT  
         Flag1, Flag2, Eff_From,
         ROW_NUMBER() OVER (PARTITION BY Flag1, Flag2 ORDER BY Eff_From) rn
     FROM  
         @ProposalTemp) t
WHERE  
    rn = 1

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您可以尝试这个。

    GO
    ;with cte as (
    select row_number() over (order by eff_from) as Slno, * from @ProposalTemp )
    , ctflg1 as (
    select * from cte where slno = (select max(slno)+1 from cte where Flag1=0))
    , ctflg2 as (
    select * from cte where slno = (select max(slno)+1 from cte where Flag2=0))
    select Flag1, Flag2, Eff_From from ctflg1
    union
    select Flag1, Flag2, Eff_From from ctflg2
    GO

首先使用cte查找flag1和flag2的各个值,然后根据需要使用它。

答案 1 :(得分:0)

一个疯狂的猜测,因为您没有提供所需的逻辑。

根据描述和预期结果,您似乎希望第二个实例的标志为1

SELECT Flag1Date    = max(case when Flag1 = 1 and r1 = 2 then Eff_From end),
       Flag2Date    = max(case when Flag2 = 1 and r2 = 2 then Eff_From end)
FROM   
(      SELECT   Flag1,Flag2,Eff_From,
                Row_number() OVER(Partition BY Flag1 ORDER BY Eff_From ) r1,
                Row_number() OVER(Partition BY Flag2 ORDER BY Eff_From ) r2
       FROM     @ProposalTemp
) t

编辑:根据您更新的问题

; with cte as
(
    SELECT  Flag1,Flag2,Eff_From,
            Row_number() OVER(ORDER BY Eff_From ) rn
    FROM    @ProposalTemp
),
f1 as
(
    select  min(Eff_From) as Flag1Date
    from    cte f1
    where   f1.Flag1    = 1
    and not exists (select * from cte x where x.Flag1 <> 1 and x.rn > f1.rn)
),
f2 as
(
    select  min(Eff_From) as Flag2Date
    from    cte f2
    where   f2.Flag2    = 1
    and not exists (select * from cte x where x.Flag2 <> 1 and x.rn > f2.rn)
)
select  *
from    f1 cross join f2

答案 2 :(得分:0)

以下使用LAGLEAD函数来获得所需的结果。

DECLARE @ProposalTemp AS TABLE (Flag1 BIT, Flag2 BIT, Eff_From DATE)

INSERT INTO @ProposalTemp VALUES
(0, 0,  '2019-03-01'), 
(1, 0,  '2019-03-01') ,
(0, 0, '2019-03-03') ,
(1, 1,  '2019-03-05') ,
(1, 0, '2019-03-07') ,
(1, 1, '2019-03-09'); 

; WITH CTE_flag1flow as
(
select lag(flag1,1,null) over(order by Eff_From desc) as nex, flag1 as curr, lead(flag1,1,null) over(order by Eff_From desc) as prev, Eff_From
FROM @ProposalTemp),
CTE_flag2flow as
(
select lag(flag2,1,null) over(order by Eff_From desc) as nex, flag2 as curr, lead(flag2,1,null) over(order by Eff_From desc) as prev, Eff_From
FROM @ProposalTemp)
select * 
from 
(SELECT max(Eff_from) AS Eff_Flag1
FROM CTE_flag1Flow
WHERE curr = 1 AND prev = 0 ) AS c1
CROSS JOIN
(SELECT max(Eff_from) AS Eff_Flag2
FROM CTE_flag2Flow
WHERE curr = 1 AND prev = 0 ) AS C2