根据日期从行中选择一个项目

时间:2013-01-03 14:18:15

标签: mysql sql select

我有两个表共享一个公共密钥'itemID',第一个表保存每个itemID的最新价格,第二个表保存itemID价格任何变化的日期和值,如图所示。只要将新条目放入tPriceHistory表中,tLatestItemPrice表就会自动更新

tLatestItemPrice TABLE
itemID,   latestPrice
---------------------
item1     400
item2     75
item3     621


tPriceHistory TABLE
itemID,   PriceChangeDate,  NewPrice
------------------------------------
item1     Jan 8th 2012      400
item1     Jan 7th 2012      300
item1     Jan 6th 2012      280
item1     Jan 3rd 2012      270
item2     Jan 8th 2012      75
item2     Jan 5th 2012      72
item2     Jan 1st 2012      60
item3     Jan 7th 2012      621
item3     Jan 6th 2012      601
item3     Jan 2nd 2012      598

我想要一个查询,它会返回tLatestItemPrice表中最新价格与特定日期商品价格之间的价格差异。即如果我要求在1月的最后一次和第4次之间更改价格,我想要一个查询返回以下数据集

itemID    Price change from 4th Jan
--------------------------
item1     130  (i.e. 400-270)
item2      15
item3      23

运行mysql Ver 14.14 Distrib 5.5.29,for Linux(x86_64)

4 个答案:

答案 0 :(得分:1)

我强烈建议使用可由数据库排序的标准格式来格式化日期(正如@JW所指出的那样)。

然后你可以做两个子选择以在给定日期获得两个不同的价格,然后在父查询中你可以对它们的价格进行减法。

这样的事情(但这是一个混乱的快速想法!):

select itemID, TO_P.NewPrice as currentPrice, (FROM_P.NewPrice - TO_P.NewPrice) as priceChange
from tPriceHistory as P 
left join (select itemID, PriceChangeDate, NewPrice from PRICES where PriceChangeDate = 'from_date') as FROM_P on FROM_P.itemID = P.itemID
left join (select itemID, PriceChangeDate, NewPrice from PRICES where PriceChangeDate = 'to_date') as TO_P on TO_P.itemID = P.itemID

显然,由于您的日期没有以机器友好的方式格式化,这只会为您提供明确日期的价格,您需要对其进行修改以使用您的数据甚至可能更改您的日期数据遵循更标准的日期格式。

答案 1 :(得分:0)

试试这个

  select t1.itemID,   (max(NewPrice)-min(NewPrice)) As PRICE  
  from  tPriceHistory t1

  group by t1.itemID
  order by t1.itemID

HERE DEMO SQLFIDDLE

答案 2 :(得分:0)

当你在表格

中将日期存储为字符串时,就会出现这种情况
SELECT  a.ITEMID, 
        d.NewPrice - a.NEWPRICE `Price change from 4th Jan`
FROM    tPriceHistory a
        INNER JOIN
        (
          SELECT itemID, 
                 DATE_FORMAT(MAX(STR_TO_DATE(PriceChangeDate, '%b %D %Y')),'%b %D %Y') maxDate
          FROM   tPriceHistory
          WHERE  STR_TO_DATE(PriceChangeDate, '%b %D %Y') <= '2012-01-04'
          GROUP BY itemID
        ) AsOfDatePrice ON a.itemID = AsOfDatePrice.ItemID AND
                           a.PriceChangeDate = AsOfDatePrice.MaxDate
        INNER JOIN 
        (
          SELECT itemID, 
                 DATE_FORMAT(MAX(STR_TO_DATE(PriceChangeDate, '%b %D %Y')),'%b %D %Y') maxDate
          FROM   tPriceHistory
          GROUP BY itemID 
        ) LatestDate ON a.itemID = LatestDate.itemid
        INNER JOIN tPriceHistory d
            ON LatestDate.itemid = d.itemID AND
               LatestDate.maxDate = d.PriceChangeDate

答案 3 :(得分:0)

我想解决这个问题:

select
  tLatestItemPrice.itemID,
  latestPrice-Substring_Index(prices, ',', -1)
from tLatestItemPrice inner join (
  select
    tPriceHistory.itemID,
    GROUP_CONCAT(NewPrice order by
       str_to_date(PriceChangeDate, '%b %D %Y')) as prices
  from tPriceHistory
  where str_to_date(PriceChangeDate, '%b %D %Y')<'2012-01-04'
  group by itemID ) mx
  on tLatestItemPrice.itemID=mx.itemID

(这并不总是最优的,因为字符串price可能会变得比group_concat_max_len更长,也可能会变慢,但我认为这个想法很好。)

请参阅this fiddle

相关问题