如何获取MAX函数的相应列值?

时间:2017-03-31 21:46:31

标签: mysql database greatest-n-per-group

我有以下查询为MAX(date_announced)提供最新的date_announced但是没有给出software_product_build的相应值? 如何获得MAX的相应software_product_build值(date_announced)?

   select 
   software_product_build,
   MAX(date_announced) as date_announced
    from software_product_builds
   group by software_product_id

enter image description here

一个案例: -

   select software_product_build,
   MAX(date_announced) as date_announced
    from software_product_builds
    where software_product_build LIKE '%QCA6290.LA.0.1%' group by software_product_id;

OUTPUT:-
|------software_product_build------|-------date_announced------|
--QCA6290.LA.0.1-00001-STD.INT-6---|------2017-03-13 21:13:32---


select software_product_build from software_product_builds where date_announced='2017-03-13 21:13:32';

OUTPUT:-
CI_QCA6290.LA.0.1-03091-STD.INT-61

1 个答案:

答案 0 :(得分:1)

首先,您需要获取每个软件产品ID的最大日期,然后您才能找到该最大日期的软件产品构建。

选择spb.software_product_build,    Tmp.date_announced     来自software_product_builds spb join (选择software_product_id,    MAX(date_announced)为date_announced     来自software_product_builds    group by software_product_id)as tmp on(tmp.software_product_id = spb.software_product_id and tmp.date_announced = spb.date_announced)