Getting the latest entry for a distinct column value [duplicate] - mysql

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 4 years ago.
I have 2 tables I need to reference software_releases and platforms. The software releases will contain a platform id, software id and version number.
I want to get the latest version number for each of the distinct platforms.
The query that I currently have is the following:
SELECT ReleaseID,
DateCreated,
SoftwareID,
platforms.PlatformID,
PlatformName,
VersionNumber
FROM software_releases, platforms
WHERE software_releases.PlatformID = platforms.PlatformID
AND software_releases.SoftwareID='3'
ORDER BY VersionNumber DESC
Which returns:
5 27/05/2017 22:37 3 7 Windows 3.0.0.0
9 27/05/2017 22:56 3 7 Windows 2.6.0.0
7 27/05/2017 22:46 3 5 Android 2.5.1.1
1 27/05/2017 23:21 3 5 Android 2.5.0.0
The column order is as follows:
ReleaseID
Date Released
Software ID
Platform ID
Version ID
What I am looking for is getting the latest release for each platform returned for the specified software ID, therefore, I am only wanting to return the following:
5 27/05/2017 22:37 3 7 Windows 3.0.0.0
7 27/05/2017 22:46 3 5 Android 2.5.1.1

I got this answer from CBrowe comment, I had no idea what I was after was called a group wise maximum query.
I changed my query to be
SELECT * FROM (SELECT DateCreated, SoftwareID, software_releases.PlatformID,
VersionNumber, PlatformName FROM software_releases, platforms WHERE
SoftwareID='3' AND
platforms.PlatformID = software_releases.PlatformID ORDER BY VersionNumber DESC)
AS s GROUP BY PlatformID

Related

Order alphabetically but according to a parent

Given the table below of categories, would there be a way where I return the names alphabetically, but include the ones with a parent_id under its parent?
ID
parent_id
name
1
NULL
Hardware
2
NULL
Software
3
NULL
Networking
4
1
Desktop
5
1
Printer
6
1
Laptop
7
2
Office
8
2
Windows
9
2
Other
10
3
Outage
11
3
Firewall
12
NULL
Accounts
13
12
New Account
14
12
Password Reset
This query is used with PHP, so I could always organized them through php code but it got me thinking if there was a way to do this in the query directly. I'm drawing a blank on how to approach this. This particular example only goes 1 level deep with children, so a solution that handles that I would be fine with at the moment. But is there an approach that could allow multiple levels of children as well?
For example, if I just pulled only parent categories (rows with a null parent_id) and ordered by name I'd have Accounts, Hardware, Software, Networking. But I'd like to have:
ID
parent_id
name
12
NULL
Accounts
13
12
New Account
14
12
Password Reset
1
NULL
Hardware
4
1
Desktop
6
1
Laptop
5
1
Printer
3
NULL
Networking
11
3
Firewall
10
3
Outage
2
NULL
Software
7
2
Office
9
2
Other
8
2
Windows
I finally came up with a solution, much simpler than I was expecting. This will work for my example, but will not take into account multiple levels deep.
SELECT p.id, p.parent_id, p.cat_name, COALESCE(c.cat_name, p.cat_name) as g
FROM category p
LEFT JOIN category c
ON p.parent_id = c.id
ORDER BY g, parent_id, cat_name
To account for children who's parent may be a child itself, I do not yet have a solution but feel like that answer lies in using WITH RECURSIVE.

mysql remove duplicate data if sequential

I have previously made a question here select only when different value and it has been answered.
Now my question is how to remove double data so that the usual select query can produce the desired data
For example I have this records:
id
name
price
1
book
5
2
lamp
7
3
lamp
7
4
book
5
5
book
5
the result I want is:
id
name
price
1
book
5
2
lamp
7
4
book
5

How To Get Row Entries with Max Column in SQL, Please Help! :p [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
I am a bit stuck on how to get all the rows I want from the max count of one column, grouping by only some specific columns. For example, over here I want to group by user and name so that they are always distinct. But for country and device type I only want the combination of their max event_ct to show up
user
name
country
device
event_ct
5
Albert
US
Mac
5
5
Albert
GB
Phone
7
5
Albert
CN
Mac
3
6
Albert
CN
Mac
1
7
Laurel
US
Phone
7
I want a mysql query to turn it into
user
name
country
device
5
Albert
GB
Phone
6
Albert
CN
Mac
7
Laurel
US
Phone
Please help! Thanks :-)
Although I still think this is a duplicate question but for the good measure.
you can use subquery in where clause:
select user,name,country,device from cte
where event_ct in (select max(event_ct) from cte group by user,name)
here is db<>fiddle for better examine.
group by and distinct are fundamental same in some way, because you are telling SQL that you want UNIQUE row. group by a,b will give you distinct combination of a and b since it "GROUP" all the same (a,b) combination together.
re-edit
in some case if you got more then one row with max event_ct
for example user 5 got two row with event_ct = 7
you can add distinct to get rid of duplicate.

select highest value in mysql [duplicate]

This question already has answers here:
Select max value of each group
(8 answers)
How to select a maximum value row in mysql table
(7 answers)
Closed 4 years ago.
So I have a database that has some values like this:
Level , Indicator
4 1
3 2
4 3
3 4
4 5
3 6
What I want to do is to select the highest level value in every indicator.
Is there any sql query that I can use that will generate a result like this?
Level , Indicator
4 1
4 3
4 5
If not, can you help me out using php and mysqli? Thank you so much.
To get Indicators having just highest level value -
select distinct Indicator, level
from your_table
where level = (select max(level) from your_table)
Also, you can use group by to get highest level for each Indicator value -
select Indicator, max(Level) from your_table group by Indicator

Order by before group by in subquery working in mysql 5.5 but not in mysql 5.7 [duplicate]

This question already has answers here:
Error related to only_full_group_by when executing a query in MySql
(18 answers)
Closed 6 years ago.
I have a problem when use Order By and Group By in a query string.
There is a table careers that contains data as shown below:
id se_id enrollment_start
-------------------------
1 1 2005-07-01
2 2 2008-10-12
3 2 2006-05-09
4 1 2016-11-10
5 3 2015-02-04
6 3 2010-08-11
I want to get se_id which has highest enrollment_start.
This is a sql statement I used. It works in mysql 5.5 but not in mysql 5.7:
SELECT
tmp.*
FROM
(SELECT
*
FROM
careers
ORDER BY enrollment_start DESC) tmp
GROUP BY tmp.se_id
This is groupwise maximum problem, and there are many topics cover about it. But I don't want the answer for that problem, I want to know why above statement woking in mysql 5.5 but it doesn't work in mysql 5.7 and is there any method to fix it? Thank you.
If you want to get se_id which has highest enrollment_start. use LIMIT:
SELECT *
FROM careers
ORDER BY enrollment_start DESC
LIMIT 1