当前位置:网站首页>统计,在sql中求每个部门的数据比值
统计,在sql中求每个部门的数据比值
2022-07-22 02:47:00 【CSDN问答】
问题遇到的现象和发生背景
在做统计的时候想直接计算每个部门中物品数量和所有部门中的物品数量的比值,但是使用group中使用sum肯定是只能计算分组后的数据。不能计算出所有列的总数。不知道有什么好办法实现,俺不才,只能想出暴力方法。
问题相关代码
SELECT *, total / num FROM ( SELECT sd.depart_name, sum( d.stock ) AS total 1 AS n FROM depart d LEFT JOIN sys_depart sd ON d.depart_id = sd.id WHERE d.is_delete = 0 GROUP BY d.depart_id ) AS n LEFT JOIN ( SELECT 1 AS n, sum( stock ) AS num FROM depart ) AS b ON n.n = b.n此处是先计算出所有部门的物品总数,并设置固定id ‘n’ 然后查询分组后的部门物品数量,设置id ‘n’,再在外面进行外连接查询,根据两个视图的数据进行连接。将每个数据上都加上所有部门物品数量总和数据去求比值。
运行结果
边栏推荐
- TDengine实验集群搭建 Success
- Barcode(条形码)
- JS高级 之 ES6 实现继承
- res中values-swxxdp计算
- Rebound shell carries out suid authorization through ordinary users
- Aidl example
- 对原数组有影响的几个方法
- ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or
- PLT draw and save the results
- Debugging VBS Visual Studio
猜你喜欢
Cloud native ide: the first general and powerful codeless development platform of IVX
A Recommendation for interface-based programming
卷妹的面试小抄每日更新Day1
Installation and use of tensorboard
Debugging VBS Visual Studio
The installation and use of harbor+trivy -- the way to build a dream
ADB 命令结合 monkey 的简单使用,超详细
第三章第五节:返回值
JS advanced ES6 implementation inheritance
mysql安装失败,不知道什么原因
随机推荐
A Recommendation for interface-based programming
如何用一手数据可视化获得老板的青睐,抓住数据可视化关键点
FPGA image processing learning face recognition
The difference between overload and override
JMeter notes 1 | introduction and architecture of JMeter
[activity registration] stack a buff for your code! Click "tea" to receive the gift
第三章第五节:返回值
ADB 命令结合 monkey 的简单使用,超详细
V-IF, V-for, list filtering and sorting, forced binding of class and style, and collection of form information
JMeter笔记1 | JMeter简介及体系结构
Leetcode exercise 1 - binary tree pruning
golang语言cli库
云原生IDE:iVX首个通用且强大无代码开发平台
Debugging VBS Visual Studio
调试VBS visual studio
【华为机试真题】组成最大数【2022 Q3 | 100分】
AutoLabel (AutoLabel)
Exponential moving average method_ Baidu Encyclopedia
halcon 使用txt文件格式显示点云
BannerText(水印文本)