在 mysql 中为 group by 语句选择不同的参数?
Selecting different parameters for a group by statement in mysql?
我有这样的数据集:
在此数据集中,所有这 3 列都没有 NULL
值,而且 user_name
和 recording_msid
对可以重复,但每个重复的对在listened_at
.
注意 (user_name, recording_msid)
的不同对 listened_at
可以重复。
我正在尝试使用 table 分组:
SELECT
user_name, recording_msid, COUNT(*)
FROM
`listenbrainz.listenbrainz.listen`
GROUP BY
user_name, recording_msid;
并得到如下结果:
但我在这个结果中还想要的是 listened_at
的另一列,其中每一行都有最旧的 listened_at
值,用于每个重复的 (user_name, recording_msid)
对,即下面的每一行table.
But what I also want in this result is another column of listened_at
where each row has the oldest listened_at
value for every repeating pair of (user_name
, recording_msid
)
使用MIN()
:
SELECT
user_name,
recording_msid,
COUNT(*) no_records,
MIN(listened_at) min_listened_at
FROM `listenbrainz.listenbrainz.listen`
GROUP BY user_name, recording_msid;
我有这样的数据集:
在此数据集中,所有这 3 列都没有 NULL
值,而且 user_name
和 recording_msid
对可以重复,但每个重复的对在listened_at
.
注意 (user_name, recording_msid)
的不同对 listened_at
可以重复。
我正在尝试使用 table 分组:
SELECT
user_name, recording_msid, COUNT(*)
FROM
`listenbrainz.listenbrainz.listen`
GROUP BY
user_name, recording_msid;
并得到如下结果:
但我在这个结果中还想要的是 listened_at
的另一列,其中每一行都有最旧的 listened_at
值,用于每个重复的 (user_name, recording_msid)
对,即下面的每一行table.
But what I also want in this result is another column of
listened_at
where each row has the oldestlistened_at
value for every repeating pair of (user_name
,recording_msid
)
使用MIN()
:
SELECT
user_name,
recording_msid,
COUNT(*) no_records,
MIN(listened_at) min_listened_at
FROM `listenbrainz.listenbrainz.listen`
GROUP BY user_name, recording_msid;