简体   繁体   English

对在另一列上过滤的列求和

[英]sum a column filtered on another column

I am trying to get the sum of a column that is filtered by another column (distinct values of that column). 我试图获取由另一列(该列的不同值)过滤的一列的总和。 I have tried "group by" "distinct" "with rollup" and many other variations. 我尝试了“分组”,“不同”,“汇总”和许多其他变体。 I have a bunch of columns showing a customer link. 我有一堆列显示客户链接。 Most services have a redundant side, though some don't have redundancy. 大多数服务具有冗余方面,尽管有些服务没有冗余。 I am trying to get a bandwidth total for entire "fnn" column but excluding the redundant side value if it exists. 我正在尝试获取整个“ fnn”列的带宽总计,但不包括冗余边值(如果存在)。 If there is redundancy, then it will be indicated by having the same fnn (and same bandwidth value). 如果存在冗余,则将通过具有相同的fnn(和相同的带宽值)来指示。

I have been struggling with this for ages. 我已经为此奋斗了很长时间。 Any help would be fantastic. 任何帮助都将是极好的。 Solution in mysql or php. 在mysql或php中的解决方案。

my table looks like: 我的桌子看起来像:

table name = cust_child

    id | cust_name |cp_bw|cp_fnn |other_columns  
    1  | A         | 30  | 11    |xxx   
    2  | A         | 30  | 11    |xxx 
    3  | B         |100  | 22    |xxx
    4  | B         |100  | 22    |xxx
    5  | C         |50   | 33    |xxx

Result should look like:

    Total bandwidth = 180
SELECT SUM(c.cp_bw) AS bandwith
FROM cust_child c
 INNER JOIN (
   SELECT c.id
   FROM cust_child c
   GROUP BY c.cp_fnn
 ) t1 ON (t1.id = c.id)

http://sqlfiddle.com/#!2/01219/1 http://sqlfiddle.com/#!2/01219/1

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

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