SQL - 分组行具有最新日期

时间:2014-06-24 18:03:23

标签: mysql sql database

我有以下表格,我想知道如何对它进行查询。

表食谱

ID | Name
0  | Apple pie
1  | Pizza

表成分

RecipeID  | Timestamp | Name    | Price
0         | 10-2-2014 | Apple   | 1.20
0         | 7-2-2014  | Apple   | 1.14
0         | 9-2-2014  | Flour   | 2.00
1         | 9-2-2014  | Tomato  | 1.12

如何通过RecipeID对此进行分组,以便获得以下结果

RecipeID  | Name      | Total_Latest_Ingredient_Price
0         | Apple Pie | 3.20
1         | Pizza     | 1.12

这个想法是查询应该只采用最新日期的成分。因此,从2014年2月2日起,苹果的价格将从2014年2月2日开始,而不是一个(或两者)。所以只有最新的日期。

这可能是一个艰难的cookie(或不是!)但我希望你可以帮助我!

4 个答案:

答案 0 :(得分:2)

假设(RecipeID, Timestamp, Name)表的Ingredient组合是唯一的,并假设名为Timestamp的列以规范形式存储(例如MySQL DATE,DATETIME,TIMESTAMP数据类型或一个字符数据类型,其格式使得值的比较将始终产生"最新"时间戳值...

规范模式是使用内联视图来检索"最新的" Timestamp,然后使用JOIN操作检索整行。

SELECT g.RecipeID
     , SUM(g.Price) AS `Total_Latest_Ingredient_Price`
  FROM Ingredient g
  JOIN (SELECT h.RecipeID
             , h.Name
             , MAX(h.Timestamp) AS `Timestamp`
          FROM Ingredient h
         GROUP
            BY h.RecipeID
             , h.Name
       ) i
    ON i.RecipeID  = g.RecipeID
   AND i.Name      = g.Name
   AND i.Timestamp = g.Timestamp
 GROUP BY g.RecipeID

内嵌视图别名为 i 获取"最新" Timestamp(再次假设Timestamp列是规范形式,这样"最大"值保证为"最新"值。如果名为Timestamp的列的数据类型为MySQL DATEDATETIMETIMESTAMP,则为true。)

外部查询引用 i 返回的行,并执行JOIN操作以从表中检索整行(别名为 g )以获取相关价格。

要从食谱表中获取Name列,我们会在该表中添加JOIN操作...

SELECT g.RecipeID
     , r.Name
     , SUM(g.Price) AS `Total_Latest_Ingredient_Price`
  FROM Ingredient g
  JOIN (SELECT h.RecipeID
             , h.Name
             , MAX(h.Timestamp) AS `Timestamp`
          FROM Ingredient h
         GROUP
            BY h.RecipeID
             , h.Name
       ) i
    ON i.RecipeID  = g.RecipeID
   AND i.Name      = g.Name
   AND i.Timestamp = g.Timestamp
  JOIN Recipe r
    ON r.ID = g.RecipeID 
 GROUP BY g.RecipeID

答案 1 :(得分:1)

上面的VMai查询已经关闭,但您需要过滤最新日期...因此选择给定配方/成分的最大时间戳并将其加入到您的桌面。您还应该能够使用WHERE子句执行此操作,但是使用Ingredient表的内部联接(使用配方/成分的最大时间戳)更直接,所以我只是选择了

 SELECT
        I.RecipeID,
        R.Name,
        SUM(Price) AS Total_Ingredient_Price
    FROM
        Recipe R
    INNER JOIN
        Ingredient I
    ON
        R.ID = I.RecipeID
    INNER JOIN (SELECT MAX(Timestamp) AS latest, RecipeID, Name FROM Ingredient GROUP BY RecipieID, Name) AS c
    ON I.RecipeID = c.RecipeID
    AND I.Name = c.Name
    AND I.TimeStamp = c.latest
    GROUP BY
        I.RecipeID,
        R.Name

答案 2 :(得分:0)

这很简单: 您可以通过食谱总结成分的价格:

SELECT
    I.RecipeID,
    R.Name,
    SUM(Price) AS Total_Latest_Ingredient_Price
FROM
    Recipe R
INNER JOIN
    Ingredient I
ON
    R.ID = I.RecipeID
GROUP BY
    I.RecipeID,
    R.Name

答案 3 :(得分:0)

又一个实施:

select a.recipeid, c.name, sum(a.price) total_latest_ingredient_price
from ingredient a
join (
  select recipeid, name, max(timestamp) m
  from ingredient 
  group by recipeid, name) b on a.recipeid = b.recipeid and a.name = b.name and a.timestamp = b.m
join recipe c on a.recipeid = c.id
group by a.recipeid;

fiddle