Solved

how return 0 is value is null

Posted on 2011-03-21
10
184 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

863 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

27 Experts available now in Live!

Get 1:1 Help Now