Ideas to accomplish desired results in my query

时间:2016-10-20 18:58:46

标签: sql

I need a query where I can get the results from column qty hand, qty allocated and qty backordered.

My problem is that I have this query and it does not show me qty on hand that is equal to 0 and any value in the other 2 columns.

select 
 item_location_view.qty_on_hand AS 'Qty on Hand', 
 item_location_view.qty_allocated AS 'Qty Allocated', 
 item_location_view.qty_backordered AS 'Qty Backordered'
FROM 
  dbo.inv_mast inv_mast, 
  dbo.item_location_view item_location_view    
WHERE 
  inv_mast.item_id = item_location_view.item_id AND
  ((item_location_view.qty_on_hand>=0) AND 
  (item_location_view.qty_allocated>0) AND
  (item_location_view.qty_backordered>0))

Thank you all

1 个答案:

答案 0 :(得分:0)

Looks like you need change AND for OR

select     
     ilv.qty_on_hand AS 'Qty on Hand', 
     ilv.qty_allocated AS 'Qty Allocated', 
     ilv.qty_backordered AS 'Qty Backordered'    
FROM dbo.inv_mast inv
JOIN dbo.item_location_view ilv
ON  inv.item_id = ilv.item_id 
WHERE (
            (item_location_view.qty_on_hand>=0)
         OR (item_location_view.qty_allocated>0) 
         OR (item_location_view.qty_backordered>0)
      )

2nd option maybe

WHERE  item_location_view.qty_on_hand >= 0 
  AND (  
            item_location_view.qty_allocated > 0
        OR  item_location_view.qty_backordered > 0
      )