SELECT SUM(JCCD) as num,PDDJ FROM JSZKPDB WHERE PQI is NOT null and XZQHDM like '36%' GROUP BY PDDJ,优化这条sql,使得可以展示出num占总数的比例
时间: 2024-02-17 12:03:34 浏览: 64
JCCD-开源
可以将原始SQL语句改为以下形式,使用子查询来实现总数的计算:
```
SELECT
SUM(JCCD) AS num,
PDDJ,
(SUM(JCCD) * 100.0 / total.total_num) AS ratio
FROM
JSZKPDB,
(SELECT SUM(JCCD) AS total_num FROM JSZKPDB WHERE PQI IS NOT NULL AND XZQHDM LIKE '36%') AS total
WHERE
PQI IS NOT NULL AND
XZQHDM LIKE '36%'
GROUP BY
PDDJ
```
这里使用了一个子查询 `total` 来计算总数,然后再在主查询中使用 `total.total_num` 来获取总数。同时,还计算了 `num` 占总数的比例,即 `num / total.total_num`,并将其命名为 `ratio`。
阅读全文