我有这个查询来获取表格中的双打,但是在不同的日期:
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)
我的问题是:如何将此结果插入另一个表?有可能吗?
答案 0 :(得分:2)
试试这个......
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)
)
答案 1 :(得分:0)
我会做这样的事情:
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