1、應用說明
可以快速查詢出指標分布及最大最小指標情況。如:查詢XX市2009年12月份各縣銷量及銷量最大最小的縣和對應值。
SQL:
select t.indent_month,
t.dept_code,
t.dept_name,
sum(t.amount) Amount_SUM,
first_value(dept_name) over(order by sum(amount) desc rows unbounded preceding) Max_Dept,
first_value(sum(amount)) over(order by sum(amount) desc rows unbounded preceding) Max_Value,
first_value(dept_name) over(order by sum(amount) asc rows unbounded preceding) Min_DEPT,
first_value(sum(amount)) over(order by sum(amount) asc rows unbounded preceding) Min_Value
from mv_jy_dept_sale_cig_month t
where t.nd = 2009
and t.indent_month = 12
group by t.indent_month, t.dept_code, t.dept_name
輸出結果:
進一步深入:
上面的輸出結果可能大家覺得意義不太大,但是進一步深入,如果要查詢出2009年全年各月銷量最大和最小的縣公司及最大最小值。
SQL:
select a.indent_month,a.Max_Dept,a.Max_Value,a.Min_DEPT,a.Min_Value from(
select t.indent_month,
t.dept_code,
t.dept_name,
sum(t.amount) Amount_SUM,
first_value(dept_name) over(partition by indent_month order by sum(amount) desc rows unbounded preceding) Max_Dept,
first_value(sum(amount)) over(partition by indent_month order by sum(amount) desc rows unbounded preceding) Max_Value,
first_value(dept_name) over(partition by indent_month order by sum(amount) asc rows unbounded preceding) Min_DEPT,
first_value(sum(amount)) over(partition by indent_month order by sum(amount) asc rows unbounded preceding) Min_Value
from mv_jy_dept_sale_cig_month t
where t.nd = 2009
group by t.indent_month, t.dept_code, t.dept_name)a
group by a.indent_month,a.Max_Dept,a.Max_Value,a.Min_DEPT,a.Min_Value
order by a.indent_month
輸出結果: