简体   繁体   English

对列中具有相同 ID 的所有值求和会在 SQL 中给我重复值吗?

[英]Summing all values with same ID in a column give me duplicated values in SQL?

I am trying to sum all the columns that have the same ID number in a specified date range, but it always gives me duplicated values我正在尝试对指定日期范围内具有相同 ID 号的所有列求和,但它总是给我重复的值

select pr.product_sku,
       pr.product_name, 
       pr.brand,
       pr.category_name,
       pr.subcategory_name,
       a.stock_on_hand, 
       sum(pr.pageviews) as page_views,
       sum(acquired_subscriptions) as acquired_subs,
       sum(acquired_subscription_value) as asv_value
from dwh.product_reporting pr
join dm_product.product_data_livefeed a 
on pr.product_sku = a.product_sku 
where pr.fact_day between '2022-05-01' and '2022-05-30' and pr.pageviews > '0' and pr.acquired_subscription_value > '0'  and store_id = 1 
group by pr.product_sku,
         pr.product_name,
         pr.brand,
         pr.category_name,
         pr.subcategory_name,
         a.stock_on_hand;

This supposes to give me: Sum of all KPI values for a distinct product SKU这应该给我: 不同产品 SKU 的所有 KPI 值的总和
Example table:示例表:

|     Date   | product_sku |page_views|number_of_subs
|------------|-------------|----------|--------------|
| 2022-01-01 |     1       |   110    |    50        |
| 2022-01-25 |     2       |   1000   |    40        |
| 2022-01-20 |     3       |   2000   |    10        |
| 2022-01-01 |     1       |   110    |    50        |
| 2022-01-25 |     2       |   1000   |    40        |
| 2022-01-20 |     3       |   2000   |    10        |

Expected Output:预期输出:

| product_sku |page_views|number_of_subs
|-------------|----------|--------------|
|     1       |   220    |    100       |
|     2       |   2000   |    80        |
|     3       |   4000   |    20        |

Sorry I had to edit to add the table examples抱歉,我必须编辑以添加表格示例

Since you're not listing the dupes (assuming they are truly appearing as duplicate rows, and not just multiple rows with different values), I'll offer that there may be something else that's at play here - I would suggest for every string value in your result set that's part of the GROUP BY clause to apply a TRIM(UPPER()) as you might be dealing with either a case insensitivity or trailing blanks that are treated as unique values in the query.由于您没有列出欺骗者(假设它们确实显示为重复的行,而不仅仅是具有不同值的多行),我将提供可能还有其他东西在这里起作用 - 我会建议每个字符串值在作为 GROUP BY 子句的一部分的结果集中应用 TRIM(UPPER()) 因为您可能正在处理不区分大小写或在查询中被视为唯一值的尾随空格。

Assuming all the columns are character based:假设所有列都是基于字符的:

select trim(upper(pr.product_sku)),
       trim(upper(pr.product_name)), 
       trim(upper(pr.brand)),
       trim(upper(pr.category_name)),
       trim(upper(pr.subcategory_name)),
       sum(pr.pageviews) as page_views,
       sum(acquired_subscriptions) as acquired_subs,
       sum(acquired_subscription_value) as asv_value
from dwh.product_reporting pr
where pr.fact_day between '2022-05-01' and '2022-05-30' and pr.pageviews > '0' and pr.acquired_subscription_value > '0'  and store_id = 1 
group by trim(upper(pr.product_sku)),
       trim(upper(pr.product_name)), 
       trim(upper(pr.brand)),
       trim(upper(pr.category_name)),
       trim(upper(pr.subcategory_name));

Thank you guys for all your help, I found out where the problem was.谢谢大家的帮助,我发现问题出在哪里了。 It was mainly in the group by when I removed all the other column names and left only the product_sku column, it worked as required当我删除所有其他列名并只留下 product_sku 列时,它主要在group by中,它按要求工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM