Hi all,
I have been around sql quite some time now, but still I need help with little things.
Consider the following table [tt]sales[/tt]
[tt]
+-------------+--------+
| customer_id | amount |
+-------------+--------+
| 1 | 1000 |
| 2 | 10 |
| 2 | 2000 |
| 1 | 20 |
+-------------+--------+
[/tt]
I want to select all rows ordered by the maximum amount per customer_id:
[tt]
+-------------+--------+
| customer_id | amount |
+-------------+--------+
| 1 | 1000 |
| 1 | 20 |
| 2 | 2000 |
| 2 | 10 |
+-------------+--------+
[/tt]
The only idea I had was
[tt]select customer_id, max(amount) from sales group by customer_id;[/tt]
giving of course
[tt]
+-------------+------+
| customer_id | max |
+-------------+------+
| 1 | 1000 |
| 2 | 2000 |
+-------------+------+
[/tt]
due to the group.
How can I proceed?
Thank you in Advance, Jan
I have been around sql quite some time now, but still I need help with little things.
Consider the following table [tt]sales[/tt]
[tt]
+-------------+--------+
| customer_id | amount |
+-------------+--------+
| 1 | 1000 |
| 2 | 10 |
| 2 | 2000 |
| 1 | 20 |
+-------------+--------+
[/tt]
I want to select all rows ordered by the maximum amount per customer_id:
[tt]
+-------------+--------+
| customer_id | amount |
+-------------+--------+
| 1 | 1000 |
| 1 | 20 |
| 2 | 2000 |
| 2 | 10 |
+-------------+--------+
[/tt]
The only idea I had was
[tt]select customer_id, max(amount) from sales group by customer_id;[/tt]
giving of course
[tt]
+-------------+------+
| customer_id | max |
+-------------+------+
| 1 | 1000 |
| 2 | 2000 |
+-------------+------+
[/tt]
due to the group.
How can I proceed?
Thank you in Advance, Jan