【问题标题】:count and percent in same query同一查询中的计数和百分比
【发布时间】: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


【解决方案1】:

我想你只需要group by total 所以整个查询将是

SELECT MAX(counthypo) AS counthypo, (MAX(counthypo)/total)*100 AS percHypo, 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 as i131
) i2
group by i2.total

【讨论】:

  • 或这个 (MAX(counthypo)/MAX(total))*100 AS percHypo 并按 i2.total 删除组
  • 谢谢,以上工作完美!谢谢你的帮助!!!祝你有美好的一天!
猜你喜欢
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多