开发者

sql query disregarding all rows that have all null columns

开发者 https://www.devze.com 2022-12-17 00:01 出处:网络
I have an SQL Server query that needs to count the number of rows returned, but I need to disregard rows where all the column values are NULL. Some rows have NULL values for some of the columns, but t

I have an SQL Server query that needs to count the number of rows returned, but I need to disregard rows where all the column values are NULL. Some rows have NULL values for some of the columns, but that is ok. I just need to filter out the ones that have ALL NULL values.

Right now I am returning all rows and using a SqlDataReader iterating the returned rows and counting the ones I need. I would like to 开发者_如何学Gomove this into the query itself if possible.

Thanks!

EDIT: What I am trying to do is similar to this, but I am apparently having a hard time making the VS SQL editor recognize what I'm doing:

SELECT COUNT(sd.[ID]) 
FROM [Some Data] sd 
WHERE sd.[Data Name] = 'something' AND ((sd.q1 IS NOT NULL) OR (sd.q2 IS NOT NULL)) 

etc..


select count(id)
from [Some Data]
where not (Column1 is null and Column2 is null and Column3 is null ...)


How about something like this:

Select Count(*) From MyTable Where Column1 IS NOT NULL And Column2 IS NOT NULL And Column3 IS NOT NULL ...


You already selected an answer, but this is the most correct it should be the fastest because the SQL Engine can optimize it and "short circuit" it.

SELECT count(id)
FROM [Some Data]
WHERE NOT (COALESCE(Column1,Column2,Column3...) is null)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号