Count row with content for specific number of columns
count, sql-server-2008, sum, t-sql
Solution
SELECT SUM(CASE WHEN A IS NULL THEN 0 ELSE 1 END)
+ SUM(CASE WHEN B IS NULL THEN 0 ELSE 1 END)
+ SUM(CASE WHEN C IS NULL THEN 0 ELSE 1 END)
AS [TOTAL]
FROM dbo.SomeTable
Demo
Problem
I can't figure out how to make a column with the count of rows with content of some other columns. Without a count on all columns (*) ``` ------------------------- Output should be (NO COLUMN D): --------- | A | B | C | D | | Count | ------------------------- --------- | foo | foo | foo | foo | | 9 | ------------------------- --------- | bar | bar | bar | bar | ------------------------- | x | x | | x | ------------------------- | y | | | y | ------------------------- ``` I've tried many approach like : - `SELECT SUM(COUNT(A) + Count(B) + COUNT(C)) from SomeTable` - `SELECT COUNT(A + B + C)` - `SELECT COUNT(A,B,C)` I can't find the correct syntax. Can anybody help me on this one ?