Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get SQL Server to return a default of 0, if no rows exist?

How can I get a default value of 0 if a sum does not return any rows?

Edit: I have edited this post to add a more thorough example (which the previous one didn't encounter)

E.g.

 DECLARE @Item TABLE
(
    Id int,
    Price decimal,
    PricePer decimal
)

DECLARE @OrderItem TABLE
(
    Id int,
    ItemId int,
    ChargedPrice nvarchar(10),
    QtyRequired int,
    QtyLeftToDespatch int
)

INSERT INTO @Item (Id,Price,PricePer) VALUES (1,1.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (2,2.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (3,3.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (4,4.00, 1)

INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 1,1,100,100,50 )
INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 2,1,200,300,50)
INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 3,1,300,300,50 )

DECLARE @ItemIdTest int 
SET @ItemIdTest = 4

SELECT SUM((price/priceper)*QtyRequired) as total_value,
       SUM((price/priceper)*QtyRequired) as outstanding_value
FROM @Item i
INNER JOIN @OrderItem o ON i.Id = o.ItemId
WHERE i.Id = @ItemIdTest
group by itemId

This will return

total_value, outstanding_value
==============
<No rows>

But I want it to have 0 as the default value returned. However, if you do a SELECT to select it, then a second SELECT to select a default, 2 result sets will be returned.

Can I do it just in one? I have looked at COALESCE, but this only works if NULL is returned, which it isn't.

Edit: I think the problem is to do with the group with, but is there a reason why still it doesn't come back with any results?

like image 540
Dominic Zukiewicz Avatar asked Sep 17 '09 13:09

Dominic Zukiewicz


People also ask

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 ...

How do I get zeros instead of NULL in SQL?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

How do I assign a default value if no rows returned from the select query MySQL?

Here's one way: SELECT *, IFNULL( ( SELECT col1 FROM table1 WHERE col1 IN ('012311','0123631','091233','092111') ), 'some_value' ) AS my_col1 FROM table1; Not neccessarily copy+paste, you will have to adjust for your specific case.


2 Answers

Try this

SELECT ISNULL(SUM(Debt) ,0)
FROM SupplierDebt
WHERE Id = @TestId
like image 131
Adriaan Stander Avatar answered Oct 24 '22 08:10

Adriaan Stander


 DECLARE @Item TABLE
(
    Id int,
    Price decimal,
    PricePer decimal
)

DECLARE @OrderItem TABLE
(
    Id int,
    ItemId int,
    ChargedPrice nvarchar(10),
    QtyRequired int,
    QtyLeftToDespatch int
)

INSERT INTO @Item (Id,Price,PricePer) VALUES (1,1.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (2,2.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (3,3.00, 1)
INSERT INTO @Item (Id,Price,PricePer) VALUES (4,4.00, 1)

INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 1,1,100,100,50 )
INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 2,1,200,300,50)
INSERT INTO @OrderItem (Id, ItemId,ChargedPrice,QtyRequired,QtyLeftToDespatch) VALUES ( 3,1,300,300,50 )

DECLARE @ItemIdTest int 
SET @ItemIdTest = 4

SELECT COALESCE(SUM((price/priceper)*QtyRequired),0) as total_value,
       COALESCE(SUM((price/priceper)*QtyRequired),0) as outstanding_value
FROM @Item i
LEFT OUTER JOIN @OrderItem o ON i.Id = o.ItemId
WHERE i.Id = @ItemIdTest
group by itemId
like image 40
bleeeah Avatar answered Oct 24 '22 10:10

bleeeah