Do not use analytic functions to find the highest monthly sales models

Each line is calculated monthly vehicle sales before 3 does not use analytic functions

create view  test_sale  as
select '2019-01' as order_date,'A' as brand_id ,10 as cnt from dual union all 
select '2019-01' as order_date,'B' as brand_id ,20 as cnt from dual union all 
select '2019-01' as order_date,'C' as brand_id ,30 as cnt from dual union all 
select '2019-01' as order_date,'D' as brand_id ,15 as cnt from dual union all 
select '2019-01' as order_date,'E' as brand_id ,22 as cnt from dual union all 
select '2019-02' as order_date,'A' as brand_id ,10 as cnt from dual union all 
select '2019-02' as order_date,'B' as brand_id ,30 as cnt from dual union all 
select '2019-02' as order_date,'C' as brand_id ,30 as cnt from dual union all 
select '2019-02' as order_date,'D' as brand_id ,15 as cnt from dual union all 
select '2019-02' as order_date,'E' as brand_id ,22 as cnt from dual ;

  

Since the association, the association use the month, association, match the current monthly sales of this model is larger than the sales data.
After the summary, the number of rows least, is the most sales.

SELECT A.ORDER_DATE, A.BRAND_ID, A.CNT, COUNT(*) AS RN
  FROM TEST_SALE A
  JOIN TEST_SALE B
    ON A.ORDER_DATE = B.ORDER_DATE
   AND A.CNT <= B.CNT
 GROUP BY A.ORDER_DATE, A.BRAND_ID, A.CNT;

  

 

Guess you like

Origin www.cnblogs.com/fooobabar/p/11271678.html