Reputation: 390
I have one aggregate sum query as following.
select sum(size) as size from table;
which gives
size
100
I want to add const column value Product='Sugar' for which I changed query as per following
select 'Sugar' as Product, sum(size) as size from table;
Which works fine except one case when there is no row I am getting following output.
Product | Size
Sugar
Here I want empty row instead Sugar. Is there anyway to do this? expected result when no row is
Product | Size
I am using postgresql but putting other db as well.
Upvotes: 0
Views: 2253
Reputation: 718
I think you are having summing result to null in sizes, if that the case this query should work
select 'Sugar' as Product, sum(size) as size from table having sum(size) is not null ;
Upvotes: 0
Reputation: 1269763
You can do this with a having
clause:
select 'Sugar' as Product, sum(size) as size
from table
having count(*) > 0;
Upvotes: 1
Reputation: 246493
Use an outer query:
SELECT *
FROM (SELECT 'Sugar' AS product,
sum(size) AS size
FROM table) AS q
WHERE size IS NOT NULL;
Or use a HAVING
clause, as suggested in the comment.
Upvotes: 0