如何避免group by中的重复记录?
下面的查询用于查找产品的最低价格,
我使用字段Available_Count更新我的表@Rates。
但由于 GROUP BY,结果会重复。
我如何更新这个?
SET NOCOUNT ON DECLARE @Products TABLE (product_id VarChar(50),product_name VarChar(50) )
INSERT INTO @Products Values ('1','Pen');
INSERT INTO @Products Values ('2','Pencil');
INSERT INTO @Products Values ('3','ASchool Bag');
INSERT INTO @Products Values ('4','Book');
INSERT INTO @Products Values ('5','Pencil Box');
SET NOCOUNT ON DECLARE @Rates TABLE (product_id VarChar(50),price int, Avail_Count VarChar(50))
INSERT INTO @Rates Values ('1','10','1');
INSERT INTO @Rates Values ('3','5','5');
INSERT INTO @Rates Values ('1','5','6');
INSERT INTO @Rates Values ('4','20','3');
INSERT INTO @Rates Values ('4','15','2');
INSERT INTO @Rates Values ('5','30','1');
;WITH CTE AS (
SELECT
count(*) over() Total_Record,
p.product_id, p.product_name, ISNULL(MIN(r.price), 0) AS MinPrice,
case when ISNULL(MIN(r.price), 0) > 0 then 1 else 0 end as sortOrder,
R.Avail_Count
FROM
@Products p LEFT OUTER JOIN @Rates r
ON
r.product_id = p.product_id
GROUP BY
p.product_id, p.product_name,R.Avail_Count
),ROWNUM as (Select *,ROW_NUMBER() OVER (ORDER BY sortOrder desc, MinPrice asc,Product_name)
AS RowNumber from CTE )
Select * from ROWNUM
Below query is used to find min price of the product
I update my table @Rates with field Available_Count.
But the result is coming duplicate because of GROUP BY.
How i update this?
SET NOCOUNT ON DECLARE @Products TABLE (product_id VarChar(50),product_name VarChar(50) )
INSERT INTO @Products Values ('1','Pen');
INSERT INTO @Products Values ('2','Pencil');
INSERT INTO @Products Values ('3','ASchool Bag');
INSERT INTO @Products Values ('4','Book');
INSERT INTO @Products Values ('5','Pencil Box');
SET NOCOUNT ON DECLARE @Rates TABLE (product_id VarChar(50),price int, Avail_Count VarChar(50))
INSERT INTO @Rates Values ('1','10','1');
INSERT INTO @Rates Values ('3','5','5');
INSERT INTO @Rates Values ('1','5','6');
INSERT INTO @Rates Values ('4','20','3');
INSERT INTO @Rates Values ('4','15','2');
INSERT INTO @Rates Values ('5','30','1');
;WITH CTE AS (
SELECT
count(*) over() Total_Record,
p.product_id, p.product_name, ISNULL(MIN(r.price), 0) AS MinPrice,
case when ISNULL(MIN(r.price), 0) > 0 then 1 else 0 end as sortOrder,
R.Avail_Count
FROM
@Products p LEFT OUTER JOIN @Rates r
ON
r.product_id = p.product_id
GROUP BY
p.product_id, p.product_name,R.Avail_Count
),ROWNUM as (Select *,ROW_NUMBER() OVER (ORDER BY sortOrder desc, MinPrice asc,Product_name)
AS RowNumber from CTE )
Select * from ROWNUM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您想显示哪个avail_count。您收到重复项是因为您在 GROUP BY 中包含了avail_count,并且它们具有不同的值。您是否希望 Avail_count 值恰好与最小值位于同一行?如果是这样,那么:
如果不是,那么您需要显示实际所需的结果,而不仅仅是解释您不想要重复项。
I am not sure which avail_count you want to show. You are getting duplicates because you are including avail_count in the GROUP BY and they have different values. Do you want the avail_count value that happens to be on the same row as the min value? If so then:
If not then you need to show actual desired results rather than just explain that you don't want duplicates.