存储过程:即使订单日期不同,三列中的值也相同
我在这里列出了一个存储过程问题::
例如客户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能在这里遗漏了一些东西,但是您对临时表的 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).