Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 右键连接多个键,其中多个键为空_Sql_Sql Server_Right Join - Fatal编程技术网

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

Sql 右键连接多个键,其中多个键为空,sql,sql-server,right-join,Sql,Sql Server,Right Join,我想我需要船长的帮助。我正在尝试将表中的数据插入到一个诱人的表中。好的,这很简单 我需要插入我们今天得到的数据和我们10天前得到的数据。where条款可能会影响它,没关系 对我来说,很难插入今天的数据,除非它在10天前没有出现在数据中 我使用的表的一个示例([datatable]): 我想要的表格诱人: Input_date Purchase Line_Purchase ---------------------------------------------------------

我想我需要船长的帮助。我正在尝试将表中的数据插入到一个诱人的表中。好的,这很简单

我需要插入我们今天得到的数据和我们10天前得到的数据。where条款可能会影响它,没关系

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

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

我想要的表格
诱人

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()执行此操作。

rextester演示:

返回:

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

(可选)如果同时执行这两个操作,则可以在同一查询中使用派生表/子查询或 ;

rextester演示:

返回:

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

从t中选择1,因为我不明白为什么选择1而不选择*我习惯使用
select 1
exists()
notexists()
都不返回行,因此可以使用
selectnull
select1
select*
,甚至
select1/0
。基于本文,使用
select1
将避免在查询编译期间检查该表的任何不需要的元数据。运行的测试显示实际性能没有差异。感谢您的支持explanation@ValentinC很乐意帮忙!
+------------+----------+---------------+
| 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 |
+------------+----------+---------------+
;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;
+------------+----------+---------------+
| 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 |
+------------+----------+---------------+