2017-07-11 70 views
0

感謝您的幫助提前。我有一個invoice表,該表類似於下面:根據最近日期檢索列

INV DATE | ITEM |SELL PRICE |COST PRICE 
------------+-------+-----------+---------- 
30/06/2016 | DOOR1 |10   |5 

以上項目由歐盟的來源,所以我有另一個表(VEN_HIS),其中列出了供貨價格的歷史記錄如下該項目:

DATE  |ITEM |CURRENCY |PRICE 
------------+-------+-----------+------ 
17/05/2017 |DOOR1 |EUR  |6 
01/01/2017 |DOOR1 |EUR  |5.8 
29/05/2016 |DOOR1 |EUR  |5.6 
05/03/2016 |DOOR1 |EUR  |5.5 

我想要做的就是加入了表中正確的貨幣價格,我們將換句話說支付到發票表作爲在發票日期的正確價格下降5.6

這是我的第一篇文章我需要得到s FX的分析很快就完成了,所以格式化道歉,任何幫助將不勝感激。

回答

0

它是一個簡單的問題Joinwhere子句。

create table #t1 (INV_DATE DATE,ITEM varchar(100), SELL_PRICE int, COST_PRICE int); 
insert into #t1 values 
('30/06/2016','DOOR1',10,5) 

create table #ven_his (colDATE date, ITEM varchar(100), CURRENCY varchar(100),PRICE float); 
insert into #ven_his values 
('17/05/2017', 'DOOR1', 'EUR',6 ), 
('01/01/2017', 'DOOR1', 'EUR',5.8), 
('29/05/2016', 'DOOR1', 'EUR',5.6), 
('05/03/2016', 'DOOR1', 'EUR',5.5) 


SELECT top 1 a.INV_DATE, a.ITEM, b.CURRENCY, b.PRICE 
FROM #t1 a 
left join #ven_his b 
on a.ITEM = b.ITEM 
WHERE b.colDATE <=a.INV_DATE 
ORDER BY b.colDATE DESC 

輸出:

#t1 
INV_DATE ITEM SELL_PRICE COST_PRICE 
---------- ------ ----------- ----------- 
2016-06-30 DOOR1 10   5 


#ven_his 
colDATE ITEM  CURRENCY  PRICE 
---------- --------- ------------ ------ 
2017-05-17 DOOR1  EUR   6 
2017-01-01 DOOR1  EUR   5.8 
2016-05-29 DOOR1  EUR   5.6 
2016-03-05 DOOR1  EUR   5.5 


INV_DATE ITEM  CURRENCY PRICE 
---------- --------- ----------- -------- 
2016-06-30 DOOR1  EUR   5.6 
0
DECLARE @invoice table (INV_Date date, Item varchar(100),SellPrice numeric(5,2), Costprice numeric(5,2)) 

INSERT INTO @invoice 

SELECT '20160630','DOOR1',10,5 

SELECT * FrOm @invoice 





DECLARE @ven_his table(Dates date,Item varchar(100),Currency varchar(10),Price numeric(5,2)) 

INSERT INTO @ven_his 

SELECT '20170517','DOOR1','EUR',6 

UNION ALL 

SELECT '20170101','DOOR1','EUR',5.8 

UNION ALL 

SELECT '20160529','DOOR1','EUR',5.6 

UNION ALL 

SELECT '20160305','DOOR1','EUR',5.5 



SELECT * FROM @ven_his 



SELECT Top 1 i.Item,INV_Date,PRICE,Dates 

FROM @invoice i 

JOIN @ven_his v 

ON i.Item = v.Item 

ORDER BY ABS(DATEDIFF(DAY,i.INV_Date,v.Dates)) 
0

使用鉛函數返回正確的價格對所有項目:

Create table #Invoice (INVDATE DATE, ITEM NVARCHAR(20), SELLPRICE SMALLMONEY, COSTPRICE SMALLMONEY) 

    INSERT #INVOICE VALUES ('2016-06-30', 'DOOR1', 10, 5) 
    --Test with additional invoices 
    --INSERT #INVOICE VALUES ('2017-02-22', 'DOOR1', 10, 5), ('2016-09-22', 'DOOR1', 10, 5) 

    CREATE TABLE #VENHIS ([DATE] DATE, ITEM NVARCHAR(20), CURRENCY NVARCHAR(5), PRICE SMALLMONEY) 

    INSERT #VENHIS VALUES ('2017-05-17', 'DOOR1', 'EUR', 6), ('2017-01-01', 'DOOR1', 'EUR', 5.8),('2016-05-29', 'DOOR1', 'EUR', 5.6), ('2016-03-05', 'DOOR1', 'EUR', 5.5) 

    SELECT I.*, V.PRICE 
    FROM #Invoice i 
    INNER JOIN (SELECT *, [DATE] as STARTDATE, LEAD([DATE]) OVER (PARTITION BY ITEM ORDER BY ITEM, [DATE] ASC) AS EndDate 
    FROM #VENHIS) v ON i.ITEM=v.ITEM AND I.[INVDATE] BETWEEN V.STARTDATE AND DATEADD(D,-1,ISNULL(V.ENDDATE, '2099-01-01'))