2017-03-31 38 views
0

我有以下查詢,它給出MAX(date_announced)的最新date_announced,但沒有給出相應的值爲software_product_build? 如何獲取MAX的相應software_product_build值(date_announced)?如何獲得MAX函數的相應列值?

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 
+0

Classic [「組的前n」](https://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row -per-group-in-sql /) – shmosel

+0

@shmosel - 所以我應該說除了software_product_id之外還有'by software_productbuild'組? –

+1

你想要什麼,最大日期爲software_product_id或software_productbuild? – Rams

回答

1

首先,你需要獲得每softwared prduct ID最多日期,那麼你可以找到軟件產品打造爲最大的日期。

選擇spb.software_product_build, Tmp.date_announced 從software_product_builds SPB加入 上(選擇software_product_id, MAX(date_announced)作爲date_announced 從software_product_builds 組由software_product_id)作爲TMP(tmp.software_product_id = spb.software_product_id和tmp.date_announced = spb.date_announced)

+0

讓我們說我想跳過日期 - 通過值'0000-00-00 00:00:00'宣佈在哪裏應該在上面的查詢中添加支票 –

+0

您可以在最後添加where子句和您的條件plz添加類似這裏的地方spb。日期宣佈<> your_condition – Rams

+0

你有沒有得到你想要做的? – Rams