Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return zero if no record is found

I have a query inside a stored procedure that sums some values inside a table:

SELECT SUM(columnA) FROM my_table WHERE columnB = 1 INTO res; 

After this select I subtract res value with an integer retrieved by another query and return the result. If WHERE clause is verified, all works fine. But if it's not, all my function returns is an empty column (maybe because I try to subtract a integer with an empty value).

How can I make my query return zero if the WHERE clause is not satisfied?

like image 590
giozh Avatar asked Jul 24 '13 16:07

giozh


People also ask

How do you show zero as count if there is no record in database?

you can use ISNULL or COALESCE:both are same with a small difference. ISNULL(param1,param2): can contains only 2 parameter, and there are no condition of having it's value. COALESCE (param1,param2,param3....):can contains multiple parameter, and there is condition of having it's 1 value mujst not be null.

Does count (*) return 0 or null?

COUNT returns the BIGINT data type. If the count includes no rows, COUNT returns 0 or NULL, depending on the query. For more details, see No Rows Returned in Count. Use COUNT in a SELECT query to count rows from the table referenced in the query and return the count in the result set.

How do you handle no data found in SQL query?

no_data_found :- Whenever PL/SQL Block having select into clause and also if requested data is not available then oracle server returns an error ora – 1403 : no data found. For handling this error oracle provided no_data_found exception name. Your employee doesn't exists.

How do I make a count 0 return in SQL?

An ugly workaround, if you want your original query to return a row with 0's, when no records are present, is to add something like this to your query: UNION SELECT NULL AS [Month], 0 AS [COUNT], 0 AS [GRAMS], 0 AS [PRINCIPAL] WHERE (SELECT COUNT(*) FROM #AllExpired) = 0 , but a better solution would be to have your ...


Video Answer


2 Answers

You could:

SELECT COALESCE(SUM(columnA), 0) FROM my_table WHERE columnB = 1 INTO res; 

This happens to work, because your query has an aggregate function and consequently always returns a row, even if nothing is found in the underlying table.

Plain queries without aggregate would return no row in such a case. COALESCE would never be called and couldn't save you. While dealing with a single column we can wrap the whole query instead:

SELECT COALESCE( (SELECT columnA FROM my_table WHERE ID = 1), 0) INTO res; 

Works for your original query as well:

SELECT COALESCE( (SELECT SUM(columnA) FROM my_table WHERE columnB = 1), 0) INTO res; 

More about COALESCE() in the manual.
More about aggregate functions in the manual.
More alternatives in this later post:

  • How to return a value from a function if no value is found
like image 171
Erwin Brandstetter Avatar answered Oct 03 '22 02:10

Erwin Brandstetter


I'm not familiar with postgresql, but in SQL Server or Oracle, using a subquery would work like below (in Oracle, the SELECT 0 would be SELECT 0 FROM DUAL)

SELECT SUM(sub.value) FROM (    SELECT SUM(columnA) as value FROM my_table   WHERE columnB = 1   UNION   SELECT 0 as value ) sub 

Maybe this would work for postgresql too?

like image 32
Jim Avatar answered Oct 03 '22 02:10

Jim