My project manager recently asked me to send him a report in Excel that showed the number of rows with NULLs in...
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
each column. The report looked like this:
Plant Code No. Of Recs PO NUM UNSPSC Code Commodity code ......... XXX 100 5 20 30 YYY 200 4 25 34 ZZZ 250 30 1
It is very simple to find out the number of rows that have NULLs in one column, but each table has lot of columns and data warehouses have many tables, so it is very difficult to write different SQL statements and combine the results. I came up with following simple solution. It was tested on Oracle 8.1.7 and 9i.
SELECT a.plant_code, no_of_recs, po_number, unspsc_code, commodity_code FROM ( SELECT plant_code, count(1) no_of_recs FROM po_lines GROUP BY plant_code ) A, ( SELECT plant_code, count(1) po_number FROM po_lines where po_number IS NULL GROUP BY plant_code ) B, ( SELECT plant_code, count(1) unspsc_code FROM po_lines where unspsc_code IS NULL GROUP BY plant_code ) C, ( SELECT plant_code, count(1) commodity_code FROM po_lines where commodity_code IS NULL GROUP BY plant_code ) D WHERE a.plant_code = b.plant_code (+) AND a.plant_code = c.plant_code (+) AND a.plant_code = d.plant_code (+) ORDER BY a.plant_code
select '( SELECT plant_code, count(1) '||lower(column_name) ||' FROM po_lines where '|| lower(column_name) || ' IS NULL GROUP BY plant_code'||' ) ' ||chr(column_id + 65) || ', ' from user_tab_columns where table_name = 'PO_LINES' order by column_idp;
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.