Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2003, 04:38 PM
mnwt mnwt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 mnwt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Query Help - Max, Group by

I am trying to write a what seems to be a simple query but am stuck. any help will be greatly appriciated.
the problem is
i have a single table having 4 fields
1. - ID
2. - User
3. - Product Name
4. - Quantity

what i want is to display the max Product ordered by each user so if the table looks like this

ID User Product Quantity
1 u1 p1 1
2 u1 p2 2
3 u1 p3 3
4 u2 p3 1
5 u2 p4 2
6 u2 p5 3

i want my results to look like

u1 p3
u2 p5

display the user and product that is ordered most by each user

thanks once again for helping me out.
mnwt

Reply With Quote
  #2  
Old December 7th, 2003, 04:53 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h
Reputation Power: 37
Code:
select * from t as q
where quantity =
(select max(quantity) from t
where t.productName = q.productName)

Reply With Quote
  #3  
Old December 7th, 2003, 05:36 PM
mnwt mnwt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 mnwt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for looking but that does not work - it gives me back the entire table.
thanks
mnwt

Reply With Quote
  #4  
Old December 7th, 2003, 06:46 PM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 993 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 44 sec
Reputation Power: 66
Code:
SELECT u1.username,
       u1.product,
       u1.quantity
FROM userprod u1
WHERE u1.quantity = (SELECT MAX (quantity) FROM userprod u2 WHERE u2.username = u1.username)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Query Help - Max, Group by


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway