1

我们将汇率存储在一个表格中,并每周更新几次以获取最新汇率。我试图能够在销售订单上取一个日期,并为其分配订单时有效的汇率。下面是一个汇率列表及其生效日期的示例,以及销售订单,以及应该是什么有效汇率(汇率@订单时间,这是我想要引入的)。

currate.effectivedate      currate.currentrate
12/2/2012                  0.55
12/7/2012                  0.52
12/13/2012                 0.54
12/19/2012                 0.53
12/22/2012                 0.56

orderhed.orderdate        orderhed.ordernum     Exchage Rate @ date of order
12/4/2012                  105                  0.55
12/8/2012                  111                  0.52
12/9/2012                  116                  0.52
12/19/2012                 120                  0.53
12/28/2012                 123                  0.56

我在下面有一个我一直在尝试做的例子,但它不起作用。首先,由于 currate.effectivedate 上的 MAX 函数,我想不出如何引入 currate.currentrate 字段。然后对于 t_rate.effectivedate,我得到的只是表格期间汇率的 MAX 天,而不是 <= orderhed.orderdate 的最大日期。我需要小于或等于 orderhed.orderdate 的 MAX currate.effectivedate 才能获得订单时有效的生效日期(以及相应的 currentrate.currentrate)。如果我重复自己,或者没有意义,我很抱歉。如果下面我悲伤的小查询不明显,我对编写查询非常陌生,因此非常感谢我能获得的任何帮助。

SELECT     
orderhed.ordernum, 
orderhed.orderdate, 
orderdtl.orderline, 
orderdtl.unitprice, 
orderdtl.docunitprice, 
orderdtl.discount, 
orderdtl.docdiscount, 
t_rate.effectivedate

FROM         orderhed LEFT OUTER JOIN

(SELECT     rate1.company, MAX(rate1.effectivedate) AS effectivedate, order1.orderdate
FROM         currrate as rate1 INNER JOIN
                  orderhed AS order1 ON rate1.company = order1.company
WHERE     (rate1.company = 'lotcol') AND (rate1.currencycode = 'usd') 
GROUP BY rate1.company, order1.orderdate) 

AS t_rate ON orderhed.company = t_rate.company AND t_rate.orderdate <= orderhed.orderdate 
                        INNER JOIN
                  orderdtl ON orderhed.company = orderdtl.company 
WHERE     (orderhed.company = 'lotcol') 

更新 - -

谢谢@LastCoder,你的回答让我朝着正确的方向前进。归根结底,这正是奏效的方法:

SELECT     orderhed.ordernum, orderhed.orderdate, orderdtl.orderline, orderdtl.unitprice, orderdtl.docunitprice, orderdtl.discount, orderdtl.docdiscount, t_rate.effectivedate, 
                  t_rate.ordernum AS Expr1, t_rate1.currentrate
FROM         orderhed INNER JOIN
                      (SELECT     rate1.company, MAX(rate1.effectivedate) AS effectivedate, order1.orderdate, order1.ordernum
                        FROM          currrate AS rate1 INNER JOIN
                                               orderhed AS order1 ON rate1.company = order1.company AND rate1.effectivedate <= order1.orderdate
                        WHERE      (rate1.company = 'lotcol') AND (rate1.currencycode = 'usd')
                        GROUP BY rate1.company, order1.orderdate, order1.ordernum) AS t_rate ON orderhed.ordernum = t_rate.ordernum AND orderhed.company = t_rate.company AND 
                  t_rate.orderdate <= orderhed.orderdate INNER JOIN
                      (SELECT     company, currencycode, effectivedate, currentrate
                        FROM          currrate AS currrate_1
                        WHERE      (company = 'lotcol') AND (currencycode = 'usd')) AS t_rate1 ON t_rate.company = t_rate1.company AND 
                  t_rate.effectivedate = t_rate1.effectivedate INNER JOIN
                  orderdtl ON orderhed.company = orderdtl.company AND orderhed.ordernum = orderdtl.ordernum
WHERE     (orderhed.company = 'lotcol')
ORDER BY orderhed.ordernum
4

1 回答 1

1
SELECT o.orderdate
    , ISNULL(r.effectivedate, (
        SELECT MAX(effectivedate) FROM t_rate WHERE effectivedate < o.orderdate
    )
FROM orderhed o
LEFT JOIN t_rate r ON o.orderdate = r.effectivedate

您的问题有一个最小的解决方案。您需要将“订单日期”与等于或小于它的“生效日期”相匹配。

于 2013-02-12T17:05:36.183 回答