Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 最大列值,基于另一列值_Sql_Postgresql_Max_Distinct - Fatal编程技术网

Sql 最大列值,基于另一列值

Sql 最大列值,基于另一列值,sql,postgresql,max,distinct,Sql,Postgresql,Max,Distinct,我在Postgres中有一个表,我试图根据另一个单元格中的唯一值返回最大列值 我的桌子现在看起来像这样 |---------------------|------------------| | endtime | woid | |---------------------|------------------| | 1515224469053 | 1 | |---

我在Postgres中有一个表,我试图根据另一个单元格中的唯一值返回最大列值

我的桌子现在看起来像这样

 |---------------------|------------------|
 |      endtime        |       woid       |
 |---------------------|------------------|                 
 |    1515224469053    |         1        |
 |---------------------|------------------|                 
 |    1515224472342    |         1        |
 |---------------------|------------------|
 |    1515224459092    |         2        |
 |---------------------|------------------|                 
 |    1515224429053    |         2        |
 |---------------------|------------------|
 |    1515224402345    |         2        |
 |---------------------|------------------|                 
 |    1515224465033    |         3        |
 |---------------------|------------------|
…我需要在endtime字段中返回最大值,其中woid字段是唯一的

因此,生成的表如下所示:

 |---------------------|--------------|------------------|
 |      endtime        |     woid     |    Max(endtime)  |
 |---------------------|--------------|------------------|              
 |    1515224469053    |       1      |   1515224472342  |
 |---------------------|--------------|------------------|                 
 |    1515224472342    |       1      |   1515224472342  |
 |---------------------|--------------|------------------|
 |    1515224459092    |       2      |   1515224459092  |
 |---------------------|--------------|------------------|                 
 |    1515224429053    |       2      |   1515224459092  |
 |---------------------|--------------|------------------|
 |    1515224402345    |       2      |   1515224459092  |                  
 |---------------------|--------------|------------------|                 
 |    1515224465033    |       3      |   1515224465033  |
 |---------------------|--------------|------------------|


按woid分组并加入以获得相应的max_endtime

select t1.endtime, t1.woid, t2.max_endtime
from table t1
join (select woid, max(endtime) as max_endtime
     from table t2
     group by woid) t2
on t1.woid = t2.woid

我只是在现有的SELECT语句中添加了“MAX(endtime)OVER(partitionbywoid)”。您是否可以将其调整为显示第二高的值?@user3580480是的,但您可能想问一个新问题。因为您的一些数据没有第二个最高值,所以需要指定null的大小写。我在这里为你做了一把小提琴还有什么,请问一个新问题
select t1.endtime, t1.woid, t2.max_endtime
from table t1
join (select woid, max(endtime) as max_endtime
     from table t2
     group by woid) t2
on t1.woid = t2.woid