I have a data-set, the columns sample information are like below:
Date ID Cost
05/01 1001 30
05/01 1024 19
05/01 1001 29
05/02 1001 28
05/02 1002 19
05/02 1008 16
05/03 1017 89
05/04 1003 28
05/04 1001 16
05/05 1017 28
05/06 1002 44
... etc...
And I want to create a table to display the top one payer(cost the most) on each day, which means there are only two columns in the table, and the output sample should be like this:
Date ID
05/01 1001
05/02 1001
05/03 1017
05/04 1003
...etc...
I know this question is simple, and my problem is that I want to simplify the queries.
My query:
select Date, ID
from (select Date, ID, max(SumCost)
from (select Date, ID, sum(cost) as SumCost
from table1
group by Date, ID) a
group by Date, ID) b;
It seems kind of stupid, and I want to optimize the queries. The point is that I want to only output the Date and the Id, these two columns.
Any suggestions?
SELECT DISTINCT Date, ID FROM table1is enough.