[英]Select max version number for each group consisting of multiple columns
I have the following tables: 我有以下表格:
Apps 应用
TYPE_ID | BUILD_ID | CONFIG_ID | VERSION_ID | (All foreign keys to the respective tables)
1 | 1 | 1 | 1 |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 3 |
2 | 2 | 3 | 4 |
Versions 版本
ID | major | minor | patch
1 | 1 |0 |1
2 | 2 |0 |0
3 | 3 |0 |3
4 | 4 |0 |0
I need to select highest version rows from Apps
table for each unique combinations of TYPE_ID
, BUILD_ID
and CONFIG_ID
. 我需要从Apps
表中为TYPE_ID
, BUILD_ID
和CONFIG_ID
每个唯一组合选择最高版本的行。
The version number should be calculated by MAX(major * 1000000 + minor * 1000 + patch)
in the versions table. 版本号应通过版本表中的MAX(major * 1000000 + minor * 1000 + patch)
来计算。
So from the given example of the Apps
table the result would be: 因此,从给定的Apps
表示例中,结果将是:
TYPE_ID | BUILD_ID | CONFIG_ID | VERSION_ID |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 4 |
Have tried something like this: 尝试过这样的事情:
SELECT p1.* FROM Apps p1
INNER JOIN (
SELECT max(VERSION_ID) MaxVersion, CONFIG_ID
FROM Apps
GROUP BY CONFIG_ID
) p2
ON p1.CONFIG_ID = p2.CONFIG_ID
AND p1.VERSION_ID = p2.MaxVersion
GROUP BY `TYPE_ID`, `BUILD_ID`, `CONFIG_ID`
But MAX
is applied on the VERSION_ID
and I need MAX
to be applied on major
, minor
and patch
combinations. 但是MAX
应用于VERSION_ID
,我需要将MAX
应用于major
, minor
和patch
组合。
MySQL Version 15.1 distribution 5.5.56-MariaDB MySQL版本15.1发行版5.5.56-MariaDB
Any help would be appreciated. 任何帮助,将不胜感激。
Cheers! 干杯!
Utilizing Nested Derived subqueries, and a bit of hacky way of identifying VERSION_ID
corresponding to MAX VERSION_NO
. 利用嵌套派生子查询,和一个位的哈克的方式识别的VERSION_ID
对应于MAX VERSION_NO
。
We basically first get a derived table determining the VERSION_NO
for each row in the Apps
table. 我们基本上首先得到一个派生表,该表确定Apps
表中每一行的VERSION_NO
。
Now using that derived table as a source for SELECT
, we group by on the TYPE_ID
, BUILD_ID
and CONFIG_ID
, and using a GROUP_CONCAT and string manipulation based trick, we determine the VERSION_ID
corresponding to maximum VERSION_NO
, for a group. 现在使用派生表作为用于源SELECT
,我们组通过对TYPE_ID
, BUILD_ID
和CONFIG_ID
,并使用GROUP_CONCAT和字符串处理基于特技,我们确定VERSION_ID
对应于最大 VERSION_NO
,为一组。
Try the following: 请尝试以下操作:
SELECT nest.TYPE_ID,
nest.BUILD_ID,
nest.CONFIG_ID,
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT nest.VERSION_ID
ORDER BY nest.VERSION_NO DESC
SEPARATOR ','), ',', 1) AS VERSION_ID
FROM (
SELECT A.TYPE_ID,
A.BUILD_ID,
A.CONFIG_ID,
A.VERSION_ID,
(V.major*1000000 + V.minor*1000 + V.patch) AS VERSION_NO
FROM Apps AS A
INNER JOIN Versions AS V ON V.ID = A.VERSION_ID
) AS nest
GROUP BY nest.TYPE_ID, nest.BUILD_ID, nest.CONFIG_ID
You can compute the maximum version per type_id, build_id, config_id
using the formula described in your question, use it again same formula to locate the version: 您可以使用问题中描述的公式来计算每个type_id, build_id, config_id
的最大版本,然后再次使用相同的公式来查找版本:
SELECT sq.type_id, sq.build_id, sq.config_id, versions.id AS version_id_max
FROM (
SELECT type_id, build_id, config_id, MAX(major * 1000000 + minor * 1000 + patch) AS max_version
FROM apps
INNER JOIN versions ON apps.version_id = versions.id
GROUP BY type_id, build_id, config_id
) sq
INNER JOIN versions ON max_version = major * 1000000 + minor * 1000 + patch
+---------+----------+-----------+----------------+
| type_id | build_id | config_id | version_id_max |
+---------+----------+-----------+----------------+
| 1 | 1 | 1 | 2 |
| 2 | 2 | 3 | 4 |
+---------+----------+-----------+----------------+
Try this : 尝试这个 :
select type_id, build_id, config_id,
max(1000000*v.major+1000*v.minor+v.patch) as version
from apps a left join versions v on a.version_id=v.id
group by type_id, build_id, config_id
Try this: 尝试这个:
SELECT a1.type_id, a1.build_id, a1.config_id, a1.version_id
FROM apps a1
WHERE NOT EXISTS(
(SELECT 'NEXT'
FROM apps a2
WHERE a2.type_id = a1.type_id
AND a2.build_id = a1.build_id
AND a2.config_id = a1.config_id
AND a2.version_id > a1.version_id))
Try this query, what I do here, I imitate well-known function ROW_NUMBER() OVER (PARTITION BY Type_id, Build_id, Config_id ORDER BY major desc, minor desc, patch desc)
. 试试这个查询,我在这里做的是,我模仿著名的函数ROW_NUMBER() OVER (PARTITION BY Type_id, Build_id, Config_id ORDER BY major desc, minor desc, patch desc)
。
select @type_id_lag := 0, @build_id_lag :=0, @config_id_lag := 0, @rn := 0;
select type_id, build_id, config_id, major, minor, patch from (
select case when @type_id_lag = type_id and
@build_id_lag = build_id and
@config_id_lag = config_id then @rn := @rn + 1 else @rn := 1 rn,
@type_id_lag := type_id type_id,
@build_id_lag := build_id build_id,
@config_id_lag := config_id config_id,
v.major, v.minor, v.patch
from Apps a
left join Versions v on a.version_id = v.id
order by a.type_id, a.build_id, a.config_id,
v.major desc, v.minor desc, v.patch desc
) a where rn = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.