3 个表 sto_Product、sto_ProductXCategory 和 sto_Category
- sto_Product 拥有所有产品信息(bvin=主键)
- sto_Category 拥有所有 Categoryinfo (bvin=Primary Key)
- sto_ProductXCategory(ProductId 和 CategoryId 是 PK 和 FK)
目前它会导致产品重复,因为某些产品在 sto_ProductXCategory 中重复(具有不同的 CategoryId)。
这个 SQL 语句是一个更大的存储过程的一部分,所以我试图让它保持相似。
这是我要修复的声明
DECLARE @Sortorder int =6;
with product as
(
select distinct p.*
from sto_Product p
inner join sto_ProductXCategory px on(p.bvin = px.productid)
where p.parentID = ''
and p.Status = 1
and p.siteprice >=0
and p.siteprice <= 2147483647
and (
(p.manufacturerid = '8036ab2c-641e-487b-a577-76c09c5c3cc9'
)
)
),
AllProducts AS
(
SELECT
RowNum =
CASE
WHEN @Sortorder = 0
THEN ROW_NUMBER() OVER (ORDER BY px.SortOrder)
WHEN @Sortorder = 1
THEN ROW_NUMBER() OVER (ORDER BY px.SortOrder)
WHEN @Sortorder = 2
THEN ROW_NUMBER() OVER (ORDER BY ProductName)
WHEN @Sortorder = 3
THEN ROW_NUMBER() OVER (ORDER BY SitePrice)
WHEN @Sortorder = 4
THEN ROW_NUMBER() OVER (ORDER BY SitePrice DESC)
WHEN @Sortorder = 6
THEN ROW_NUMBER() OVER (ORDER BY ProductName DESC)
ELSE ROW_NUMBER() OVER (ORDER BY px.SortOrder)
END,
p.*
from product p
inner join sto_ProductXCategory px on(p.bvin = px.productid)
)
SELECT AllProducts.RowNum,*,
(SELECT COUNT(*) FROM AllProducts) As TotalRowCount
FROM AllProducts
WHERE RowNum BETWEEN (1) and (12 )
ORDER BY AllProducts.RowNum
样本数据集
sto_Product
- bvin,sku,产品名称,网站价格
- 0001,001,产品名称,100
- 0002,002,B产品名称,50
- 0003,003,C产品名称,75
sto_Category
- bvin,类别名称
- 1000,A类别名称
- 2000,B类别名称
- 3000,C类别名称
sto_ProductXCategory
- 产品 ID、类别 ID、排序顺序
- 0001,1000,1
- 0001,2000,2
- 0001,3000,3
- 0002,2000,4
- 0003,2000,5
- 0003,3000,6
提前致谢