Oracle分析函数、多维函数和Model函数简要说明,主要针对BI报表(2)

来源:未知 责任编辑:责任编辑 发表时间:2014-02-18 03:27 点击:

            WHEN AreaSEQ=4 THEN 华东地区
            ELSE 其他地区
       END
  FROM (SELECT SalesNumber,ROW_NUMBER() OVER(ORDER BY ROWID) DateSEQ,ROUND(dbms_random.VALUE(1,5)) AreaSEQ FROM ComputerSales) A;
DROP TABLE ComputerSales;
RENAME ComputerSalesBAK TO ComputerSales;
 

--移动平均值,累计求和,当前窗口平均值,当前窗口求和,以及窗口函数和排序函数的作用域
SELECT
  Area,SalesDate,SalesNumber,
  MIN(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS min_Area_SalesDate,
  MAX(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS max_Area_SalesDate,
  AVG(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS avg_Area_SalesDate,  
  SUM(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS sum_Area_SalesDate,  
  COUNT(*) OVER (PARTITION BY Area ORDER BY SalesDate) AS count_Area,
  MIN(SalesNumber) OVER (PARTITION BY Area) AS min_Area,
  MAX(SalesNumber) OVER (PARTITION BY Area) AS max_Area,
  AVG(SalesNumber) OVER (PARTITION BY Area) AS avg_Area,  
  SUM(SalesNumber) OVER (PARTITION BY Area) AS sum_Area,  
  COUNT(*) OVER (PARTITION BY Area) AS count_Area 
FROM ComputerSales

--观察Rank、Dense_Rank,Row_number,Count的区别
--Rank跳号,Dense_Rank不跳号,Row_number唯一,Count按统计数计也跳号
--如果PARTITION BY和order by 的字段是唯一的话,则这四个函数没什么区别
SELECT
  Area,SalesDate,SalesNumber,
  RANK() OVER (PARTITION BY Area order by SalesNumber) AS Rank_Area_SalesNumber,
  DENSE_RANK() OVER (PARTITION BY Area order by SalesNumber) AS DenseRank_Area_SalesNumber,
  ROW_NUMBER() OVER (PARTITION BY Area order by SalesNumber) AS Rownumber_Area_SalesNumber,
  COUNT(*) OVER (PARTITION BY Area order by SalesNumber) AS CountAll_Area_SalesNumber,
  COUNT(SalesNumber) OVER (PARTITION BY Area order by SalesNumber) AS Count_Area_SalesNumber
FROM ComputerSales

--观察Lag和Lead的异同,以及Lag参数之间的异同
--缺省情况下Lag取前一行的值,Lead取后一行的值
--Lag、lead的第一个参数决定了取行的位置,第二个参数为取不到值时的缺省值
SELECT
  Area,SalesDate,SalesNumber,
  LAG(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS Lag_Area_SalesNumber, 
  LEAD(SalesNumber) OVER (PARTITION BY Area order by SalesDate) AS Lead_Area_SalesNumber,   
  LAG(SalesNumber,1) OVER (PARTITION BY Area order by SalesDate) AS Lag1_Area_SalesNumber,
  LAG(SalesNumber,2) OVER (PARTITION BY Area order by SalesDate) AS Lag2_Area_SalesNumber,
  LEAD(SalesNumber,1) OVER (PARTITION BY Area order by SalesDate) AS Lead1_Area_SalesNumber,
  LEAD(SalesNumber,2) OVER (PARTITION BY Area order by SalesDate) AS Lead2_Area_SalesNumber,
  LAG(SalesNumber,1,0) OVER (PARTITION BY Area order by SalesDate) AS Lag10_Area_SalesNumber,

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Table函数使用简介
  • Oracle数据库Constraint约束的常用操作及异常处理
  • Bulk Collect性能分析(zz)
  • export/import的使用
  • OCP043第十五讲 Database Security
  • ORACLE10gr2数据导入MySQL方案
  • oracle 让sys用户可以使用isqlplus
  • 在oracle数据库下使用iSQL*Plus DBA访问数据库
  • Oracle行列转换小结
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1