每个ID只返回最近的条目

时间:2015-04-17 07:20:21

标签: sql firebird greatest-n-per-group firebird2.5

我在Firebird 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(即1000033/1000027)的记录,则只有一条记录(具有最后aktivitaet.creationdatetime的记录)应该在结果中。

2 个答案:

答案 0 :(得分:1)

引用MySQL manual (though it's valid for all DBMS implementing the SQL '92 Standard)

保持某一列的分组最大值的行

任务:对于每篇文章,找到价格最贵的经销商或经销商。

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

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.price具有更大的值,s2行值将为NULL。

答案 1 :(得分:1)

当遇到来自连接的多个结果并且您想要匹配最近日期的行时,连接条件中的相关子查询是我发现编写起来非常简单且执行速度非常快的子查询:

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