使用多个表按事务日期和位置查找成本

时间:2013-12-19 19:03:10

标签: sql

我正在尝试创建一个连接3个表以及多个价格和位置的查询。我正在拉动的数据。

表A - 产品,产品名称

表B - transactionid,date,product,returnreason,quantity,costprice,costamount,
          位置,记者

表C - price1,price2,price3,price4,price5,price6,product,activedate

表C 正在更新我们产品的价格,从一个日期到另一个日期可能相同,或根据市场完全变化。表B中的位置最多可以有四个不同的位置。

实施例。表C(每个产品在表格中的设置基本相同。)

product   price1    price2    price3    price4    price 5    price6    activedate 
A         0.00      0.00      0.00      0.00      0.00       0.00      1/1/11 
A         0.00      0.00      0.00      0.00      0.00       0.00      2/1/11 
A         1.00      0.00      0.63      0.00      1.20       0.20      1/1/12 
A         1.20      0.53      0.01      1.00      0.42       0.00      4/1/13

表B

product  transactionid  tdate  returnreason  quantity costprice  costamount location journalid
A        00001         5/1/11   def          100        2.00        2.50      100     000010
B        00205         6/13/11  col          250       10.00       15.00      300     000320
A        00207         4/11/13  col          50         5.00        1.50      100     000720

我需要按日期的最新价格获取信息。因此,如果我需要年度报告,我可以看到产品A - 1/1 / 13-4 / 30/13的price1为1.00,而5 / 1/13-现在的价格为1.20。

我尝试了很多东西,包括子查询和最新的(最接近的),通过创建一个where语句为activedate< = tdate来限制它,这将使我的每个活动日期低于和等于tdate。为此,请假设产品A位于位置100.如何限制在活动时间范围内激活的产品?

最近...... 我添加了行号,以确保我获得期刊的每一行...只是视觉。

选择distinct(b.transactionid),b.tdate,b.linenum b.product,b.location,
       b.journalid,c.price1,c.price2,c.price3,c.price4,c.price5,c.activedate       从表b内连接            表c c.product = b.product       其中c.activedate< = b.tdate

当我运行这个时,我得到的所有日期都少于交易日期,但我需要具体。例如,在我的例子中的第一个事务,它发生在5/1/11,当我运行查询时,它将给出表c中1/1/11和2/1/11的结果。我只需要交易发生时的价格日期。

0 个答案:

没有答案