根据最近的日期检索列

时间:2017-07-11 11:14:41

标签: sql-server tsql

提前感谢您的帮助。我有一个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

这是我的第一篇文章,我需要快速完成一些FX分析,以便对格式化表示歉意,我们将非常感谢任何帮助。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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))

答案 2 :(得分:0)

使用LEAD功能为所有项目返回正确的价格:

    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'))