【问题标题】:TSQL count vs sum distinct valuesTSQL 计数与不同值的总和
【发布时间】:2018-04-23 20:20:29
【问题描述】:

我有一个有点令人困惑的难题,我整天都被困住了。

我有以下类型的数据...

对于每个客户记录,我都有订单号,对于每个订单,我有一系列包裹号,对于每个包裹号,我都有可能的区域...通常,如果有 1 个包裹,数学会相对简单例如,对于 1 个或多个区域,我们只需选择不同数量的座位。

+-----------+-------+-----+------+-------+
|  customer | order | pkg | zone | seats |
+-----------+-------+-----+------+-------+
|         1 |     1 |  11 |    7 |     2 |
|         1 |     1 |  12 |    7 |     2 |
+-----------+-------+-----+------+-------+

我们知道客户 1 每个包裹有 2 个座位。
这就是棘手的地方

+----------+-------+-----+------+-------+
| customer | order | pkg | zone | seats |
+----------+-------+-----+------+-------+
|        2 |     3 |   8 |    5 |     2 |
|        2 |     3 |   9 |    5 |     2 |
|        2 |     3 |  10 |    5 |     2 |
-- In the above case we know a given customer has one order #3, with three packages in the same zone each package has two seats. 
|        2 |     3 |   9 |    6 |     1 |
|        2 |     3 |   9 |    8 |     1 |
|        2 |     3 |  10 |    7 |     2 |
+----------+-------+-----+------+-------+

-- Here things are confusing because the same customer, has a single order #3 (and its possible     
-- both scenarios occur in one single order) with two packages 9 and 10, package 9 has two zones    
-- 1 and 1 and package 10 has one zones with two seats. how do we distinguish when we simply count  
-- the seats like in the first/second occurrence or when we sum the seats like in the last example. 

重申一下,单个客户将有一个订单,每个订单中可以包含多个具有不同包裹编号的包裹,每个包裹可以有 1 个或多个区域,每个区域可以有 1 个或多个座位。

当单个包裹的区域相同时,我们只需计算不同的区域。当一个订单+包裹有多个区域时,我们不计算在内。

我不知道如何编写逻辑代码。请帮忙。

我的专栏是customer_no、order_no、pkg_no、zone_no 和pkg_seats。

这是一个真实的例子

+----------+-------+-----+-------+------+
| customer | order | pkg | seats | zone |
+----------+-------+-----+-------+------+
|      374 |   876 |  68 |     2 |   26 |
|      374 |   876 |  68 |     1 |   32 |
|      374 |   876 |  68 |     1 |   56 |
|      374 |   876 |  71 |     2 |   56 |
|      374 |   876 |  71 |     2 |   79 |
|      862 |   538 |  71 |     2 |   33 |
|      862 |   538 |  71 |     1 |   81 |
|      862 |   538 |  71 |     1 |   82 |
-- In the below case we simply count 2. in the above we sum. 
|      575 |   994 |  68 |     2 |   34 |
|      575 |   994 |  68 |     2 |   79 |
+----------+-------+-----+-------+------+

我应该添加一个超级混乱的部分。我们有一系列包是其他包的一部分。比如包68、70、71都在一起,父包是68。

我无法弄清楚分组。

【问题讨论】:

  • 您希望从样本数据中得到什么结果?我不知道你想达到什么“逻辑”。
  • 我正在尝试计算每个包裹的座位总数。 (我们最终不关心客户/订单数据的细节,但我们一直在进行测试)。并且在某些情况下我们求和而其他情况下的逻辑令人困惑
  • 然后按包分组并对席位求和。你为什么要让它比这更难?选择不同的座位是一种愚蠢的方法。 Group/SUM 是通用的。它适用于所有情况。
  • 因为如果您查看我的实际示例,我们无法计算座位数——在第二部分中,客户是 575,在这种情况下,我们计算不同的座位数,对于 68 号包裹,订单 994 有只有 2 个座位不是 4. 当包装相同但在这种情况下是两个不同的区域时,我们只需计算一次。但是当包裹不同并且区域不同时它会变得很棘手然后我们总结
  • 这正是您需要在问题中发布所需结果的原因。因此,您不只是想要每个包裹的总座位数。创建一些示例数据,其中包含您需要逻辑的所有可能场景,然后从该数据发布所需的结果。那么也许您的问题可能足够清楚,有人可以帮助您。

标签: sql-server count sum grouping distinct


【解决方案1】:
with data as (
    select *,
        min(zone_no) over
            (partition by customer_no, order_no, pkg_no) as min_zone_no1,
        min(zone_no) over
            (partition by customer_no, order_no, pkg_no, pkg_seats) as min_zone_no2
    from T
)
select
    customer_no, order_no, 
    sum(case when zone_no = min_zone_no1 then pkg_seats end) as seat_total1,
    sum(case when zone_no = min_zone_no2 then pkg_seats end) as seat_total2
from data
group by customer_no, order_no
order by customer_no, order_no;

我已经多次重复您的描述,但我仍然不确定我是否走在正确的轨道上。您似乎有重复计算的问题:本质上您想要一个总和,但某些行不应该包括在内。 (在这里“计算不同的座位”可能是错误的命名法。)

我上面的方法是尝试识别涉及“重复”的行集和一些有助于仅计算其中一个的数据。我不确定如何处理 876 号订单,它在三个区域的座位数不同。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多