試試吧
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
我會說車包含一個訂單。一個訂單包含OrderItems。一個OrderItem有一個ProductId。所以一個購物車要很多訂單。一個訂單到許多訂單項。一個訂單項到一個產品。我沒有看到將購物車鏈接到訂購產品或產品的任何價值。 –
這是你的家庭作業,你希望其他人爲你做?到目前爲止你做了什麼? – Eric
你是否提供這個數據庫教授或者你創建了它?我問,因爲該數據庫有很多問題。我創造了肖恩, –