Sql 选择max,并使用tiebreaker
我有一张Sql 选择max,并使用tiebreaker,sql,ms-access,greatest-n-per-group,ms-access-2013,Sql,Ms Access,Greatest N Per Group,Ms Access 2013,我有一张道路检查表表格: create table road_insp ( insp_id integer, road_id integer, insp_date date, condition number, insp_length number ); --Run each insert statement, one at a time. INSERT INTO road_insp (insp_id, road_id, insp_date, condi
道路检查表
表格:
create table road_insp
(
insp_id integer,
road_id integer,
insp_date date,
condition number,
insp_length number
);
--Run each insert statement, one at a time.
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (1, 100, #1/1/2017#, 5.0, 20);
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (2, 101, #2/1/2017#, 5.5, 40);
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (3, 101, #3/1/2017#, 6.0, 60);
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (4, 102, #4/1/2018#, 6.5, 80);
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (5, 102, #5/1/2018#, 7.0, 100);
INSERT INTO road_insp (insp_id, road_id, insp_date, condition, insp_length) VALUES (6, 102, #5/1/2018#, 7.5, 120);
+---------+---------+-----------+-----------+-------------+
| insp_id | road_id | insp_date | condition | insp_length |
+---------+---------+-----------+-----------+-------------+
| 1 | 100 | 1/1/2017 | 5 | 20 |
| 2 | 101 | 2/1/2017 | 5.5 | 40 |
| 3 | 101 | 3/1/2017 | 6 | 60 |
| 4 | 102 | 4/1/2018 | 6.5 | 80 |
| 5 | 102 | 5/1/2018 | 7 | 100 |
| 6 | 102 | 5/1/2018 | 7.5 | 120 |
+---------+---------+-----------+-----------+-------------+
我可以根据道路选择最近的检查:
SELECT
b.insp_id,
b.road_id,
b.insp_date,
b.condition,
b.insp_length
FROM
road_insp b
WHERE
b.insp_date=(
select
max(insp_date)
from
road_insp a
where
a.road_id = b.road_id
);
+---------+---------+-----------+-----------+-------------+
| insp_id | road_id | insp_date | condition | insp_length |
+---------+---------+-----------+-----------+-------------+
| 1 | 100 | 1/1/2017 | 5 | 20 |
| 3 | 101 | 3/1/2017 | 6 | 60 |
| 5 | 102 | 5/1/2018 | 7 | 100 |
| 6 | 102 | 5/1/2018 | 7.5 | 120 |
+---------+---------+-----------+-----------+-------------+
但是,正如您所看到的,每个道路、每个日期都可以进行多次检查(检查#5
和#6
)。结果是每条道路返回多条记录
相反,如果每条道路、每个日期都有多个检查,我想通过只选择最长的检查来打破僵局
+---------+---------+-----------+-----------+-------------+
| insp_id | road_id | insp_date | condition | insp_length |
+---------+---------+-----------+-----------+-------------+
| 1 | 100 | 1/1/2017 | 5 | 20 |
| 3 | 101 | 3/1/2017 | 6 | 60 |
| 6 | 102 | 5/1/2018 | 7.5 | 120 | <--Largest length.
+---------+---------+-----------+-----------+-------------+
+---------+---------+-----------+-----------+-------------+
|检查id |道路id |检查日期|状况|检查长度|
+---------+---------+-----------+-----------+-------------+
| 1 | 100 | 1/1/2017 | 5 | 20 |
| 3 | 101 | 3/1/2017 | 6 | 60 |
|6 | 102 | 5/1/2018 | 7.5 | 120 |您可以使用order by
和top
:
SELECT ri.*
FROM road_insp as ri
WHERE ri.insp_id = (select top 1 ri2.insp_id --removed brackets on top(1)
from road_insp as ri2
where ri2.road_id = ri.road_id
order by ri2.insp_date desc, ri2.insp_length desc,
ri2.insp_id
);