0

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.

The version number should be calculated by MAX(major * 1000000 + minor * 1000 + patch) in the versions table.

So from the given example of the Apps table the result would be:

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.

MySQL Version 15.1 distribution 5.5.56-MariaDB

Any help would be appreciated.

Cheers!

Salman A
  • 262,204
  • 82
  • 430
  • 521

5 Answers5

2

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:

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 |
+---------+----------+-----------+----------------+
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Brillian @Salman A, this is almost what i need, but would it be possible to return just an original `VERSION_ID` column at the end instead of the last 5 columns? Cheers! – Marius Kurgonas Sep 14 '18 at 13:26
  • @MariusKurgonas just name the columns (I used `*` to show all details including unnecessary ones). – Salman A Sep 14 '18 at 13:34
  • Cheers! This is what i need - but with the original names of the `Apps` table columns. So if i renamed `version_id_max` to `version_id` that would be it! – Marius Kurgonas Sep 14 '18 at 13:43
1

Utilizing Nested Derived subqueries, and a bit of hacky way of identifying VERSION_ID corresponding to MAX VERSION_NO.

We basically first get a derived table determining the VERSION_NO for each row in the Apps table.

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.

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 

SQL FIDDLE

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
1

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
Zebra8844
  • 141
  • 5
0

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))
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
0

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).

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;
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69