简体   繁体   中英

Count Nulls in a table and group by column name in SQL Server

Basically I have a table that I need to count the number of NULL values in each column and return the count for that each column along with that column's name. I can query each column individually but there are 191 columns in this table.

Any assistance would be greatly appreciated.

This gets the Null value for one column but I would have to run this 190 more times to get all the rows:

SELECT COUNT(*) AS TS_EQUIPMENTLOCATION
FROM dbo.USR_IT_PURCHASE_TRACKER
WHERE (TS_EQUIPMENTLOCATION IS NULL)

use case when with aggregate function sum()

SELECT sum(case when TS_EQUIPMENTLOCATION IS NULL then 1 else 0 end )
AS TS_EQUIPMENTLOCATION    
FROM dbo.USR_IT_PURCHASE_TRACKER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM