使用COUNT计算百分比

huangapple go评论84阅读模式
英文:

Calculating percentages with COUNT

问题

我有一个包含列owneridnamecreatedonsvb_contactname的表,名为incident。我试图使用svb_contactname列来计算每个人的NULL和非NULL值的百分比(PercYesPercNo列),但我在获取百分比时遇到了问题。

示例数据表如下:

owneridname createdon svb_contactname
Brittany Miller 7/3/2023 NULL
Christine Hak 7/3/2023 Jeremiah Smith
Dawn Cembellin 7/3/2023 Robert Drago
Dominic Sanchez 7/3/2023 Frank Kirby
Dylinn Guiles 7/3/2023 NULL

当我尝试使用 COUNT(svb_contactname)/COUNT(*) [PercYes] 时,它似乎只返回10(如下面的结果表所示),而我希望返回一个小数作为结果。PercNo列也是同样的情况。

SELECT
  owneridname,
  CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week],
  FORMAT(createdon, 'MMMM', 'en-US') [Month],
  COUNT(svb_contactname) AS [Yes], /*计算非NULL值的数量*/
  COUNT(*) - COUNT(svb_contactname) AS [No], /*计算列中的NULL值数量*/
  COUNT(svb_contactname)/COUNT(*) [PercYes],
  (COUNT(*)-COUNT(svb_contactname))/COUNT(*) [PercNo]
FROM incident
WHERE createdon >= DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
GROUP BY
  owneridname,
  CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date),
  FORMAT(createdon, 'MMMM', 'en-US')
ORDER BY
  Week DESC;

初始结果:

owneridname Week Date Month Yes No PercYes PercNo
John Smith 7/3/2023 July 7 0 1 0
Margo Johnson 7/3/2023 July 18 7 0 0
Caitlin Dakota 7/3/2023 July 0 2 0 1
Matthew Drake 7/3/2023 July 5 2 0 0
Dan Bingo 7/3/2023 July 0 1 0 1

我希望生成以下结果:

owneridname Week Date Month Yes No PercYes PercNo
John Smith 7/3/2023 July 7 0 1 0
Margo Johnson 7/3/2023 July 18 7 .72 .28
Caitlin Dakota 7/3/2023 July 0 2 0 1
Matthew Drake 7/3/2023 July 5 2 .71 .29
Dan Bingo 7/3/2023 July 0 1 0 1
英文:

I have a table with columns owneridname, createdon,svb_contactname in a table called incident. I am trying to calculate the percentage of NULL and non-NULL values for each person using the svb_contactname column (the PercYes and PercNo column) but I am having trouble getting the percentages to work.

Sample (example) data table:

owneridname createdon svb_contactname
Brittany Miller 7/3/2023 NULL
Christine Hak 7/3/2023 Jeremiah Smith
Dawn Cembellin 7/3/2023 Robert Drago
Dominic Sanchez 7/3/2023 Frank Kirby
Dylinn Guiles 7/3/2023 NULL

When I attempt to use COUNT(svb_contactname)/COUNT(*) [PercYes] it only seems to return a 1 or a 0 (as you can see in the results table below) when I am looking for a decimal number to be returned as the result. Same is for the PercNo column.

SELECT
  owneridname,
  CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week],
  FORMAT(createdon, 'MMMM', 'en-US') [Month],
  COUNT(svb_contactname) AS [Yes], /*Counts non-NULL values*/
  COUNT(*) - COUNT(svb_contactname) AS [No], /*Counts only NULL values in the column*/
  COUNT(svb_contactname)/COUNT(*) [PercYes],
  (COUNT(*)-COUNT(svb_contactname))/COUNT(*) [PercNo]
FROM incident
WHERE createdon >= DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
GROUP BY
  owneridname,
  CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date),
  FORMAT(createdon, 'MMMM', 'en-US')
ORDER BY
  Week DESC;

Initial results:

owneridname Week Date Month Yes No PercYes PercNo
John Smith 7/3/2023 July 7 0 1 0
Margo Johnson 7/3/2023 July 18 7 0 0
Caitlin Dakota 7/3/2023 July 0 2 0 1
Matthew Drake 7/3/2023 July 5 2 0 0
Dan Bingo 7/3/2023 July 0 1 0 1

I am looking to produce these results:

owneridname Week Date Month Yes No PercYes PercNo
John Smith 7/3/2023 July 7 0 1 0
Margo Johnson 7/3/2023 July 18 7 .72 .28
Caitlin Dakota 7/3/2023 July 0 2 0 1
Matthew Drake 7/3/2023 July 5 2 .71 .29
Dan Bingo 7/3/2023 July 0 1 0 1

答案1

得分: 0

如果你除以整数,结果将是整数。只需将其中一个变成浮点数,你将得到小数作为结果:

SELECT
    owneridname,
    CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [],
    FORMAT(createdon, 'MMMM', 'en-US') [月份],
    COUNT(svb_contactname) AS [], /*计算非NULL值的数量*/
    COUNT(*) - COUNT(svb_contactname) AS [], /*仅计算该列中的NULL值数量*/
    CAST(COUNT(svb_contactname) AS FLOAT)/COUNT(*) [百分比是],
    CAST((COUNT(*)-COUNT(svb_contactname)) AS FLOAT)/COUNT(*) [百分比否]
FROM incident
WHERE createdon >= DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
GROUP BY
    owneridname,
    CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date),
    FORMAT(createdon, 'MMMM', 'en-US')
ORDER BY
     DESC;

将这部分改成小数:

ROUND(CAST(COUNT(svb_contactname) AS FLOAT)/COUNT(*) * 100, 2) [百分比是],
ROUND(CAST((COUNT(*)-COUNT(svb_contactname)) AS FLOAT)/COUNT(*) * 100, 2) [百分比否]
英文:

If you divide integers, the result is integer. Just by making one of them float, you will get decimal as result:

  SELECT
      owneridname,
      CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week],
      FORMAT(createdon, 'MMMM', 'en-US') [Month],
      COUNT(svb_contactname) AS [Yes], /*Counts non-NULL values*/
      COUNT(*) - COUNT(svb_contactname) AS [No], /*Counts only NULL values in the column*/
      CAST(COUNT(svb_contactname) AS FLOAT)/COUNT(*) [PercYes],
      CAST((COUNT(*)-COUNT(svb_contactname)) AS FLOAT)/COUNT(*) [PercNo]
    FROM incident
    WHERE createdon >= DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
    GROUP BY
      owneridname,
      CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date),
      FORMAT(createdon, 'MMMM', 'en-US')
    ORDER BY
      Week DESC;

Change this for decimals:

ROUND(CAST(COUNT(svb_contactname) AS FLOAT)/COUNT(*) * 100, 2) [PercYes],
ROUND(CAST((COUNT(*)-COUNT(svb_contactname)) AS FLOAT)/COUNT(*) * 100, 2) [PercNo]

huangapple
  • 本文由 发表于 2023年7月5日 00:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定