我有以下代码将具有最小和最大时间戳的行写入新表:
sql_sort_and_delete = """
CREATE TABLE tmp AS
SELECT DISTINCT * FROM c2g
WHERE time = (SELECT max(time) FROM c2g)
UNION SELECT DISTINCT * FROM c2g
WHERE time = (SELECT min(time) FROM c2g);;"""
它工作正常,但是 c2g 表中有不同城市的子集,我想更改代码,以便获得每个城市的不同最小/最大结果。是否有捷径可寻?谢谢!