解释一条SQL语句

小D2013 小D2013 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


CREATE Procedure CMRC_CustomerAlsoBought
(
@ProductID int
)
As

/* We want to take the top 5 products contained in
the orders where someone has purchased the given Product */
SELECT TOP 5
CMRC_OrderDetails.ProductID,
CMRC_Products.ModelName,
SUM(CMRC_OrderDetails.Quantity) as TotalNum

FROM
CMRC_OrderDetails
INNER JOIN CMRC_Products ON CMRC_OrderDetails.ProductID = CMRC_Products.ProductID

WHERE OrderID IN
(
/* This inner query should retrieve all orders that have contained the productID */
SELECT DISTINCT OrderID
FROM CMRC_OrderDetails
WHERE ProductID = @ProductID
)
AND CMRC_OrderDetails.ProductID != @ProductID

GROUP BY CMRC_OrderDetails.ProductID, CMRC_Products.ModelName

ORDER BY TotalNum DESC



GO
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
小D2013
.NET社区 62,272 成员 668,968 内容