TSQL计数与总和不同的值

时间:2018-04-23 20:20:30

标签: sql-server count sum grouping distinct

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

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

对于每个客户记录,我有订单号和每个订单,我有一系列的包裹编号,对于每个包裹编号,我有可能是区域...通常,如果有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_noorder_nopkg_nozone_nopkg_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.

我无法弄清楚分组。

1 个答案:

答案 0 :(得分:0)

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,这三个区域的座位数不同。