我想,我需要船长的帮助。我试图将表格中的数据插入到临时表中。好的,这很容易
我需要插入我们今天获得的数据以及我们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)
提前致谢
答案 0 :(得分: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 expression 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
返回:
+------------+----------+---------------+
| 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 |
+------------+----------+---------------+