473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to write query to select the Max(version) for each unique file_name record?

Hello All,

I am a MySQL newbie trying to write a query that selects file_name records
possessing the highest numbered version for that unique file_name. I show
sample data and two trial queries below. Logically I want to use
max(version) as a constraint in a Where Clause. However, the max() function
is not allowed directly in a where clause.

I have contemplated a second table to track the max version for each file
name. I would like to structure the data in an efficient manner for query
performance when the data set grows to many thousands of unique file_name
records with many hundreds of versions each.

Any guidance will be appreciated.

David Oberlitner
------------------------------------------------------------------------
----------------

Sample data:

mysql> select * from test;
+----+-----------+---------+
| id | file_name | version |
+----+-----------+---------+
| 1 | fone | 1 |
| 2 | ftwo | 1 |
| 3 | fone | 2 |
| 4 | fone | 3 |
| 5 | fthree | 1 |
| 6 | ffour | 1 |
| 7 | ftwo | 2 |
| 8 | ffour | 2 |
+----+-----------+---------+

The query below gets close in that it returns each file name and its
max(version), however it returns the ID field associated with the first
record instance of file_name and not the ID associated with the
max(version) instance of the file_name record.

mysql> select id, file_name, max(version) from test group by file_name;
+----+-----------+--------------+
| id | file_name | max(version) |
+----+-----------+--------------+
| 6 | ffour | 2 |
| 1 | fone | 3 |
| 5 | fthree | 1 |
| 2 | ftwo | 2 |
+----+-----------+--------------+
4 rows in set (0.02 sec)

The query below returns the empty set.

mysql> select id, file_name, max(version) from test group by file_name
having max(version);
Empty set (0.00 sec)

Additionally,
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 3773

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, I recently upgraded my machine to SuSE 8.1 and went about getting all the rpms I could get related to MySQL and install them. I did notice that unlike before, I have a "max" version of...
1
by: nbrcrunch | last post by:
We have a transaction record that, for the sake of brevity, I will use a simple paradigm to convey my need: A sales clerk sells several pairs of shoes over the period of a day, each & every day...
7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
0
by: TJS | last post by:
webmatrix msde dataset vb Does anyone have example of function to get max record id from dataset ?
0
by: ashdeepthi1234 | last post by:
Can anybdoy please tell me the difference between mysql max version andthe non-max version? Also lemme know how the max version is helpful if the installation of mysql on the clinet machine is to be...
7
by: khunohm | last post by:
I would like to know how to select each item of data from text file transfer to each column in excel file. source 1..159662 /organism="Candidatus...
4
by: John | last post by:
Hi I need to check if a record exists (or not) in a table for a particular id. Is there a quick way to do this (like ms access dlookup function or similar)? Thanks Regards
2
by: Bruce A. Julseth | last post by:
Is there a multi-query version of mysql_query? I have my sql in a text file and it works great from the command line (mysql -u me -p <my.sql). my.sql had multiple lines of sql. I would now like to...
2
by: Shivajirp | last post by:
I have table in which Shift no, Start time. End time. if start time of shift is 6pm and end time of shift is 6 am then I want to select record between start time and end time.I need query to selct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.