Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Mysql MariaDB:选择值1,最大值(值2),按值3分组_Mysql_Sql_Group By_Mariadb_Maxdate - Fatal编程技术网

Mysql MariaDB:选择值1,最大值(值2),按值3分组

Mysql MariaDB:选择值1,最大值(值2),按值3分组,mysql,sql,group-by,mariadb,maxdate,Mysql,Sql,Group By,Mariadb,Maxdate,我有以下专栏: |订单id |客户id |订单时间戳|买方id |(所有整数) 它从一个听起来很简单的任务“给我看每个客户最后一次订单的买家”开始,所以基本上 SELECT client_id, max(order_timestamp), buyer_id FROM table t GROUP BY client_id; 如果小组成员能如愿工作。我知道这是一个常见的问题,但我从来没有见过这种情况,特别是除了分组依据的值之外,还需要另一个值的情况。我想使用窗口函数可能会有

我有以下专栏:

|订单id |客户id |订单时间戳|买方id |
(所有整数)

它从一个听起来很简单的任务“给我看每个客户最后一次订单的买家”开始,所以基本上

SELECT
    client_id,
    max(order_timestamp),
    buyer_id
FROM table t
GROUP BY client_id;
如果小组成员能如愿工作。我知道这是一个常见的问题,但我从来没有见过这种情况,特别是除了分组依据的值之外,还需要另一个值的情况。我想使用窗口函数可能会有所帮助,但我们使用的是MariaDB 10.0,所以这不是一个真正的选项。我尝试了不同的subselect和join,但最终的问题是,我不能使用order\u id进行join,因为我必须按client\u id进行分组。我也想到了使用client\u id和order\u时间戳进行join,但组合在表中不是唯一的,因为可能有完全相同的订单(Unix)一个客户或客户/买家组合的时间戳(是的,这将是一个边缘案例,我需要订单的买家具有更高的订单id,但我想这是另一天的问题)

如果桌子上的人都是这样

| order_id | client_id | order_timestamp | buyer_id  |
|     1    |     123   |     9876543     |    2      |
|     2    |     123   |     9876654     |    3      |
|     3    |     234   |     9945634     |    2      |
|     4    |     234   |     9735534     |    1      |
我想去

| client_id | buyer_id |
------------|----------|
|     123   |    3     |
|     234   |    2     |

希望有人能帮助我,这样我今晚就可以安心睡觉了。

如果您的MariaDB版本支持窗口功能,您可以使用
行号()

请参阅。
结果:

如果没有窗口功能,则不存在使用

select t.client_id, t.buyer_id
from tablename t
where not exists (
  select 1 from tablename
  where client_id = t.client_id 
  and (
    order_timestamp > t.order_timestamp 
    or (order_timestamp = t.order_timestamp and order_id > t.order_id)
  )  
)
如果使用max(字段),它将拾取组条件的第一列。在您的情况下,每个组的第一个客户id不是您想要的

试试这个

select client_id, order_timestamp, buyer_id from t
  where order_timestamp= 
     (select max(ot) from t as tcopy where tcopy.client_id= t.client_id ) 
  group by client_id;

MariaDB 10.0不支持窗口功能,但10.2支持。我很想使用窗口功能,但我被MariaDB 10.0卡住了。但是谢谢你的选择!这非常有效,而且非常简单,我想知道为什么我没有想到它。非常感谢。请投票并将其标记为正确答案。谢谢您的理解。请投票并将其标记为正确答案。谢谢你的理解。
select t.client_id, t.buyer_id
from tablename t
where not exists (
  select 1 from tablename
  where client_id = t.client_id 
  and (
    order_timestamp > t.order_timestamp 
    or (order_timestamp = t.order_timestamp and order_id > t.order_id)
  )  
)
select client_id, order_timestamp, buyer_id from t
  where order_timestamp= 
     (select max(ot) from t as tcopy where tcopy.client_id= t.client_id ) 
  group by client_id;