【问题标题】:Using the CASE statement not showing the pecent使用 CASE 语句不显示百分比
【发布时间】:2015-03-31 07:29:25
【问题描述】:

我有一个表,我正在尝试使用 CASE 语句获取百分比

SELECT "Agent",
(100* ( max(case when "Agent"= 'Agent1' then "TOTAL"  end)/
max(case when "Agent" = 'TOTAL'  then "TOTAL" end) ))
    as "Agent1%"
From "SumofAgent"
group by Agent

查询在新列旁边显示一个空白。我想这可能是因为价值太小了..这可能吗?

我的桌子是这样的:

Agent       TOTAL
-----       -----
Agent1       13
Agent2       21
Agent3       49
TOTAL        1343

(13/1343)*100=0.96%

我已经尝试了他们自己的max(case when "Agent" = 'TOTAL' then "TOTAL" end)max(case when "Agent"= 'Agent1' then "TOTAL" end) 的案例陈述。他们正在提取正确的值,但是当您尝试进行数学运算时,它不会显示任何内容。

使用 Zoho 报告但将使用 PostgreSQL 进行测试

【问题讨论】:

    标签: postgresql case-statement zoho


    【解决方案1】:

    在组 'Agent1' 中,max(case when "Agent" = 'TOTAL' then "TOTAL" end) 为空

    在“TOTAL”组中max(case when "Agent"= 'Agent1' then "TOTAL" end) 为空

    所有其他组都为空。

    因此,结果列中的每个值在所有结果行上都为空。

    在此处查看示例:

    How to use a SQL window function to calculate a percentage of an aggregate

    【讨论】:

      猜你喜欢
      • 2014-02-09
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2020-10-17
      • 2013-04-23
      相关资源
      最近更新 更多