1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| -- 方法一 with tmp as ( select C.year, C.quat, A.orderid, round(sum(B.amount), 2) amount from sale A join saledetail B on A.orderid=B.orderid join dimdate C on A.dt=C.dt group by C.year, C.quat, A.orderid ) select year, quat, orderid, amount, rank from ( select year, quat, orderid, amount, dense_rank() over (partition by year, quat order by amount desc) rank from tmp ) tmp1 where rank <= 10;
-- 方法二 with tmp as( select year(A.dt) year, case when month(A.dt) <= 3 then 1 when month(A.dt) <= 6 then 2 when month(A.dt) <= 9 then 3 else 4 end quat, A.orderid, round(sum(B.amount), 2) amount from sale A join saledetail B on A.orderid = B.orderid group by year(A.dt), case when month(A.dt) <= 3 then 1 when month(A.dt) <= 6 then 2 when month(A.dt) <= 9 then 3 else 4 end, A.orderid ) select year, quat, orderid, amount, rank from ( select year, quat, orderid, amount, dense_rank() over (partition by year, quat order by amount desc) rank from tmp ) tmp1 where rank <= 10;
-- 方法三。求季度 select floor(month(dt/3.1)) + 1;
with tmp as ( select year(A.dt) year, floor(month(A.dt)/3.1) + 1 quat, A.orderid, round(sum(B.amount), 2) amount from sale A join saledetail B on A.orderid=B.orderid group by year(A.dt), floor(month(A.dt)/3.1) + 1, A.orderid ) select year, quat, orderid, amount, rank from ( select year, quat, orderid, amount, dense_rank() over (partition by year, quat order by amount desc) rank from tmp ) tmp1 where rank <= 10;
|