COUNT(1) - COUNT(column_name)只返回空值的计数吗?

时间:2016-09-16 15:44:57

标签: sql sql-server ssms

COUNT(1) - COUNT(column_name) as total_nulls

只返回给定列的空值计数?

我确信它确实如此但想肯定地知道。感谢

4 个答案:

答案 0 :(得分:4)

你为什么不试试?

<强> DEMO

CREATE TABLE Table1
    ([Country] varchar(7))
;

INSERT INTO Table1
    ([Country])
VALUES
    ('Germany'),
    ('France'),
    (NULL),
    (NULL),
    ('Spain')
;

SELECT count(*), count(country), count(1)
FROM Table1

enter image description here

答案 1 :(得分:1)

count(1)=count(*)--returns nulls
count(columnname)--won't return nulls

答案 2 :(得分:1)

是的。更合乎逻辑且易于阅读的版本将是:

#!/usr/bin/python

import sys

(theother) = (sys.argv[1])
import boto3

e = boto3.client('ec2').describe_instances()
possible = {}

for r in e['Reservations']:
  for i in r['Instances']:
    if 'PrivateIpAddress' in i:
     p = i['PrivateIpAddress']
     if 'NetworkInterfaces' in i:
        for n in i['NetworkInterfaces']:
          if 'Association' in n and 'PublicIp' in n['Association']:
            u = n['Association']['PublicIp']
            if u == theother or p == theother:
              print "found"
              possible['id'] = i['InstanceId']
              possible['public'] = u
              possible['private'] = p
              break

print possible

PS:此版本也将受益于现有索引。

答案 3 :(得分:1)

另一种方法是使用SUM。

select sum(case when MyColumn is null then 1 else 0 end)