CodePudding user response:
Can you put the table structure and the sample data to the part out? My train of thought is to use a subquery, first by the subquery, collect shipments in the shipping way, find the top five, and then the associated outside the original table, collect shipments, according to the shipping method and the country again and get to the top five countries; Pseudo code is as follows:Select
The delivery way
,
, the sum (shipments)
The from table1 a
Inner join (select
The delivery way
, the sum (shipments)
The from table1 a
Where the time between April and may
Group by shipping method
The order by the sum (shipments) desc
Limit 5) b
Way=b. delivery on a. delivery way
Way=b. delivery where a. delivery way
And where the time between April and may
Group by shipping method, the state
The order by the sum (shipments) desc
Is wrong, this method can take to each shipping method delivery of shipments of the order, but can't get to the top five countries
CodePudding user response:
Wanted to think, want to a the most stupid method, can't think of a better way, the others if there is a better way, still hope glad:Select
The delivery way
,
, shipments _sum
, the if (@ objno=a. countries,
@ rank:=@ rank + 1,
@ rank:=1) as row_number
, @ objno=a. countries
The from
(select
The delivery way
,
, the sum (shipments) as shipments _sum
The from table1 a
Inner join (select
The delivery way
, the sum (shipments)
The from table1 a
Where the time between April and may
Group by shipping method
The order by the sum (shipments) desc
Limit 5) b
Way=b. delivery on a. delivery way
Way=b. delivery where a. delivery way
And where the time between April and may
Group by shipping method, the state
The order by shipping method
, the state, the sum (shipments) desc
) a
, (@ objno:=NULL, @ rank:=0) b
Where row_number & lt;=5
Because mysql itself does not support such as oracl support over (partition by) function, so write up a little bit difficult
CodePudding user response: