【发布时间】:2018-01-31 15:13:16
【问题描述】:
我正在使用以下查询来获取基于 WHERE 参数的计数。
我希望能够根据计数 (i131ID) 包含每个组的百分比,但我似乎无法让查询返回结果。
原始查询:
SELECT MAX(counthypo) counthypo, MAX(counteuthyroid) counteuthyroid, MAX(counthyper) counthyper, MAX(none) none, MAX(unknown) unknown
FROM (
SELECT count(*) as counthypo, 0 as counteuthyroid, 0 as counthyper, 0 as none, 0 as unknown
FROM tbl_I131_data
WHERE `recheck_t4` < `recheck_t4_range_low`
UNION
SELECT 0 as counthypo, count(*) as counteuthyroid, 0 as counthyper, 0 as none, 0 as unknown
FROM tbl_I131_data
WHERE (`recheck_t4` BETWEEN `recheck_t4_range_low` AND `recheck_t4_range_high`)
UNION
SELECT 0 as counthypo, 0 as counteuthyroid, count(*) as counthyper, 0 as none, 0 as unknown
FROM tbl_I131_data
WHERE `recheck_t4` > `recheck_t4_range_high`
UNION
SELECT 0 as counthypo, 0 as counteuthyroid, 0 as counthyper, count(*) as none, 0 as unknown
FROM tbl_I131_data
WHERE `nordvmfollowup` = '1' AND `isotope` = '1'
UNION
SELECT 0 as counthypo, 0 as counteuthyroid, 0 as counthyper, 0 as none, count(*) as unknown
FROM tbl_I131_data
WHERE `recheck_t4` is null AND `isotope` = '1' and `nordvmfollowup` is null
) i
我试过做一个交叉连接,使用
CROSS JOIN
(SELECT COUNT(*) as total
FROM tbl_I131_data i131
) i2
然后在最初的 SELECT 中,我尝试了类似的方法
SELECT MAX(counthypo) counthypo, (counthypo/total)*100 AS percHypo, MAX(counteuthyroid) counteuthyroid, MAX(counthyper) counthyper, MAX(none) none, MAX(unknown) unknown
我就是想不通。
【问题讨论】:
-
那么,您尝试过的其他每件事都没有结果,还是什么?
标签: mysql