使用 WHERE 子句子查询,通过决胜局选择每组中最大的 1 个

问题描述 投票:0回答:3

我在 SQLite 3.38.2 数据库中有一个 RoadInsp 表。为了解决这个问题,我已将数据放入 CTE 中:

with roadinsp (objectid, asset_id, date_, condition) as (
values
(1, 1, '2016-04-01', 20),
(2, 1, '2019-03-01', 19),
(3, 1, '2022-01-01', 18),
  
(4, 2, '2016-04-01', 17),
(5, 2, '2022-01-01', 16),
  
(6, 3, '2022-03-01', 15),  --duplicate date
(7, 3, '2022-03-01', 14),  --duplicate date
(8, 3, '2019-01-01', 13)
)  
select * from roadinsp

objectid  asset_id      date_   condition
       1         1  2016-04-01         20
       2         1  2019-03-01         19
       3         1  2022-01-01         18

       4         2  2016-04-01         17
       5         2  2022-01-01         16

       6         3  2022-03-01         15
       7         3  2022-03-01         14
       8         3  2019-01-01         13

演示


我使用的 GIS 软件只允许我在 WHERE 子句/SQL 表达式中使用 SQL,而不是完整的 SELECT 查询。

我想使用 WHERE 子句选择“每组中最大的 1 个”。换句话说,对于每个 ASSET_ID,我想选择具有最新日期的行。 我可以使用这样的 WHERE 子句表达式来实现这一点:

date_ = (select max(subq.date_) from roadinsp subq where roadinsp.asset_id = subq.asset_id) objectid asset_id date_ condition 3 1 2022-01-01 18 5 2 2022-01-01 16 6 3 2022-03-01 15 7 3 2022-03-01 14

这可行,但它为资产 #3 选择两行,因为该资产有两行具有相同的日期。

所以我想通过选择条件值最高的行来打破平局。它看起来像这样:

objectid asset_id date_ condition 3 1 2022-01-01 18 5 2 2022-01-01 16 6 3 2022-03-01 15 --this row has a higher condition value than the other duplicate row. --so the other duplicate row was omitted.

我只想为每个资产选择一行。因此,如果条件值也重复,那么选择什么条件并不重要,只要只选择一行即可。

使用 WHERE 子句子查询,如何选择
每组最大的 1 个

,并使用具有最大条件的行打破平局?

sql sqlite where-clause greatest-n-per-group arcgis
3个回答
3
投票

SELECT * FROM roadinsp r WHERE objectid IN ( SELECT objectid FROM roadinsp WHERE asset_id = r.asset_id ORDER BY date_ DESC, condition DESC LIMIT 1 )

这可以轻松扩展到每个资产 
n

行。并且可以轻松地将附加条件添加到 ORDER BY 中。

演示


2
投票
where

子句中添加另一个类似的条件(据我了解您的问题,这就是您可以修改的全部内容?): select * from roadinsp r where date_ = ( select max(subq.date_) from roadinsp subq where r.asset_id = subq.asset_id ) and condition = ( select Max(condition) from roadinsp c where c.date_ = r.date_ and c.asset_id = r.asset_id );

更新了
小提琴


-1
投票
要求

我使用的 GIS 软件只允许我在 WHERE 子句/SQL 表达式中使用 SQL,而不是完整的 SELECT 查询。
  • 使用 WHERE 子句子查询,如何选择每组中最大的 n,并与具有最大条件的行断开联系?
业务规则

对于每个
    asset_id
  • ,选择最近的
    date_
    如果同一 
  • date_
  • 上有多于一行,请选择
    condition
     最大的一行
    
解决方案

    date_
  • condition
    连接成一个,然后选择 max() 如下:
    添加前导 0 取决于 
  • condition
  •  的最大位数
    
  • select * from roadinsp where date_ || substr('00'||condition,-2,2) = (select max(subq.date_ || substr('00'||condition,-2,2)) from roadinsp subq where roadinsp.asset_id = subq.asset_id);
结果:

objectid|asset_id|date_ |condition| --------+--------+----------+---------+ 3| 1|2022-01-01| 18| 5| 2|2022-01-01| 16| 6| 3|2022-03-01| 15|

更新:

如果有多行具有相同的
    date_
  • condition
    ,请将
    objectid
    添加到列表中:
    
    
  • select * from roadinsp where (date_ || substr('00'||condition,-2,2) || substr('00000000'||objectid,-8,8)) = (select max(subq.date_ || substr('00'||condition,-2,2) || substr('00000000'||objectid,-8,8)) from roadinsp subq where roadinsp.asset_id = subq.asset_id);
© www.soinside.com 2019 - 2024. All rights reserved.