Solved

how return 0 is value is null

Posted on 2011-03-21
10
183 Views
Last Modified: 2012-06-27
I have the following which works fine. However if the following sub query returns null then my results are incorrect. How can i ensure the "Sum" keyword return 0 is there are no results.
(select Sum(oi.Quantity)
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)

Open in new window



Here is the full query
 
(select Sum(oi.Quantity) as available from
 OrderItems oi inner join Orders o on oi.OrderId = o.Id where (oi.OrderItemStatusId = 0 or oi.OrderItemStatusId = 1)  and oi.ProductId = 80493)
 
 
 SELECT p.Id                                   as Id,
       p.Name,
       CustomerSku,
       p.ProductKey,
       p.MinQuantity,
       p.MaxQuantity,
       p.Stock,
       p.IsStockItem,
       p.ShowStockPrice,
       ImageThumbUrl,
       p.Teaser,
       p.Description,
       (p.Stock
          - (select Sum(oi.Quantity)
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)) as ProjectedStock
FROM   ProductCategories pc
       inner join Products p
         on pc.ProductId = p.Id
WHERE  pc.CompanyId = 27
       and pc.CategoryId = 138
       and p.Archive = 0
       and p.Disable = 0
       and p.AutoDisable = 0
       and EXISTS (select *
                   from   RetailerInGroup rig
                   where  (rig.RetailerGroupId = p.RetailerGroupId
                           and rig.RetailerId = 13)
                           or p.RetailerGroupId is Null)

Open in new window

 
0
Comment
Question by:frosty1
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35179665
(select

case when Sum(oi.Quantity) is NULL then 0 End
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35179667
isnull((select Sum(oi.Quantity)
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id), 0)
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35179669
(select

case when Sum(oi.Quantity) is NULL then 0 End as available
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)
0
 
LVL 10

Accepted Solution

by:
John Claes earned 500 total points
ID: 35179672
replace in the Sum(oi.Quantity)
into 1 of the folowing


Sum(case when oi.Quantity is null then 0 else oi.Quantity end )  
Or
Sum(ISNULL (  oi.Quantity ,0))

They do the same

poor beggar

0
 
LVL 10

Expert Comment

by:John Claes
ID: 35179676
The reason why I change the check inside the SUM


4 + 5 + Null = null
4 + 5 + 0 = 9
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:ramkihardy
ID: 35179712
You can check like this.....

 set @seta=((select Sum(oi.Quantity)
             from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)
 );
select @seta;
if @seta >0
return @seta
else
return 0;
.....you can perform the desired operations in the if and else statements...
Let me know if you want any further help...
Regards
Ramki
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35181713
poor_beggar,

The only case when a sum can result in NULL is when there are no records to sum up:
    select sum(a) from (select 1 as a union all select 2 union all  select null ) a
will result in 3
 
   select sum(a) from (select 1 as a from syscolumns where 1=0) a
will result in NULL
0
 
LVL 20

Expert Comment

by:Amitkumar Panchal
ID: 35196311
Check using NVL() function.

(select nvl(Sum(oi.Quantity),0) as available from
 OrderItems oi inner join Orders o on oi.OrderId = o.Id where (oi.OrderItemStatusId = 0 or oi.OrderItemStatusId = 1)  and oi.ProductId = 80493)


Following is the updated full query.

SELECT p.Id                                   as Id,
       p.Name,
       CustomerSku,
       p.ProductKey,
       p.MinQuantity,
       p.MaxQuantity,
       p.Stock,
       p.IsStockItem,
       p.ShowStockPrice,
       ImageThumbUrl,
       p.Teaser,
       p.Description,
       (p.Stock
          - (select nvl(Sum(oi.Quantity),0)  
           from   OrderItems oi
                    inner join Orders o
                      on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2
                    and o.OrderStatus = 3
                    and oi.ProductId = p.Id)) as ProjectedStock
FROM   ProductCategories pc
       inner join Products p
         on pc.ProductId = p.Id
WHERE  pc.CompanyId = 27
       and pc.CategoryId = 138
       and p.Archive = 0
       and p.Disable = 0
       and p.AutoDisable = 0
       and EXISTS (select *
                   from   RetailerInGroup rig
                   where  (rig.RetailerGroupId = p.RetailerGroupId
                           and rig.RetailerId = 13)
                           or p.RetailerGroupId is Null)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35196901
amit_n_panchal,

NVL is Oracle, we are talking 'bout MSSQL here.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35196936
BTW, such a subselect in the columns part usually performs bad. It is much better to use a derived table here (you could also do with CTEs if MSSQL 2005 or above):
SELECT p.Id                                   as Id,
       p.Name,
       CustomerSku,
       p.ProductKey,
       p.MinQuantity,
       p.MaxQuantity,
       p.Stock,
       p.IsStockItem,
       p.ShowStockPrice,
       ImageThumbUrl,
       p.Teaser,
       p.Description,
       (p.Stock - isnull(ordered.quantity,0)) as ProjectedStock
FROM   ProductCategories pc
join Products p
         on pc.ProductId = p.Id
left join (select oi.ProductId, Sum(oi.Quantity) as quantity
             from   OrderItems oi
             join Orders o
               on oi.OrderId = o.Id
             where  oi.OrderItemStatusId < 2 and o.OrderStatus = 3) ordered
         on ordered.ProductId = p.Id
WHERE  pc.CompanyId = 27
       and pc.CategoryId = 138
       and p.Archive = 0
       and p.Disable = 0
       and p.AutoDisable = 0
       and EXISTS (select *
                   from   RetailerInGroup rig
                   where  (rig.RetailerGroupId = p.RetailerGroupId
                           and rig.RetailerId = 13)
                           or p.RetailerGroupId is Null)

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now