我有一个具有以下架构的产品表
(ProductID INT, ProductStartDate date, ProductExpDate date,
ProductTypeID int, #PacketsInProduct int,
Price int, Discount int, Score int)
我需要在 MongoDB 中编写类似这样的查询。我正在使用 C# 驱动程序 1.5。我在下面的第一个 CTE (Products_CTE) 中遇到的问题,其中 Row_NUMBER() 正在根据我需要过滤第二个 CTE 中的行的排名来决定与我的 where 条件匹配的产品排名。我所有的条件都是动态的,任何人都可以在这里帮助我。提前致谢
;WITH Products_CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY Score Desc, Price Asc) as RankByProduct,
*
FROM Products
WHERE
ProductID IN (1,2,3)
AND ProductTypeID IN (001,002)
AND [#PacketsInProduct] >3 and [#PacketsInProduct] <10
and ProductExpDate < GETDATE()
),
Products2_CTE
AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY Score DESC, Price ASC) as rownum,
*
FROM Products_CTE
WHERE RankByProduct <= 3
)
SELECT
*
from Products2_CTE cte1
where rownum BETWEEN 15 AND 25
ORDER BY [Score] DESC, [Price] ASC