Solved

how return 0 is value is null

Posted on 2011-03-21
10
185 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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

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 21

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 69

Expert Comment

by:Qlemo
ID: 35196901
amit_n_panchal,

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Make Temp Table Query Faster 5 40
MS SQL with ODBC 5 34
SQL Server 2012 r2 - Sum totals 2 22
Return 0 on SQL count 24 28
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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

813 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

17 Experts available now in Live!

Get 1:1 Help Now