mysql - 关于 MySQL 子总和

标签 mysql subtotal

我在获取 MySQL 小计时遇到问题。

源数据

网址计数

/test/a/1/temp.txt 10
/test/a/2/temp.txt 10
/test/a/3/temp.txt 10
/test/b/1/temp.txt 10
/test/1/temp.txt 10
/test/1/temp2.txt 10
/test/1/temp3.txt 10

结果

网址计数级别

/test    70    1
/test/a    30    2
/test/a/1    10    3
/test/a/1/temp.txt    10    4
/test/a/2    10    3
/test/a/2/temp.txt    10    4
/test/a/3    10    3
/test/a/3/temp.txt    10    4
/test/b    40    2
/test/b/1    10    3
/test/b/1/temp.txt    10    4
/test/1    30    2
/test/1/temp.txt    10    3
/test/1/temp2.txt    10    3
/test/1/temp3.txt    10    3

我想知道怎么做。

谢谢 谨致问候。

最佳答案

对于您的特定数据,您可以执行以下操作:

select url, sum(count(*)
from ((select substring_index(url, '/', 2) as url, count
       from source
      ) union all
      (select substring_index(url, '/', 3) as url, count
       from source
      ) union all
      (select substring_index(url, '/', 4) as url, count
       from source
      ) union all
      (select substring_index(url, '/', 5) as url, count
       from source
      ) 
     ) s
group by url
order by url;

关于mysql - 关于 MySQL 子总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970085/

相关文章:

mysql - 使用 MySQL MATCH 混合不区分大小写的列类型

pandas - 将按类别分类汇总的行添加到我的数据框

Magento - 如何获取购物车中单个产品的小计?

python - 无法将小计插入 Pandas 数据框

php - 在选择中推进子查询

mysql - 带有 MySQL docker 容器网络设置的 Node.js

mysql - 在sql中显示合并的数据

mysql 比较两个表以查找匹配的记录

excel - 为什么没有值时我的小计代码计数为 1?

mysql - 每行字段中某个值出现的次数