[英]sql select max date in a query
我有這樣的查詢
SELECT DISTINCT
FND.ID_CON,
SPRT.CODE,
SPRT.NOM,
SPRT.DATE_VALUE,
COTPLACE.LIBELLE
FROM
FND,
SPRT,
CONTRACT,
COTPLACE
WHERE
FND.code=SPRT.code
and FND.cot_place=SPRT.cot_place
and FND.cot_place=COTPLACE.cot_place(+)
and FND.origine=SPRT.origine
and FND.ID_CON=CONTRACT.ID_CON
and FND.ORIGINE=CONTRACT.ORIGINE
and SPRT.code = '12345678'
and CONTRACT.ID_CON like '%ABC123%'
....
此查詢返回兩個具有不同DATE_VALUE的lignes如何僅選擇最大DATE_VALUE的行? 謝謝
您可以使用max
:
select nom, prenom, max(date_empl)
from your_table
where your_condition
group by nom, prenom
您可以order by
+ LIMIT 1
使用order by
:
select nom, prenom, date_empl
from person, employ, enterprise
where person.id= "test"
and person.id= emploi.id
and person.enterpriseName = enterprise.name
order by date_empl DESC LIMIT 1
這回答了問題的原始版本。
假設您的意思是您為同一nom
/ prenom
組合獲得多個行。 一種典型的方法是:
select p.nom, p.prenom, max(e.date_empl)
from person p join
employ em
on p.id = em.id
where p.id = 'test'
group by p.nom, p.prenom;
該查詢似乎不需要enterprise
表。
筆記:
FROM
子句中使用逗號。 始終使用正確的顯式JOIN
語法。 如果只想返回一行,則可以使用ORDER BY
和某種限制結果的方式。 ANSI標准方法是:
select p.nom, p.prenom, e.date_empl
from person p join
employ em
on p.id = em.id
where p.id = 'test'
order by e.date_empl desc
fetch first 1 row only;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.