Return 每个 ID 只有最近的条目
Return only most recent entry per id
我在我的 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;
它 returns 我这个结果:
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-Bienne0027_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-Bienne0027.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, Luzern0031.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-Bienne0033.doc";"01.04.2015 00:00:00";"100033";;"1080012.05";
"01.04.2015 16:47:58";"C:\Daten\Demo\Dokumente";"C:\Daten\Dokumente0033.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)。
引用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;
左连接:
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。
当面对一个连接的多个结果并且您想匹配最近日期的行时,连接条件中的相关子查询是我发现编写起来非常简单且执行速度非常快的子查询:
...
JOIN aktivitaet a
ON a.bold_id = al.aktivitaeten
AND a.creationdatetime =(
SELECT Max( creationdatetime )
FROM aktivitaet
WHERE bold_id = a.bold_id )
...
我在我的 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;
它 returns 我这个结果:
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-Bienne0027_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-Bienne0027.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, Luzern0031.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-Bienne0033.doc";"01.04.2015 00:00:00";"100033";;"1080012.05";
"01.04.2015 16:47:58";"C:\Daten\Demo\Dokumente";"C:\Daten\Dokumente0033.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)。
引用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;
左连接:
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。
当面对一个连接的多个结果并且您想匹配最近日期的行时,连接条件中的相关子查询是我发现编写起来非常简单且执行速度非常快的子查询:
...
JOIN aktivitaet a
ON a.bold_id = al.aktivitaeten
AND a.creationdatetime =(
SELECT Max( creationdatetime )
FROM aktivitaet
WHERE bold_id = a.bold_id )
...