【问题标题】:Right join multiple key where mutiple key is null右连接多个键,其中多个键为空
【发布时间】:2017-09-26 12:49:26
【问题描述】:

我想我显然需要船长的帮助。我正在尝试将表中的数据插入到临时表中。好的,这很容易

我需要插入今天得到的数据和 10 天前得到的数据。 where子句可以承受,没关系

对我来说很难的是插入今天的数据,前提是它没有出现在 10 天前的数据中

我使用的表格示例([datatable]):

Date    Purchase    Line_Purchase
---------------------------------------------------------------------------
2017-04-29    0000002    01
2017-04-29    0000002    02
2017-04-29    0000003    01
2017-04-29    0000003    02
2017-04-29    0000003    03
2017-04-29    0000004    01
2017-04-29    0000005    01
2017-04-19    0000001    01
2017-04-19    0000001    02
2017-04-19    0000001    03
2017-04-19    0000002    01
2017-04-19    0000002    02

我想要的桌子temptable

Input_date    Purchase    Line_Purchase
-------------------------------------------------------------------------
2017-04-19    0000001    01
2017-04-19    0000001    02
2017-04-19    0000001    03
2017-04-19    0000002    01
2017-04-19    0000002    02
2017-04-29    0000003    01
2017-04-29    0000003    02
2017-04-29    0000003    03
2017-04-29    0000004    01
2017-04-29    0000005    01

SQL 中是否有任何请求可以改变这种情况?

我试过这种方式

INSERT INTO #TEMPTABLE
    (Input_date ,Purchase ,Line_Purchase)
SELECT 
    table.Date
    ,table.Purchase
    ,table.Line_Purchase
FROM
    datatable table
WHERE
    convert(date, table.Date) = convert(date, GETDATE() - 10)


INSERT INTO #TEMPTABLE
    (Input_date ,Purchase ,Line_Purchase)
SELECT 
    table.Date
    ,table.Purchase
    ,table.Line_Purchase
FROM
    datatable table
    RIGHT JOIN #TEMPTABLE temp
        on table.Purchase = temp.Purchase and table.Line_Purchase = temp.Line_Purchase
WHERE
    convert(date, table.Date) = convert(date, GETDATE())
    AND (temp.Purchase is null AND temp.Line_Purchase is null)

提前致谢

【问题讨论】:

    标签: sql sql-server right-join


    【解决方案1】:

    您可以使用not exists()

    select date as Input_date, Purchase, Line_Purchase
    into #temptable
    from t
    where date = '2017-04-19' --convert(date, getdate() - 10);
    
    insert into #temptable (Input_date, Purchase, Line_Purchase)
    select *
    from t
    where date = '2017-04-29'
      and not exists (
        select 1
        from t as i
        where i.purchase=t.purchase
          and i.line_purchase=t.line_purchase
          and i.date = '2017-04-19' --convert(date, getdate() - 10)
        );
    
    select * 
    from #temptable;
    

    rextester 演示:http://rextester.com/SAQSG21367

    返回:

    +------------+----------+---------------+
    | Input_Date | Purchase | Line_Purchase |
    +------------+----------+---------------+
    | 2017-04-19 |  0000001 |            01 |
    | 2017-04-19 |  0000001 |            02 |
    | 2017-04-19 |  0000001 |            03 |
    | 2017-04-19 |  0000002 |            01 |
    | 2017-04-19 |  0000002 |            02 |
    | 2017-04-29 |  0000003 |            01 |
    | 2017-04-29 |  0000003 |            02 |
    | 2017-04-29 |  0000003 |            03 |
    | 2017-04-29 |  0000004 |            01 |
    | 2017-04-29 |  0000005 |            01 |
    +------------+----------+---------------+
    

    或者,如果您同时执行这两项操作,您可以在同一个查询中使用派生表/子查询或common table expressionrow_number() ;

    ;with cte as (
    select date, Purchase, Line_Purchase
      , rn = row_number() over (partition by Purchase,Line_Purchase order by date)
     from t
    --where date in ('2017-09-26','2017-09-16') 
    where date in (convert(date, getdate()), convert(date, getdate()-10))
    )
    select date as Input_date, Purchase, Line_Purchase
    into #temptable
    from cte
    where rn = 1
    
    select *
    from #temptable;
    

    rextester 演示:http://rextester.com/QMF5992

    返回:

    +------------+----------+---------------+
    | Input_date | Purchase | Line_Purchase |
    +------------+----------+---------------+
    | 2017-09-16 |  0000001 |            01 |
    | 2017-09-16 |  0000001 |            02 |
    | 2017-09-16 |  0000001 |            03 |
    | 2017-09-16 |  0000002 |            01 |
    | 2017-09-16 |  0000002 |            02 |
    | 2017-09-26 |  0000003 |            01 |
    | 2017-09-26 |  0000003 |            02 |
    | 2017-09-26 |  0000003 |            03 |
    | 2017-09-26 |  0000004 |            01 |
    | 2017-09-26 |  0000005 |            01 |
    +------------+----------+---------------+
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-28
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多