我有这个查询来获取表中的双打,但日期不同:
SELECT acf.ID , acf.ID2 , acf.CNK , acf.Omsch , acf.Groep , acf.prijs
FROM dbo.Acf_LstPrijzen acf
WHERE HstDate = '2013-12-01'
INTERSECT
SELECT acf.ID , acf.ID2 , acf.CNK , acf.Omsch , acf.Groep , acf.Prijs
FROM dbo.Acf_LstPrijzen acf
WHERE HstDate = (SELECT MAX(acf2.hstDate)
FROM dbo.Acf_LstPrijzen acf2
WHERE acf2.HstDate < '2013-12-01'
AND acf.id = acf2.id
GROUP BY id)
我的问题是:如何将此结果插入另一个表中?可能吗?
试试这个...
insert into #T
select * from
(
SELECT acf.ID , acf.ID2 , acf.CNK , acf.Omsch , acf.Groep , acf.prijs
FROM dbo.Acf_LstPrijzen acf
WHERE HstDate = '2013-12-01'
INTERSECT
SELECT acf.ID , acf.ID2 , acf.CNK , acf.Omsch , acf.Groep , acf.Prijs
FROM dbo.Acf_LstPrijzen acf
WHERE HstDate = (SELECT MAX(acf2.hstDate)
FROM dbo.Acf_LstPrijzen acf2
WHERE acf2.HstDate < '2013-12-01'
AND acf.id = acf2.id
GROUP BY id)
)
我会做这样的事情:
INSERT INTO TableName (column1,column2,column3,column4,column5,column6)
SELECT t.ID , t.ID2 , t.CNK , t.Omsch , t.Groep , t.prijs
FROM(
SELECT t.ID , t.ID2 , t.CNK , t.Omsch , t.Groep , t.prijs
FROM dbo.t_LstPrijzen t
WHERE HstDate = '2013-12-01'
INTERSECT
SELECT t1.ID , t1.ID2 , t1.CNK , t1.Omsch , t1.Groep , t1.Prijs,
FROM (SELECT ID, ID2, CNK , Omsch , Groep , Prijs,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY HstDate DESC) AS rnk
FROM dbo.t_LstPrijzen
WHERE HstDate < '2013-12-01') t1
WHERE t1.rnk = 1 ) t