返回每个ID只有最新的条目(Return only most recent entry per id

2019-10-22 17:28发布

我跑我的火鸟2.5-DB此SQL脚本:

SELECT aktivitaet.creationdatetime,
(select STRINGPROPVALUE from PROPERTY WHERE PROPERTYNAME LIKE 'GlobalDokPfad') as basispfad, 
aktivitaet.pfad, 
cast(rechnung.datum as date),
rechnung.nummer, 
projekt.code, 
cast(rechnung.verrtotal as numeric(10,2)), 
projekt.betreffend 
FROM rechnung 
INNER JOIN aktivitaetenlink ON rechnung.bold_id=aktivitaetenlink.eintraege 
INNER JOIN aktivitaet ON aktivitaetenlink.aktivitaeten=aktivitaet.bold_id 
Left JOIN projekt ON aktivitaet.projekt=projekt.bold_id 
where rechnung.datum >= '01.01.2013' and rechnung.verrechnet=1 and aktivitaet.typ=15104 
order by rechnung.nummer, aktivitaet.creationdatetime DESC;

它返回了我这样的结果:

CREATIONDATETIME,BASISPFAD,PFAD,CAST,NUMMER,CODE,CAST,BETREFFEND 
"14.05.2014 16:53:25";"C:\Daten\Demo\Dokumente";"C:\Daten\Demo\Dokumente\Comte AG,Biel-Bienne\100027_2.doc";"18.12.2014 00:00:00";"100027";;"9491.0499999999993"; 
"14.05.2014 16:53:01";"C:\Daten\Demo\Dokumente";"C:\Daten\Demo\Dokumente\Comte AG, Biel-Bienne\100027.doc";"18.12.2014 00:00:00";"100027";;"9491.0499999999993"; 
"17.10.2014 14:06:08";"C:\Daten\Demo\Dokumente";"C:\Daten\Demo\Dokumente\Baumann GmbH, Luzern\100031.doc";"17.10.2014 00:00:00";"100031";;"842.39999999999998"; 
"16.04.2015 09:51:03";"C:\Daten\Demo\Dokumente";"C:\Daten\Demo\Dokumente\Comte AG, Biel-Bienne\100033.doc";"01.04.2015 00:00:00";"100033";;"1080012.05";
"01.04.2015 16:47:58";"C:\Daten\Demo\Dokumente";"C:\Daten\Dokumente\100033.doc";"01.04.2015 00:00:00";"100033";"COMTE AG";"1080012.05";"Treuhand" 
"15.04.2015 09:24:46";"C:\Daten\Demo\Dokumente";"100035.doc";"15.04.2015 00:00:00";"100035";;"3240.";

我的问题:

如果存在2条记录与同rechnung.nummer(即百万零二十七分之百万零三十三),只有一条记录(一个与最后aktivitaet.creationdatetime)应在结果中。

Answer 1:

当面对从连接多个结果,你想最近日期排,在连接标准相关子查询匹配是一个我发现是编写非常简单,非常快的执行:

...
JOIN    aktivitaet  a
    ON  a.bold_id = al.aktivitaeten
    AND a.creationdatetime =(
        SELECT  Max( creationdatetime )
        FROM    aktivitaet
        WHERE   bold_id = a.bold_id )
...


Answer 2:

引述MySQL手册(虽然它的有效实施SQL '92标准的所有DBMS) :

行持有某列的组间最大值

任务:为每篇文章,找到经销商或经销商最昂贵的代价。

这个问题可以通过这样一个子查询来解决:

SELECT article, dealer, price
FROM   shop s1
WHERE  price=(SELECT MAX(s2.price)
              FROM shop s2
              WHERE s1.article = s2.article);

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0001 | B      |  3.99 |
|    0002 | A      | 10.99 |
|    0003 | C      |  1.69 |
|    0004 | D      | 19.95 |
+---------+--------+-------+

前面的示例使用相关子查询,其可以是低效的(见第13.2.10.7“相关子查询”)。 解决该问题的其他可能性都在使用一个关联子查询FROM子句或LEFT JOIN。

不相关子查询:

SELECT s1.article, dealer, s1.price
FROM shop s1
JOIN (
  SELECT article, MAX(price) AS price
  FROM shop
  GROUP BY article) AS s2
  ON s1.article = s2.article AND s1.price = s2.price;

LEFT JOIN:

SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price
WHERE s2.article IS NULL;

该LEFT JOIN的基础上工作,当s1.price是在其最大价值,有一个更大的价值和S2行的值将是NULL没有s2.price。



文章来源: Return only most recent entry per id