sql—至少选择两行,然后排序(没有完整的分组)

rqdpfwrv  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(255)

我有一个microsoft access表,其值如下:

id | C     | D   | ED         | T |
---+-------+-----+------------+---+
1  | 33105 | ABC | 2020/01/04 | 1 |
2  | 33105 | ABC | 2020/01/08 | 2 |
3  | 33102 | DEF | 2020/02/01 | 2 |
4  | 34145 | GHI | 2020/02/09 | 1 |
5  | 34145 | GHI | 2020/02/10 | 2 |
6  | 34162 | JKL | 2020/02/08 | 1 |

我想提取具有相同c但最低t(具有此优先级)的行,并最终按日期(ed)降序对结果排序。因此,我的预期结果如下:

id | C     | D   | ED         | T |
---+-------+-----+------------+---+
4  | 34145 | GHI | 2020/02/09 | 1 |
6  | 34162 | JKL | 2020/02/08 | 1 |
3  | 33102 | DEF | 2020/02/01 | 2 |
1  | 33105 | ABC | 2020/01/04 | 1 |

sql中最快的方法是什么(表实际上相当大)?

x7yiwoj4

x7yiwoj41#

你可以用它 NOT EXISTS :

SELECT t.*
FROM tablename AS t
WHERE NOT EXISTS (SELECT 1 FROM tablename WHERE C = t.C AND T < t.T)

或使用相关子查询:

SELECT t.*
FROM tablename AS t
WHERE t.T = (SELECT MIN(T) FROM tablename WHERE C = t.C)

相关问题