SQL Server - 窗口函数

时间:2017-07-27 17:20:31

标签: sql-server windowed

所以,我需要一些家庭作业的帮助。

下面是我需要使用的数据库部分的架构。 Table Scema

我确定其中有一些冗余(我也很感激设置方面的任何帮助)。无论如何,这是作业:

  
      
  • 销售经理现在希望您创建一份报告,根据其总销售额和总销售量对每个产品进行排名(每个   将是它自己的专栏)。创建一个返回
    的存储过程   以下列,但也添加了两个新的排名列。

         

    产品名称|订单数量|总销售额|总销售数量

  •   
  • 首席执行官希望根据上个月的销售情况了解销售情况。请向她提供一个查询,按产品数量对产品进行排名   上个月的订单。应该没有SKIPPED NUMBERS。

  •   
  • 创建一个SELECT语句,生成一个包含客户ID,订单日期等的订单列表。***使用行号功能   为按订单日期DESC

  • 排序的每一行生成唯一ID   

1 个答案:

答案 0 :(得分:1)

试试吧

CREATE PROCEDURE report_salesmanager 
AS 
  WITH cte_main AS 
  ( 
             SELECT     p.NAME, 
                        Isnull(Count(DISTINCT o.orderid), 0)  corderid, 
                        Sum(Isnull(o.ordertotal, 0))          sordertotal, 
                        Sum (Isnull(oi.orderitemquantity, 0)) sorderitemquantity 
             FROM       product p 
             LEFT JOIN  orderitem oi 
             INNER JOIN orders o 
             ON         o.orderid - oi.orderid 
             ON         p.productid = oi.productid 
             GROUP BY   p.NAME), cte_rank_value AS 
  ( 
           SELECT   NAME, 
                    Rank() OVER( ORDER BY sordertotal DESC) rk 
           FROM     cte_main), cte_rank_qtd AS 
  ( 
           SELECT   NAME, 
                    Rank() OVER( ORDER BY sorderitemquantity DESC) rk 
           FROM     cte_main) 
  SELECT     a.NAME [Product Name], 
             a.corderid, 
             [Orders Count] a.sordertotal [Total Sales Value], 
             b.rk [Rank Total Sales Value], 
             a.sorderitemquantity [Total Sales Quantity], 
             c.rk [Rank Total Sales Quantity] 
  FROM       cte_main a 
  INNER JOIN cte_rank_value b 
  ON         a.NAME = b.NAME 
  INNER JOIN cte_rank_qtd c 
  ON         a.NAME = c.NAME

GO

CREATE PROCEDURE report_ceo 
AS 
  DECLARE @somedateLastMonth DATE = Dateadd(-1,m,Getdate()) WITH cte_main AS 
  ( 
             SELECT     p.NAME, 
                        Isnull(Count(DISTINCT o.orderid), 0)  corderid, 
                        Sum(Isnull(o.ordertotal, 0))          sordertotal, 
                        Sum (Isnull(oi.orderitemquantity, 0)) sorderitemquantity 
             FROM       product p 
             INNER JOIN orderitem oi 
             INNER JOIN orders o 
             ON         o.orderid - oi.orderid 
             ON         p.productid = oi.productid 
             WHERE      Year(o.orderdate) * 100 + Month(o.orderdate) = Year(@somedateLastMonth) * 100 + Month(@somedateLastMonth)
             GROUP BY   p.NAME), 
    cte_rank_value AS 
  ( 
           SELECT   NAME, 
                    Rank() OVER( ORDER BY sordertotal DESC) rk 
           FROM     cte_main), 
    cte_rank_qtd AS 
  ( 
           SELECT   NAME, 
                    Rank() OVER( ORDER BY sorderitemquantity DESC) rk 
           FROM     cte_main) 
  SELECT     a.NAME [Product Name], 
             a.corderid, 
             [Orders Count] a.sordertotal [Total Sales Value], 
             b.rk [Rank Total Sales Value], 
             a.sorderitemquantity [Total Sales Quantity], 
             c.rk [Rank Total Sales Quantity] 
  FROM       cte_main a 
  INNER JOIN cte_rank_value b 
  ON         a.NAME = b.NAME 
  INNER JOIN cte_rank_qtd c 
  ON         a.NAME = c.NAME
go


SELECT     Row_number() OVER (ORDER BY o.orderdate DESC, o.ordertime DESC) rowNumber ,
           * 
FROM       product p 
INNER JOIN orderitem oi 
INNER JOIN orders o 
ON         o.orderid - oi.orderid 
ON         p.productid = oi.productid 
INNER JOIN cart c 
ON         c.orderid = o.orderid