Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 拥有“2018年10月13日第1季度季度尽职调查”。如果您想了解您在这里所说的内容,我们必须将日期定为2018/10/13,在表1中找到上一个日期,即2018/10/10,然后再次返回“1 b c D E 2018/10/10”。如果这是您想要的,请相应地_Sql_Sql Server_Tsql_Common Table Expression - Fatal编程技术网

Sql 拥有“2018年10月13日第1季度季度尽职调查”。如果您想了解您在这里所说的内容,我们必须将日期定为2018/10/13,在表1中找到上一个日期,即2018/10/10,然后再次返回“1 b c D E 2018/10/10”。如果这是您想要的,请相应地

Sql 拥有“2018年10月13日第1季度季度尽职调查”。如果您想了解您在这里所说的内容,我们必须将日期定为2018/10/13,在表1中找到上一个日期,即2018/10/10,然后再次返回“1 b c D E 2018/10/10”。如果这是您想要的,请相应地,sql,sql-server,tsql,common-table-expression,Sql,Sql Server,Tsql,Common Table Expression,拥有“2018年10月13日第1季度季度尽职调查”。如果您想了解您在这里所说的内容,我们必须将日期定为2018/10/13,在表1中找到上一个日期,即2018/10/10,然后再次返回“1 b c D E 2018/10/10”。如果这是您想要的,请相应地编辑您的问题。@KMittal,您的逻辑不清楚,请您详细解释一下好吗谢谢,这就是我要找的。谢谢,这就是我要找的。如果表1中的D和E也可以有空值,我想这个解决方案行不通,否则就太好了,谢谢分享。。这是一次很好的学习。@KMittal您能确切地解释


拥有“2018年10月13日第1季度季度尽职调查”。如果您想了解您在这里所说的内容,我们必须将日期定为2018/10/13,在表1中找到上一个日期,即2018/10/10,然后再次返回“1 b c D E 2018/10/10”。如果这是您想要的,请相应地编辑您的问题。@KMittal,您的逻辑不清楚,请您详细解释一下好吗谢谢,这就是我要找的。谢谢,这就是我要找的。如果表1中的D和E也可以有空值,我想这个解决方案行不通,否则就太好了,谢谢分享。。这是一次很好的学习。@KMittal您能确切地解释一下在什么情况下不起作用吗?如果我们将第一行的表1“D”替换为null,将第二行的表“B”替换为null。然后它为所有行提供“E”。虽然一行中应该有空值,但我认为这个解决方案是行不通的,如果表1中D和E也可以有空值,否则很好,谢谢大家分享。。这是一次很好的学习。@KMittal您能确切地解释一下在什么情况下不起作用吗?如果我们将第一行的表1“D”替换为null,将第二行的表“B”替换为null。然后它为所有行提供“E”。虽然一行中应该有null。
ID  B   C   D   E   Date

1   b   c   D   E   2018/10/10
1   c   d   A   B   2018/10/14
ID  B   C   Date
1   b   c   2018/10/10
1   x   y   2018/10/11
1   y   x   2018/10/12
1   p   q   2018/10/13
1   c   d   2018/10/14
Select * from Table2 t2 left join table1 t1
on t2.id=t1.id and t2.Date = t1.Date
ID  B   C   D   E   Date1           ID  B   C   Date2

1   b   c   D   E   2018/10/10      1   b   c   2018/10/10
-   -   -   -   -   -               1   x   y   2018/10/11
-   -   -   -   -   -               1   y   x   2018/10/12
    -   -   -   -   -               1   p   q   2018/10/13
1   c   d   A   B   2018/10/14      1   c   d   2018/10/14
ID  B   C   D   E   Date

1   b   c   D   E   2018/10/10
1   x   y   D   E   2018/10/11
1   y   x   D   E   2018/10/12
1   p   q   D   E   2018/10/13
1   c   d   A   B   2018/10/14
select 
    t2.ID,
    isnull(t1.B,t2.B) as B,
    isnull(t1.C,t2.C) as C,
    isnull(t1.D,t2.D) as D,
    isnull(t1.E,t2.E) as E,
    isnull(t1.[Date],t2.[Date]) as [Date]
from Table2 t2 
left join table1 t1
on t2.id=t1.id and t2.Date = t1.Date
with
    cte
as
(
    select
        t2.ID   ,
        t2.B    ,
        t2.C    ,
        t1.D    ,
        t1.E    ,
        t2.[Date]   ,
        sum(case when t1.D is null then 0 else 1 end) over (order by t2.[Date]) as D_partition,
        sum(case when t1.E is null then 0 else 1 end) over (order by t2.[Date]) as E_partition
    from
        Table2 t2
    left join
        table1 t1
    on
        t2.id = t1.id
        and
            t2.[Date] = t1.[Date]
)
select
    cte.ID  ,
    cte.B   ,
    cte.C   ,
    first_value(D) over(partition by D_partition order by D desc)   as D    ,
    first_value(E) over(partition by E_partition order by E desc)   as E    ,
    cte.Date
from
    cte;
-- create data from example:
-- ------------------------
select * into #Table1 from
(select 1 as ID, 'b' as B, 'c' as C, 'D' as D, 'E' as E, cast('2018-10-10' as date) as [Date]
union all select 1,   'c',   'd',   'A',   'B',   '2018-10-14')t

select * into #Table2 from
(select 1 as ID,   'b' as B,   'c' as C ,   cast('2018-10-10' as date) as [Date]
union all select 1,   'x',   'y',   '2018-10-11'
union all select 1,   'y',   'x',  '2018-10-12'
union all select 1,   'p',   'q',   '2018-10-13'
union all select 1,   'c',   'd',   '2018-10-14')t

-- SOLUTION
-- -------- 
select
    T2.ID,
    T2.B,
    T2.C,
    T1.D,
    T1.E,
    T2.[Date]
from
    #Table2 T2
    outer apply
    (
        select top 1 * from #Table1 T1
        where T1.ID=T2.ID and T1.[Date] <= T2.[Date]
        order by T1.[Date] desc
    ) T1


-- clean everything
-- ----------------
drop table #Table1
drop table #Table2
Select t2.ID,t2.B,t2.C,t1.D,t1.E, t2.Date from Table2 t2 left join table1 t1
on t2.id=t1.id and (t2.Date >= t1.Date)
where not exists (select 1 from table1 t12 where t2.Date > t1.Date and t2.Date >= t12.Date and t12.Date > t1.Date)