Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8444

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?

Upvotes: 9

Views: 27730

Answers (4)

bleeeah
bleeeah

Reputation: 3604

 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

Upvotes: 4

bleeeah
bleeeah

Reputation: 3604

Or use

SELECT COALESCE(SUM(Debt) ,0)
FROM SupplierDebt
WHERE Id = @TestId

COALESCE is an ANSI standard, if that bothers you and can take multiple parameters.

Upvotes: 10

n8wrl
n8wrl

Reputation: 19765

SQL 2k5: this worked for me:

isnull(sum(Dept), 0)

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166396

Try this

SELECT ISNULL(SUM(Debt) ,0)
FROM SupplierDebt
WHERE Id = @TestId

Upvotes: 13

Related Questions