How to check the various versions of the first three trading volume in the stock trading data? (MySQL grouping ranking)

SQL query, grouped according to column A, column B for the group to perform statistical functions, is a common and important features, such as
select T.a, max(T.b) from T order by T.a asc, T.b desc group by T.a
select T.a, count(T.b) from T order by T.a asc, T.b desc group by T.a
has been written SQL, until one day, a friend stock market is doing (non-IT personnel) and I were talking , he said he used Python whole point of data, to analyze each section of the volume of stocks at the first three, asked me if I know how to use SQL to check out (which is a grouping rankings take a number of heads of demand). Honestly, this is a common problem, but a sudden my mind a bit ignorant forced back on the road has been thinking about this problem, when some years ago with SQLServer, there is a grouping method ROW_NUMBER () OVER (), seems to be able to achieve this grouping ranking function.

Syntax: row_number () over (partition by grouping columns sort order by column desc)

And is the Internet search MySQL implementation, and she recorded a bunch of friends over. Referring to an article CSDN micro-Bo " Mysql achieve ROW_NUMBER () OVER () content," the implementation process will be recorded as follows.

Look under the table structure:
view_market_trade_amt has three columns, the first column is the index, the second column is the stock code, the third column is the volume.
Demand is the index of each sector of the stock traded in the first three check out.
How to check the various versions of the first three trading volume in the stock trading data?  (MySQL grouping ranking)

Before implementing MySQL rownum () over (), need to understand the idea of ​​this implementation is: according to the grouping columns sorted, labeled define rownum, rownum mark changes with changes in the grouping columns, the same grouping column values, without rownum becomes the last column of each group form a sequence rownum, rownum then ranked based screening sequence.

  1. Defined rownum
select  v.stockCode,v.tradeIndex,v.amount,
if(@grpvar=v.tradeIndex,@rownum:=@rownum+1,@rownum:=1) as rownum, @grpvar:=v.tradeIndex
from (select stockCode,tradeIndex,amount from view_market_trade_amt order by tradeIndex asc ,amount desc) v ,
(select @grpvar := null ,@rownum:=0)  vardef

vardef definition packet is variable and rownum variables are assigned a default null and 0;
grpvar assignment tradexIndex, while judging the current record variable is equal tradeIndex and, if they are equal rownum + 1, or start from an assignment.
search result:
How to check the various versions of the first three trading volume in the stock trading data?  (MySQL grouping ranking)

How to check the various versions of the first three trading volume in the stock trading data?  (MySQL grouping ranking)

Can be seen, different tradeIndex, corresponding to the sequence rownum are independent.

  1. According to filter records rownum
    this step is more simple, based on the above continue screened to less than 3 rownum.
select tradeIndex, stockCode, amount from (

select  v.stockCode,v.tradeIndex,v.amount,
if(@grpvar=v.tradeIndex,@rownum:=@rownum+1,@rownum:=1) as rownum, @grpvar:=v.tradeIndex
from (select stockCode,tradeIndex,amount from view_market_trade_amt order by tradeIndex asc ,amount desc) v ,
(select @grpvar := null ,@rownum:=0)  vardef

)  as result
where result.rownum<=3

How to check the various versions of the first three trading volume in the stock trading data?  (MySQL grouping ranking)

Guess you like

Origin blog.51cto.com/10705830/2466834