How Do I Replace the Null Result with 0?
When OUTER JOIN (LEFT JOIN, RIGHT JOIN, and FULL JOIN) is executed, the match failure in the outer join generates a large number of NULL values. You can replace these null values with 0.
You can use the COALESCE function to do that. This function returns the first non-null parameter value in the parameter list. For example:
1 2 3 4 5 |
SELECT coalesce(NULL,'hello'); coalesce ---------- hello (1 row) |
Use left join to join the tables course1 and course2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
SELECT * FROM course1; stu_id | stu_name | cour_name ----------+------------+-------------------- 20110103 | ALLEN | Math 20110102 | JACK | Programming Design 20110101 | MAX | Science (3 rows) SELECT * FROM course2; cour_id | cour_name | teacher_name ---------+--------------------+-------------- 1002 | Programming Design | Mark 1001 | Science | Anne (2 rows) SELECT course1.stu_name,course2.cour_id,course2.cour_name,course2.teacher_name FROM course1 LEFT JOIN course2 ON course1.cour_name = course2.cour_name ORDER BY 1; stu_name | cour_id | cour_name | teacher_name ------------+---------+--------------------+-------------- ALLEN | | | JACK | 1002 | Programming Design | Mark MAX | 1001 | Science | Anne (3 rows) |
Use the COALESCE function to replace null values in the query result with 0 or other non-zero values:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
SELECT course1.stu_name, coalesce(course2.cour_id,0) AS cour_id, coalesce(course2.cour_name,'NA') AS cour_name, coalesce(course2.teacher_name,'NA') AS teacher_name FROM course1 LEFT JOIN course2 ON course1.cour_name = course2.cour_name ORDER BY 1; stu_name | cour_id | cour_name | teacher_name ------------+---------+--------------------+-------------- ALLEN | 0 | NA | NA JACK | 1002 | Programming Design | Mark MAX | 1001 | Science | Anne (3 rows) |
Database Usage FAQs
- How Do I Change Distribution Columns?
- How Do I View and Set the Database Character Encoding?
- What Do I Do If Date Type Is Automatically Converted to the Timestamp Type During Table Creation?
- Do I Need to Run VACUUM FULL and ANALYZE on Common Tables Periodically?
- Do I Need to Set a Distribution Key After Setting a Primary Key?
- Is GaussDB(DWS) Compatible with PostgreSQL Stored Procedures?
- What Are Partitioned Tables, Partitions, and Partition Keys?
- How Can I Export the Table Structure?
- How Do I Delete Table Data Efficiently?
- How Do I View Foreign Table Information?
- If No Distribution Column Is Specified, How Will Data Be Stored?
- How Do I Replace the Null Result with 0?
- How Do I Check Whether a Table Is Row-Stored or Column-Stored?
- How Do I Query the Information About GaussDB(DWS) Column-Store Tables?
- Why Sometimes the GaussDB(DWS) Query Indexes Become Invalid?
- How Do I Use a User-Defined Function to Rewrite the CRC32() Function?
- What Are the Schemas Starting with pg_toast_temp* or pg_temp*?
- Solutions to Inconsistent GaussDB(DWS) Query Results
- Which System Catalogs That the VACUUM FULL Operation Cannot Be Performed on?
- In Which Scenarios Would a Statement Be "idle in transaction"?
- How Does GaussDB(DWS) Implement Row-to-Column and Column-to-Row Conversion?
- What Are the Differences Between Unique Constraints and Unique Indexes?
- What Are the Differences Between Functions and Stored Procedures?
- How Do I Delete Duplicate Table Data?
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbotmore