如何计算sql server中非空列的百分比?

时间:2015-12-30 05:26:58

标签: sql-server sql-server-2008

如何计算sql server中非空列的百分比?

1 个答案:

答案 0 :(得分:1)

如果您的意思是“我的表中某个列中有多少个非空值”:

SELECT
  percentage=CASE WHEN (SELECT COUNT(*) FROM your_table)=0
    THEN 
      NULL
    ELSE
      CAST((SELECT COUNT(*) FROM your_table WHERE your_column IS NOT NULL) AS FLOAT)/
      (SELECT COUNT(*) FROM your_table)
  END

如果您的全局意味着sql server实例中的所有表中有多少非空列:

SELECT
  percentage=
  CAST((
    SELECT
      COUNT(*)
    FROM
      INFORMATION_SCHEMA.COLUMNS
    WHERE
      IS_NULLABLE='NO'
  ) AS FLOAT)/
  (
    SELECT
      COUNT(*)
    FROM
      INFORMATION_SCHEMA.COLUMNS
  )