两个计数值的除法

时间:2019-04-04 06:36:45

标签: sql sql-server tsql reporting-services

嗨,我是SQL的新手,我试图按照下面的代码所述对输出值进行除法

试过这个,但是没用

SELECT COUNT(DATE_COMPLETE)/COUNT(SUB_SYS) AS FINAL 
FROM VW_TWCOMPLETION

我正在使用此代码,其中SUB_SYS的值类似于A,B,C,而DATE_COMPLETE是日期值,如11/19/2019,12/19/2019,13/19/2019

SELECT  SUB_SYS ,COUNT(SUB_SYS) AS TOTAL , COUNT(DATE_COMPLETE) AS COMPLETE
FROM VW_TWCOMPLETION
GROUP BY SUB_SYS
ORDER BY SUB_SYS

我想将从计数中得到的输出除以COMPLETE/TOTAL 如何划分这些输出值?

Table Data

SUB_SYS         

MG1-AS-PWER-05-0

MG1-AS-PWER-06-0

MG1-AS-SPAL-05-0

MG1-AS-SPAL-07-0

TOTAL

6

11

4

17

12

COMPLETE

0

6

0

2

1

0

最后我得到的是下面的代码

0

0

0

0

0

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作

   with cte as
  (
   SELECT  SUB_SYS ,COUNT(SUB_SYS) AS TOTAL , COUNT(DATE_COMPLETE) AS COMPLETE
   FROM VW_TWCOMPLETION
   GROUP BY SUB_SYS

  ) select SUB_SYS, (COMPLETE*1.0) /NULLIF(TOTAL,0) as final from cte ORDER BY SUB_SYS