code_junkie

SQL find non-null columns

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. select max(field1),max(field2),max(field3),...
  2. from series where t_stamp between x and y
  3.  
  4. SELECT
  5. CASE WHEN EXISTS (SELECT * FROM Series WHERE t_stamp BETWEEN @x AND @y AND field1 IS NOT NULL) THEN 1 ELSE 0 END AS field1,
  6. CASE WHEN EXISTS (SELECT * FROM Series WHERE t_stamp BETWEEN @x AND @y AND field2 IS NOT NULL) THEN 1 ELSE 0 END AS field2,
  7. ...
  8.  
  9. create table series (fieldno integer, t_stamp date);
  10.  
  11. select distinct fieldno from series where t_stamp between x and y;
  12.  
  13. select 'field1' as fieldname from series
  14. where field1 is not null and t_stamp between x and y
  15. UNION
  16. select 'field2' from series where field2 is not null
  17. ... etc
  18.  
  19. select count(field1),count(field2),count(field3),...
  20. from series where t_stamp between x and y
  21.  
  22. SELECT CASE WHEN field1 IS NOT NULL THEN '' ELSE 'contains null' END AS field1_stat,
  23. CASE WHEN field2 IS NOT NULL THEN '' ELSE 'contains null' END AS field2_stat,
  24. ... for every field to be checked
  25. FROM series
  26. WHERE foo IN bar
  27. GROUP BY CASE WHEN field1 IS NOT NULL THEN '' ELSE 'contains null' END,
  28. CASE WHEN field2 IS NOT NULL THEN '' ELSE 'contains null' END
  29. ... etc
Add Comment
Please, Sign In to add comment