存储过程:即使订单日期不同,三列中的值也相同

发布于 2024-10-09 11:55:23 字数 3208 浏览 6 评论 0原文

我在这里列出了一个存储过程问题::

例如客户 A 有: i.) 前 30 天内有 5 个订单, ii.) 前 60 天内有 5 个订单, iii.) 前 90 天内有 5 个订单

因此结果应该是:

     A.) First 30 Days: Five orders
     B.) First 60 Days: Five + Five = 10 Orders
     C.) First 90 Days: Five + Five + Five = 15 Orders

但是,我的存储过程在所有 30,60 和 90 天内显示“15 个订单”。您能帮助我吗,以便我的存储过程在 30 天内显示“5 个订单”,“60 天内有 10 个订单”,在 90 天内显示“15 个订单”。 [以下是代码的一部分] 感谢您的帮助。


 IF (@CUSTOMERID IS NOT NULL)
 BEGIN

CREATE TABLE #TEMP_D
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID



CREATE TABLE #TEMP_E
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID


CREATE TABLE #TEMP_F
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID


SELECT D.CUSTOMERID, D.ORDERID AS 'ORDERS 30 DAYD',D.PRODUCTID AS 'PRODUCTS 30 DAYS',D.QUANTITY, E.CUSTOMERID, E.ORDERID AS 'ORDERS 60                 
   DAYS',E.PRODUCTID AS 'PRODUCTS 60 DAYS',E.QUANTITY,  F.CUSTOMERID, F.ORDERID AS 'ORDERS 90 DAYD',F.PRODUCTID AS 'PRODUCTS 90 DAYS',F.QUANTITY
   FROM #TEMP_D D
   INNER JOIN  #TEMP_E E  ON D.CUSTOMERID=E.CUSTOMERID
   INNER JOIN #TEMP_F F ON E.CUSTOMERID = F.CUSTOMERID

结尾

I have a Stored Procedure Problem listed here::

For example Customer A has:
i.) Five orders in first 30 days,
ii.) Five orders in first 60 days,
iii.) Five orders in first 90 days

So the result should be:

     A.) First 30 Days: Five orders
     B.) First 60 Days: Five + Five = 10 Orders
     C.) First 90 Days: Five + Five + Five = 15 Orders

However, my stored procedure is showing '15 Orders' for all 30,60 and 90 days. Could you please help me, so that my Stored Procedure shows '5 orders' in 30 days, '10 orders in 60 days' and '15 orders' in 90 days. [BELOW IS THE PART OF THE CODE]
Thank You for your Help.


 IF (@CUSTOMERID IS NOT NULL)
 BEGIN

CREATE TABLE #TEMP_D
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID



CREATE TABLE #TEMP_E
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID


CREATE TABLE #TEMP_F
(
CUSTOMERID VARCHAR(20),
ORDERID INT,
PRODUCTID INT,
QUANTITY VARCHAR(20)
)
INSERT INTO #TEMP_F(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 (CUSTOMERID VARCHAR(20),ORDERID INT,PRODUCTID INT,QUANTITY VARCHAR(20))
 INSERT INTO #TEMP_D(CUSTOMERID,ORDERID,PRODUCTID,QUANTITY)
 SELECT C.CUSTOMERID, COUNT(O.ORDERID),COUNT(OD.PRODUCTID),
        RANKING = CASE
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)>250 THEN 'REGULAR'
        WHEN SUM(OD.QUANTITY*OD.UNITPRICE)< 250 THEN 'MODERATE'
        END
        FROM CUSTOMERS C 
        INNER JOIN
        ORDERS O ON C.CUSTOMERID=O.CUSTOMERID
        INNER JOIN
        [ORDER DETAILS] OD ON O.ORDERID=OD.ORDERID
        WHERE (C.CUSTOMERID = @CUSTOMERID)
        GROUP BY C.CUSTOMERID


SELECT D.CUSTOMERID, D.ORDERID AS 'ORDERS 30 DAYD',D.PRODUCTID AS 'PRODUCTS 30 DAYS',D.QUANTITY, E.CUSTOMERID, E.ORDERID AS 'ORDERS 60                 
   DAYS',E.PRODUCTID AS 'PRODUCTS 60 DAYS',E.QUANTITY,  F.CUSTOMERID, F.ORDERID AS 'ORDERS 90 DAYD',F.PRODUCTID AS 'PRODUCTS 90 DAYS',F.QUANTITY
   FROM #TEMP_D D
   INNER JOIN  #TEMP_E E  ON D.CUSTOMERID=E.CUSTOMERID
   INNER JOIN #TEMP_F F ON E.CUSTOMERID = F.CUSTOMERID

END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-10-16 11:55:23

我可能在这里遗漏了一些东西,但是您对临时表的 3 个查询似乎都是同一个查询。您没有在 WHERE 子句中按日期进行过滤,因此它们都会得出相同的结果(15 个订单)。

I may be missing something here, but your 3 queries into temporary tables all seem to be the same query. You are not filtering by date in your WHERE clauses, therefore they will all come up with the same result (15 orders).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文