1

I have the following data pattern in my SQL 2005 table:

MPAN, Date, Reading1, Reading2, Reading3,......, Reading48

134, 21/05/11, , ,0.345 ,......,0.789

134, 22/05/11, , , ,......, 0.467

456, 21/05/11, , , , , 1.234

456, 22/05/11, 0.009 , , 0.534,......

223, 21/05/11, , , ,........, 3.345

223, 22/05/11, 3.223, 1.234, , , ....,0.989

For each record row there are differnt number of fields having missing data. How could I find out how many fileds have data missing for each row (group by MPAN and Date). Have had a search on Google, it seems that people suggests Stored Procedures for a similar case?

Please can you help?

7 Answers 7

1

The usual approach is to use CASE:

SELECT CASE WHEN field_1 IS NULL THEN 1 ELSE 0 END
       + CASE WHEN field_2 IS NULL THEN 1 ELSE 0 END
       + ... AS total_nulls,
       COUNT(*) as num_rows
FROM table 
GROUP BY total_nulls;

With the grouping and field names from your question:

SELECT MPAN, Date,
       SUM(CASE WHEN Reading1 IS NULL THEN 1 ELSE 0 END
       + CASE WHEN Reading2 IS NULL THEN 1 ELSE 0 END
       + ... ) AS total_nulls
FROM table 
GROUP BY MPAN, Date;
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT
  MPAN,
  Date,
  NullCount = COUNT(*) * 48
    - COUNT(Reading1)
    - COUNT(Reading2)
    - COUNT(Reading3)
    …
    - COUNT(Reading48)
FROM atable
GROUP BY MPAN, Date

Comments

0
select MPAN,
       Date,
       sum
          (
            case when Reading1 is null then 1 else 0 end +
            case when Reading2 is null then 1 else 0 end +
            case when Reading3 is null then 1 else 0 end
          ) as NullCount
from T
group by MPAN,
         Date

Comments

0

Use a case statement to compute the sum:

select MPAN, date, sum(null_cnt) from (
  select MPAN, date,
  (case when val1 is null then 1 else 0 end) +
  (case when val2 is null then 1 else 0 end) + ...
  (case when val48 is null then 1 else 0 end) null_cnt
  from ...
) group by MPAN, date

Comments

0

I'm afraid you'd have to do something ugly like

select MPAN, Date, (
    CASE WHEN Reading1 is null then 1 else 0 end +
    CASE WHEN Reading2 is null then 1 else 0 end +
    CASE WHEN Reading3 is null then 1 else 0 end +
    CASE WHEN Reading4 is null then 1 else 0 end +
    ... +
    CASE WHEN Reading48 is null then 1 else 0 end
) as CountOfNulls

from YourTable

1 Comment

Thanks all for your quick replies. I beleive the CASE function should do the work.
0

If you can change the schema, the first thing I would do is break this up into three tables:

'Readings' with an ID and an Name, values like

ID  Name
1   Reading1
2   Reading2
...
48  Reading48

This makes it easy to add more as you undoubtedly will.

Next, a 'MPAN' table with the 'MPAN's with ID, MPAN and date:

ID MPAN Date
1  134  21/05/11
2  134  22/05/11

And last, a 'Score' table that relates the two and includes the scores with ReadingID, MPANID, and Score:

MPANID ReadingID Score
1      3         0.345
1      48        0.789
2      48        0.467

Then you can let relational databases do what they do best and leverage the relationships. For example, this query would get all the MPAN's that had a score for Reading2:

SELECT MPAN.*, Scores.Score
  FROM MPAN
  JOIN Scores ON Scores.MPANID = MPAN.ID
             AND Scores.ReadingID = 2

Comments

0

USE THIS

select 
MPAN, Date,
coalesce(isnull(reading1,0)+isnull(reading2,0)+isnull(reading3,0)+isnull(reading4,0),0) 
from 
tablename

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.