user3613119
user3613119

Reputation: 91

Group by two columns is possible?

I have this table:

ID       Price   Time
0        20,00    20/10/10
1        20,00    20/10/10
2        20,00    12/12/10
3        14,00    23/01/12
4        87,00    30/07/14
4        20,00    30/07/14

I use this syntax sql to get the list of all prices in a way that does not get repeated values:

SELECT * FROM myTable WHERE id in (select min(id) from %@ group by Price)

This code return me the values (20,14,87,20)

But in this case I would implement another check, that will not only sort by price but also by date, example: That syntax is getting the list by price, if I find a way to check by date, the code will return me the values (20,20,14,87,20)

He repeats 20 two times but if we see in the table we have three numbers 20 (two with the date 20/10/10 and one with the date 12/12/10) and is exactly what I'm wanting to get!

Somebody could help me?

Upvotes: 0

Views: 46

Answers (2)

Vulcronos
Vulcronos

Reputation: 3456

To group by multiple columns, just put a comma in between the list.

SELECT price FROM myTable group by price, time order by time

The group by looks at all distinct combinations of the listed columns values, and discards duplicates. You can also use aggregate functions like sum or max to pull in additional columns to the results.

Upvotes: 2

Mr. Llama
Mr. Llama

Reputation: 20899

The following should work as long as all you need is the price/time combination. If you need to include the ID, things get more complicated:

SELECT `Price` FROM items
GROUP BY `Price`, `Time`
ORDER BY `Time`;

Here's a fiddle with the result in action: http://sqlfiddle.com/#!2/40821/1

Upvotes: 1

Related Questions