SQL中的视图和排名

时间:2013-10-23 02:25:42

标签: sql

Create VIEW TSalesView
AS 
SELECT TOP (3)
         z.Outlet_Id,
         z.Outlet_Name,
         z.Region_Id,
         z.Region_Name,
         z.Prod_Id,
         z.Prod_Name,
         z.Month,
         z.Month_No,
         z.Year,
         z.TSales,
         z.TSalesAmt,
         RANK() Over(PARTITION By z.Month,z.Outlet_Id
         Order By z.TSalesAmt desc) AS Rank 
         FROM(
         Select fs.Outlet_Id,
                o.Outlet_Name,
                fs.Region_Id,
                r.Region_Name,
                fs.Prod_Id,
                pd.Prod_Name,
                d.Month,
                d.Month_No,
                d.Year,
                SUM(fs.Quantity) AS TSales,
                SUM(fs.Quantity)*(pd.Prod_Price) AS TSalesAmt
                FROM FACT_SALE 
                INNER JOIN DIM_TIME d ON fs.TIME_ID=d.TIME_ID
                INNER JOIN DIM_PROD pd on pd.PROD_ID=fs.PROD_ID
                INNER JOIN DIM_OUTLET o ON o.OUTLET_ID=fs.OUTLET_ID
                INNER JOIN DIM_REG r ON r.REGION_ID=fs.REGION_ID

                GROUP BY 
                      fs.OUTLET_ID,
                      o.OUTLET_ID,
                      fs.REGION_ID,
                      r.REGION_NAME,
                      fs.PROD_ID,
                      d."MONTH",
                      d.MONTH_NO,
                      d."YEAR",
                      pd.PROD_NAME,
                      pd.PROD_PRICE) AS z

                      ORDER BY 
                      z.Outlet_Id asc,
                      z.Month_No asc,
                      z.Year asc

我在RANK()OVER函数中遇到错误 任何人都可以帮我纠正这个问题

0 个答案:

没有答案