2017-07-27 59 views
-2

所以,我需要一些幫助做些功課。SQL Server - 窗口函數

下面是我需要使用的數據庫部分的模式。 Table Scema

我敢肯定,它有一些冗餘(我希望任何與安裝有關的幫助)。不管怎麼說,這裏是分配:

  • 銷售經理現在想你創建一個由他們的總銷售額和總銷售數量都位居她的產品的報告(每個 將自己列)。創建一個存儲過程,返回
    以下列,但也添加了兩個新的等級列。

    產品名稱|訂單計數|總銷售價值|總銷售數量

  • CEO希望根據上個月的銷售情況瞭解銷售情況。請向她提供一個查詢,以便在上個月根據訂單數量 對產品進行排名。應該沒有跳過的數字。

  • 創建生成客戶編號,訂單日期,等***訂單列表的SELECT語句中使用的行數函數 產生由訂單日期排序倒序每行一個唯一的ID

+0

我會說車包含一個訂單。一個訂單包含OrderItems。一個OrderItem有一個ProductId。所以一個購物車要很多訂單。一個訂單到許多訂單項。一個訂單項到一個產品。我沒有看到將購物車鏈接到訂購產品或產品的任何價值。 –

+2

這是你的家庭作業,你希望其他人爲你做?到目前爲止你做了什麼? – Eric

+1

你是否提供這個數據庫教授或者你創建了它?我問,因爲該數據庫有很多問題。我創造了肖恩, –

回答

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