在这里-http: //sqlfiddle.com/#!6/ce7cf/4
样本数据(如您在原始问题中设置的那样):
create table TAB1 ( cod integer primary key, n_order varchar(10) not null, s_date date not null, revision integer not null );alter table tab1 add constraint UQ1 unique (n_order,revision);insert into TAB1 values ( 1, '001/18', '2018-02-01', 0 );insert into TAB1 values ( 2, '002/18', '2018-01-31', 0 );insert into TAB1 values ( 3, '002/18', '2018-01-30', 1 );
查询:
select * from tab1 djoin ( select n_ORDER, MAX(REVISION) as REVISION FROM TAB1 Group By n_ORDER ) m on m.n_ORDER = d.n_ORDER and m.REVISION = d.REVISION
意见建议:
- 谷歌阅读经典著作:Martin Gruber的“ Understanding SQL”
- 阅读Firebird SQL参考:https : //www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-zh-CN/html/fblangref25.html
这是Firebird 3中引入的使用 窗口化函数的 另一种解决方案 -http: //sqlfiddle.com/#!6/
ce7cf/13
我手头没有Firebird 3,因此无法实际检查是否会有突然的不兼容性,请在家进行:-D
SELECt * FROM( SELECt TAB1.*, ROW_NUMBER() OVER ( PARTITION BY n_order ORDER BY revision DESC ) AS rank FROM TAB1) dWHERe rank = 1
三种解决方案(包括Gordon的一种)中哪种更快,取决于特定的数据库-实际数据,现有索引,索引的选择性。
尽管窗口函数可以进行无联接查询,但我不确定对实数据的查询会更快,因为它可能只需要忽略cortege上的索引,而是在应用条件之前
order+revision进行
全扫描
rank=1。尽管第一个解决方案很可能会使用索引来获取最大值,而无需实际读取表中的每一行。
- Firebird支持的邮件列表提出了一种跳出循环的方法,只使用一个查询即可:窍门是同时使用Windows函数和CTE(公用表表达式):http
//sqlfiddle.com/#!18/ ce7cf / 2
WITH TMP AS (
SELECt
*,
MAX(revision) OVER (
PARTITION BY n_order
) as max_REV
FROM TAB1
)
SELECt * FROM TMP
WHERe revision = max_REV



