472,958 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

find all products with maximum version

Hi.

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?

And which solutions are MySql specific, and which are ANSISql (for
maximum portability I want to be as close as possible to ANSISql)?

And are there other solutions, even including a new datamodel design?
# works in MySql
# fails in SQLAnywhere (but change int(1) to int, remove unique index
and it works)
create table product (
id int(1) unsigned not null auto_increment,
name varchar(50) not null,
version smallint(1) not null,
primary key (id),
unique index theproduct (name, version)
);

# fails in MySql as syntax error
# fails in SQLAnywhere as subquery only allowed on one column
select p.id, p.name, p.version
from product p
where row(p.name, p.version) = (
select p2.name, max(p2.version)
from product p2
group by p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p
where p.version = (
select max(p2.version)
from product p2
where p.name = p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p,
(select p2.name, max(p2.version) as version from product p2 group
by p2.name) as pp
where p.name = pp.name and p.version = pp.version
Thanks.
Jul 20 '05 #1
8 2917

"Siemel Naran" <na*******@excite.com> wrote in message
news:3d**************************@posting.google.c om...
Hi.

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?


Wouldn't the following work?

select name,max(version) from product group by name;
or
select id,name,max(version) from product group by name;
Regards,

Mike Chirico
Jul 20 '05 #2

"Siemel Naran" <na*******@excite.com> wrote in message
news:3d**************************@posting.google.c om...
Hi.

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?


Wouldn't the following work?

select name,max(version) from product group by name;
or
select id,name,max(version) from product group by name;
Regards,

Mike Chirico
Jul 20 '05 #3
"Mike Chirico" <mc******@comcast.net> wrote in message
news:J6qdnX5fUa_LQxTdRVn-
"Siemel Naran" <na*******@excite.com> wrote in message

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?


Wouldn't the following work?

select name,max(version) from product group by name;
or
select id,name,max(version) from product group by name;


The first works if you want to find just the product name and maximum
version.

The second does not work in MySql. The problem is that select id selects
the id of the the first record with name X. Thus if my table is

id name version
1 X 1
2 X 2
3 Y 1
4 X 3

The select id, name, max(version) group by name does the following in MySql
4.0.18

id name version
1 X 3
3 Y 1

Note that the first row for product name "X" picks id=1 but should pick
id=4.

That's why the more complex solutions in my post.

I've added mailing.database.sql-general back to the newsgroup list because
your solution may be correct, just that MySql is not doing it right. But my
ATT news server does not this newsgroup, so I can't crosspost from this
account.

So more generally, in

select X, Y, max(Z)
from ...
group by X

will the selected Y be the Y of any record that has X equal to the grouped
by value? Or will Y be the Y of the record that has the max(Z) in this
group? But then how would the SQL engine deal with avg(Z) or sum(Z); which
Y would it select?

It could also be a syntax error because in ANSISql select columns must
appear in the group by clause, or be used in statistical functions like max,
sum, etc. Though not fully sure about this statement.
Jul 20 '05 #4
"Mike Chirico" <mc******@comcast.net> wrote in message
news:J6qdnX5fUa_LQxTdRVn-
"Siemel Naran" <na*******@excite.com> wrote in message

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?


Wouldn't the following work?

select name,max(version) from product group by name;
or
select id,name,max(version) from product group by name;


The first works if you want to find just the product name and maximum
version.

The second does not work in MySql. The problem is that select id selects
the id of the the first record with name X. Thus if my table is

id name version
1 X 1
2 X 2
3 Y 1
4 X 3

The select id, name, max(version) group by name does the following in MySql
4.0.18

id name version
1 X 3
3 Y 1

Note that the first row for product name "X" picks id=1 but should pick
id=4.

That's why the more complex solutions in my post.

I've added mailing.database.sql-general back to the newsgroup list because
your solution may be correct, just that MySql is not doing it right. But my
ATT news server does not this newsgroup, so I can't crosspost from this
account.

So more generally, in

select X, Y, max(Z)
from ...
group by X

will the selected Y be the Y of any record that has X equal to the grouped
by value? Or will Y be the Y of the record that has the max(Z) in this
group? But then how would the SQL engine deal with avg(Z) or sum(Z); which
Y would it select?

It could also be a syntax error because in ANSISql select columns must
appear in the group by clause, or be used in statistical functions like max,
sum, etc. Though not fully sure about this statement.
Jul 20 '05 #5
na*******@excite.com (Siemel Naran) wrote in message news:<3d**************************@posting.google. com>...
Hi.

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?

And which solutions are MySql specific, and which are ANSISql (for
maximum portability I want to be as close as possible to ANSISql)?

And are there other solutions, even including a new datamodel design?
# works in MySql
# fails in SQLAnywhere (but change int(1) to int, remove unique index
and it works)
create table product (
id int(1) unsigned not null auto_increment,
name varchar(50) not null,
version smallint(1) not null,
primary key (id),
unique index theproduct (name, version)
);

# fails in MySql as syntax error
# fails in SQLAnywhere as subquery only allowed on one column
select p.id, p.name, p.version
from product p
where row(p.name, p.version) = (
select p2.name, max(p2.version)
from product p2
group by p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p
where p.version = (
select max(p2.version)
from product p2
where p.name = p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p,
(select p2.name, max(p2.version) as version from product p2 group
by p2.name) as pp
where p.name = pp.name and p.version = pp.version
Thanks.


Why select the id column as this is causing what looks like an
unnecessary problem If you do not need the id column then

SELECT name, max(version)
FROM product
GROUP BY name

would then work
Hope this helps

Duncan
Jul 20 '05 #6
na*******@excite.com (Siemel Naran) wrote in message news:<3d**************************@posting.google. com>...
Hi.

I have a table of products and versions, and these 2 columns are the
unique key.

My challenge is to write a SQL statement to find the latest version of
each product. My solutions below seem correct, yet MySql complains
that the syntax is incorrect. How to fix?

And which solutions are MySql specific, and which are ANSISql (for
maximum portability I want to be as close as possible to ANSISql)?

And are there other solutions, even including a new datamodel design?
# works in MySql
# fails in SQLAnywhere (but change int(1) to int, remove unique index
and it works)
create table product (
id int(1) unsigned not null auto_increment,
name varchar(50) not null,
version smallint(1) not null,
primary key (id),
unique index theproduct (name, version)
);

# fails in MySql as syntax error
# fails in SQLAnywhere as subquery only allowed on one column
select p.id, p.name, p.version
from product p
where row(p.name, p.version) = (
select p2.name, max(p2.version)
from product p2
group by p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p
where p.version = (
select max(p2.version)
from product p2
where p.name = p2.name
);

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p,
(select p2.name, max(p2.version) as version from product p2 group
by p2.name) as pp
where p.name = pp.name and p.version = pp.version
Thanks.


Why select the id column as this is causing what looks like an
unnecessary problem If you do not need the id column then

SELECT name, max(version)
FROM product
GROUP BY name

would then work
Hope this helps

Duncan
Jul 20 '05 #7
"Siemel Naran" <na*******@excite.com> wrote in message
news:3d**************************@posting.google.c om...
# fails in MySql as syntax error
# fails in SQLAnywhere as subquery only allowed on one column
select p.id, p.name, p.version
from product p
where row(p.name, p.version) = (
select p2.name, max(p2.version)
from product p2
group by p2.name
);
Still fails in 4.1. Row constructors must not be supported in this version.

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p
where p.version = (
select max(p2.version)
from product p2
where p.name = p2.name
);
Works in 4.1.

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p,
(select p2.name, max(p2.version) as version from product p2 group
by p2.name) as pp
where p.name = pp.name and p.version = pp.version


Works in 4.1.
Jul 20 '05 #8
"Siemel Naran" <na*******@excite.com> wrote in message
news:3d**************************@posting.google.c om...
# fails in MySql as syntax error
# fails in SQLAnywhere as subquery only allowed on one column
select p.id, p.name, p.version
from product p
where row(p.name, p.version) = (
select p2.name, max(p2.version)
from product p2
group by p2.name
);
Still fails in 4.1. Row constructors must not be supported in this version.

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p
where p.version = (
select max(p2.version)
from product p2
where p.name = p2.name
);
Works in 4.1.

# fails in MySql as syntax error
# works in SQLAnywhere
select p.id, p.name, p.version
from product p,
(select p2.name, max(p2.version) as version from product p2 group
by p2.name) as pp
where p.name = pp.name and p.version = pp.version


Works in 4.1.
Jul 20 '05 #9

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

Similar topics

2
by: Raed Sawalha | last post by:
I have following XML: How can I find the depth of XML? in other word max level of the XML <Root> ----- level 0 <Name>----- level 1 <FirstName>name</FirstName> ---- level 2...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
24
by: Rob R. Ainscough | last post by:
I was reading yet another book on .NET - VB 2005 Professional (wrox) and read the statement; "Microsoft has staked their future on .NET and publicly stated that henceforth almost all their...
5
by: Sunny123 | last post by:
hello i am trying to find the maximum value of a function and where it occur. i.e there is an array x |y ============= | |
2
by: mktselvan | last post by:
Hi, Existing running oracle application 11i (11.5.8) Database version is 8.1.7.4 There is any command / way to know the number of concurrent users for this application. ...
9
by: Satish Itty | last post by:
How do I write the following c# code in vb Product FindProduct(string code) { List<Productproducts = getProducts(); return products.Find(delegate(Product bo) { return bo.Code == code; }); }
4
by: Salad | last post by:
I have a situation where some, not all, users get the message "Couldn't find file "F:\AccessApps\AppName.mdw". This file is required for startup". My app the users are attempting to access is...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
7
by: laredotornado | last post by:
Hi, I'm using PHP 5 with MySql 5. I have a MySQL InnoDB table with a column of type INTEGER UNSIGNED. Is there a constant in PHP to insert the maximum value possible into the column? The...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.