2017-02-22 63 views
1

我有以下的查詢和數據:如何梳理彙總

select * from (
    select provider, title, sum(customer_price) revenue from `100` 
    group by provider, title 
    with rollup 
) x 
where provider is not null 

這給了我:

enter image description here

我想通過以下標準對它進行排序:

  • 首先,由提供者根據其收入
  • 其次,標題ASC

所以,結果應該是,然後:

provider    title     revenue 
Electric Entertainment NULL     41.95 
Electric Entertainment Leverage, Season 4  31.99 
Electric Entertainment The Cross My Heart Job 2.99 
Electric Entertainment The Inside Job   1.99 
Electric Entertainment The Radio Job   1.99 
Electric Entertainment The Scheherazade Job 2.99 
...etc... 

我將如何排序上面?

回答

1

我認爲這將工作:

select x.* 
from (select provider, title, sum(customer_price) revenue 
     from `100` 
     group by provider, title with rollup 
    ) x join 
    (select provider, sum(customer_price) as sum_customer_price 
     from `100` p2 
     group by provider 
    ) p2 
    on p2.provider = x.provider 
where p2.provider is not null 
order by p2.sum_customer_price desc, 
     x.provider, 
     title, 
     x.revenue desc;