gpt4 book ai didi

mysql - 具有多个条件的 SQL group by

转载 作者:行者123 更新时间:2023-11-29 00:19:15 25 4
gpt4 key购买 nike

我正在处理访客日志数据,需要按 IP 地址对其进行汇总。数据如下所示:

id        | ip_address     | type     | message  | ...----------+----------------+----------+----------------1         | 1.2.3.4        | purchase | ...2         | 1.2.3.4        | visit    | ...3         | 3.3.3.3        | visit    | ...4         | 3.3.3.3        | purchase | ...5         | 4.4.4.4        | visit    | ...6         | 4.4.4.4        | visit    | ...

And should summarize with:

type="purchase" DESC, type="visit" DESC, id DESC

产量:

chosenid  | ip_address     | type     | message  | ...----------+----------------+----------+----------------1         | 1.2.3.4        | purchase | ...4         | 3.3.3.3        | purchase | ...6         | 4.4.4.4        | visit    | ...

是否有一种优雅的方式来获取这些数据?


下面是一个丑陋的方法:

set @row_num = 0; CREATE TEMPORARY TABLE IF NOT EXISTS tt AS SELECT *,@row_num:=@row_num+1 as row_index FROM log ORDER BY type="purchase" DESC, type="visit" DESC, id DESCORDER BY rating desc;

然后获取每个ip_address的最小row_index和id (https://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column)

然后将这些id连接回原表

最佳答案

我认为这应该是您需要的:

SELECT yourtable.*
FROM
yourtable INNER JOIN (
SELECT ip_address,
MAX(CASE WHEN type='purchase' THEN id END) max_purchase,
MAX(CASE WHEN type='visit' THEN id END) max_visit
FROM yourtable
GROUP BY ip_address) m
ON yourtable.id = COALESCE(max_purchase, max_visit)

请参阅 fiddle here .

我的子查询将返回最大购买 ID(如果没有购买则返回 null)和最大访问 ID。然后我用 COALESCE 连接表,如果 max_purchase 不为空,连接将在 max_purchase 上,否则它将在 max_visit 上。

关于mysql - 具有多个条件的 SQL group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21740183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com