右连接多个键,其中多个键为空

Right join multiple key where mutiple key is null

我想我需要船长的帮助。我正在尝试将数据从 table 插入临时 table。好的,这很简单

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

对我来说很难的是只有当今天的数据没有出现在 10 天前的数据中时才插入今天的数据

我使用的 table 的一个例子 ([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

我想要的tabletemptable:

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)

提前致谢

你可以用 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

returns:

+------------+----------+---------------+
| 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 |
+------------+----------+---------------+

可选地,如果您同时执行这两项操作,则可以使用派生的 table/subquery 或 common table expression with row_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

returns:

+------------+----------+---------------+
| 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 |
+------------+----------+---------------+