如何避免group by中的重复记录?

发布于 2024-12-28 13:47:51 字数 1310 浏览 2 评论 0原文

下面的查询用于查找产品的最低价格,

我使用字段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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-01-04 13:47:52

我不确定您想显示哪个avail_count。您收到重复项是因为您在 GROUP BY 中包含了avail_count,并且它们具有不同的值。您是否希望 Avail_count 值恰好与最小值位于同一行?如果是这样,那么:

;WITH x AS
(
    SELECT 
        Total_Record = COUNT(*) OVER(),
        rn = ROW_NUMBER() OVER (PARTITION BY p.product_id ORDER BY r.price),
        p.product_id,
        p.product_name,
        MinPrice = COALESCE(r.price, 0),
        sortOrder = CASE WHEN COALESCE(r.price, 0) > 0 THEN 1 ELSE 0 END
    FROM 
        @Products AS p 
    LEFT OUTER JOIN
        @Rates AS r
        ON r.product_id = p.product_id
)
SELECT 
    Total_Record,
    product_id,
    product_name,
    MinPrice,
    sortOrder,
    RowNumber = ROW_NUMBER() OVER (ORDER BY sortOrder DESC, MinPrice, product_name) 
FROM x WHERE rn = 1;

如果不是,那么您需要显示实际所需的结果,而不仅仅是解释您不想要重复项。

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:

;WITH x AS
(
    SELECT 
        Total_Record = COUNT(*) OVER(),
        rn = ROW_NUMBER() OVER (PARTITION BY p.product_id ORDER BY r.price),
        p.product_id,
        p.product_name,
        MinPrice = COALESCE(r.price, 0),
        sortOrder = CASE WHEN COALESCE(r.price, 0) > 0 THEN 1 ELSE 0 END
    FROM 
        @Products AS p 
    LEFT OUTER JOIN
        @Rates AS r
        ON r.product_id = p.product_id
)
SELECT 
    Total_Record,
    product_id,
    product_name,
    MinPrice,
    sortOrder,
    RowNumber = ROW_NUMBER() OVER (ORDER BY sortOrder DESC, MinPrice, product_name) 
FROM x WHERE rn = 1;

If not then you need to show actual desired results rather than just explain that you don't want duplicates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文