If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > DISTINCT with MAX

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-18-07, 23:40
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
DISTINCT with MAX

Hi

I want to get the maximum Price for every Item. There can be many same Items with different price.

This works :
Code:
SELECT `Item`, `Price`
FROM `table1` t1
WHERE
`Price` = (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`)
ORDER BY `Price` DESC
LIMIT 0,10;
Though I was wondering if theres a way to get it done this way
Code:
SELECT `Item`, (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`) AS `Price`
FROM `table` t1
WHERE `Position` = '1 - 3'
ORDER BY Price DESC
LIMIT 0,10;
Problem is, this 2nd one returns duplicate Items - how do I get DISTINCT `Items` ?
I understand DINSTINCT a, b, c is equivalent to DINSTINCT a, DINSTINCT b, DINSTINCT c

Thanks
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 12-18-07, 23:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by anjanesh
Though I was wondering if theres a way to get it done this way
no, not that way
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-19-07, 01:40
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
SELECT DISTINCT `Item`,
(SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`) AS `MaxPrice`
FROM `table` t1
ORDER BY `MaxPrice` DESC
LIMIT 0,10;
did seem to return the same result set - except it took 4 times longer !
Price is of type float(5,2) I normally dont create an index on non-textual columns. Will creating an index on Price speed up the process ?

Also, (Item, Price) isnt unique.
So what if there are same Items with same Price ?
Code:
SELECT `Item`, `Price`
FROM `table1` t1
WHERE
`Price` = (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`)
ORDER BY `Price` DESC
LIMIT 0,10;
can return duplicate rows.
__________________
MySQL 5.1

Last edited by anjanesh; 12-19-07 at 02:46.
Reply With Quote
  #4 (permalink)  
Old 12-19-07, 08:19
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Take a look at GROUP BY
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 12-19-07, 08:34
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
I did. Im aware of the consequences when using GROUP BY for LARGE sets of data.
Is this equivalent to GROUP BY ?
Code:
SELECT DISTINCT `Item`, `Price`
FROM `table1` t1
WHERE
`Price` = (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`)
ORDER BY `Price` DESC
How is DISTINCT `Item`, `Price` interpreted as?
__________________
MySQL 5.1
Reply With Quote
  #6 (permalink)  
Old 12-19-07, 09:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if an item can have multiple prices, and you want only the maximum price for each item, abandon all hope of getting DISTINCT to provide your solution

DISTINCT item,price will provide one row for every item,price combination

GROUP BY is a good solution, but then you will get only the maximum price per item, and no other columns
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-19-07, 10:50
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
a,4
a,5
a,8
a,4
b,2
b,9
b,8
b,5
b,8
b,1
c,3
c,9
c,4
c,3
c,5
SELECT DISTINCT `Item`, `Price` should return
Code:
a,4
a,5
a,8
b,2
b,9
b,8
b,5
b,1
c,3
c,9
c,4
c,5
SELECT `Item`, `Price` ...
WHERE `Price` = (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`)
should return
Code:
a,8
b,8
b,8
c,9
Its the duplicate (b,8) pair that I want to rid off. Hence the addition of DISTINCT with MAX in where

SELECT DISTINCT `Item`, `Price` ...
WHERE `Price` = (SELECT MAX(`Price`) FROM `table1` WHERE `Item` = t1.`Item`)
should return
Code:
a,8
b,8
c,9
__________________
MySQL 5.1
Reply With Quote
  #8 (permalink)  
Old 12-19-07, 10:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
okay, you are right, you would use DISTINCT this way

the real problem here is why {b,8} occurs more than once
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-19-07, 11:01
ortho ortho is offline
Registered User
 
Join Date: Nov 2006
Location: Quebec
Posts: 172
Code:
SELECT `Item`, MAX(Price)
FROM table1
GROUP BY Item
Simple enough ? Think that's what you want!
__________________
Less is more.
How long is now?
http://www.lesouterrain.com
Reply With Quote
  #10 (permalink)  
Old 12-19-07, 11:18
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
True, except when an ORDER BY in included, its taking far more time.
Code:
SELECT `Item`, MAX(`Price`)
FROM `table1`
GROUP BY `Item`
ORDER BY `Price` DESC
__________________
MySQL 5.1
Reply With Quote
  #11 (permalink)  
Old 12-19-07, 11:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you should always only ever ORDER BY one of the columns in the SELECT

SELECT `Item`, MAX(`Price`) as maxprice
FROM `table1`
GROUP BY `Item`
ORDER BY maxprice DESC
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 12-19-07, 11:24
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Sorry - thats what I meant - forgot to add that here.
Code:
SELECT `Item`, MAX(`Price`)
FROM `table1`
GROUP BY `Item`
ORDER BY MAX(`Price`) DESC
__________________
MySQL 5.1
Reply With Quote
  #13 (permalink)  
Old 12-19-07, 11:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
what indexes have you defined?

please do a SHOW CREATE TABLE for your table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 12-19-07, 14:27
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
CREATE TABLE `test` (
`ID` bigint(20) NOT NULL auto_increment,
`ShopID` bigint(20) NOT NULL,
`Item` varchar(255) NOT NULL,
`Price` float(5,2) NOT NULL,
`Position` varchar(10) NOT NULL,
PRIMARY KEY  (`ID`),
UNIQUE KEY `ShopID` (`ShopID`,`Item`),
KEY `Item` (`Item`(100)),
KEY `Price` (`Price`)
ENGINE=MyISAM AUTO_INCREMENT=5001 DEFAULT CHARSET=latin1
It may be worth mentioning that I had a Position = 'some-text' in my WHERE clause of my queries. Just didnt mention it here.
__________________
MySQL 5.1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On