Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a sql query

 
Priyakant Charokar
Ranch Hand
Posts: 58
Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table:

+--------------+---------+----------+
| product_name | sale_id | quantity |
+--------------+---------+----------+
| IPhone | 4 | 10 |
| IPhone | 10 | 20 |
| IPhone | 5 | 15 |
| IPhone | 11 | 20 |
| IPhone | 6 | 20 |
| Moto | 3 | 8 |
| Moto | 1 | 25 |
| Moto | 2 | 16 |
| Samsung | 8 | 18 |
| Samsung | 9 | 20 |
| Samsung | 7 | 20 |
+--------------+---------+----------+



Want the maximum item sale .... it should return

+--------------+---------+----------+
| product_name | sale_id | quantity |
+--------------+---------+----------+
| IPhone | 4 | 10 |
| IPhone | 10 | 20 |
| IPhone | 5 | 15 |
| IPhone | 11 | 20 |
| IPhone | 6 | 20 |
+--------------+---------+----------+

Please help ... with the query
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to define how you want to group your required output, and what constitutes the "maximum" value for each group. It's not at all clear how you get from your inputs to your expected outputs in your example.

You also need to read about using aggregate functions (like MAX) and GROUP BY in SQL.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic