在 python 中提取 SQL table 的 MIN MAX

Extract MIN MAX of SQL table in python

我有一个包含以下信息的数据库:

+----+------+-----+-----+------+------+-----------+    
| id | time | lat | lon | dep  | dest |  reg      |    
+----+------+-----+-----+------+------+-----------+    
| a  |    1 |  10 |  20 | home | work | alpha     |    
| a  |    6 |  11 |  21 | home | work | alpha     |    
| a  |   11 |  12 |  22 | home | work | alpha     |    
| b  |    2 |  70 |  80 | home | cine | beta      |    
| b  |    8 |  70 |  85 | home | cine | beta      |    
| b  |   13 |  70 |  90 | home | cine | beta      |    
+----+------+-----+-----+------+------+-----------+    

是否可以提取以下信息:

+----+------+------+----------+----------+----------+----------+------+------+--------+    
| id | tmin | tmax | lat_tmin | lon_tmin | lat_tmax | lon_tmax | dep  | dest |   reg  |    
+----+------+------+----------+----------+----------+----------+------+------+--------+    
| a  |    1 |   11 |       10 |       20 |       12 |       22 | home | work | alpha  |    
| b  |    2 |   13 |       70 |       80 |       70 |       90 | home | cine | beta   |    
+----+------+------+----------+----------+----------+----------+------+------+--------+

如果 dep 和 dest 不同怎么办 - select 他们会怎样?

感谢

select min(t.time) as tmin, max(t.time) as tmax, (...) from table_name t group by t.dest

(...) 对其他列重复

您可以为此使用 window 函数:

SELECT     t0.id,
           t0.time as     tmin, t1.time as     tmax,
           t0.lat  as lat_tmin, t1.lat  as lat_tmax,
           t0.lon  as lon_tmin, t1.lon  as lon_tmax,
           t0.dep,
           t0.dest,
           t0.reg
FROM       ( SELECT *, 
                    row_number() over (partition by id order by time asc) as rn
             FROM   t) AS t0
INNER JOIN ( SELECT *, 
                    row_number() over (partition by id order by time desc) as rn
             FROM   t) AS t1
        ON t0.id = t1.id
WHERE      t0.rn = 1 
       AND t1.rn = 1

这将 return 每个 id 的第一行和最后一行的数据,当按 id 排序时,时间.

depdestreg 的值取自第一行 (仅根据 id)。

如果您还想为 when 设置单独的行 - 对于相同的 id - 您有不同的 dep 值或dest,然后只需将它们添加到 partition by 子句中。一切都取决于您在这种情况下期望的输出:

SELECT     t0.id,
           t0.time as     tmin, t1.time as     tmax,
           t0.lat  as lat_tmin, t1.lat  as lat_tmax,
           t0.lon  as lon_tmin, t1.lon  as lon_tmax,
           t0.dep,
           t0.dest,
           t0.reg           
FROM       ( SELECT *, 
                    row_number() over (partition by id, dep, dest, reg 
                                       order by time asc) as rn
             FROM   t) AS t0
INNER JOIN ( SELECT *, 
                    row_number() over (partition by id, dep, dest, reg 
                                       order by time desc) as rn
             FROM   t) AS t1
        ON t0.id = t1.id
WHERE      t0.rn = 1 
       AND t1.rn = 1

由于 this remark in the documentation:

,请考虑为列 time 使用不同的名称

The following keywords could be reserved in future releases of SQL Server as new features are implemented. Consider avoiding the use of these words as identifiers.

... TIME ...