0
我有一个SQL是为下:SQL查询援助
SELECT ib.branch_no,
ib.on_hand,
p.weightedav,
p.item_code,
FROM physical p
INNER JOIN
item_branch as ib on p.item_code = ib.item_code
WHERE ib.on_hand <> 0
有此SQL只返回branch_no on_hand <> 0
我试图让所有的branch_nos不论on_hand字段,但仍使用where on_hand子句。
把on_hand子句带走解决了我的问题,但给我大量的不需要的行与0。
我正在使用SQL SERVER 2008 R2。
在此先感谢您的任何指导。如果我错过了任何信息,请道歉。
------------------------------------------ ENTIRE SQL QUERY(已更新)---------------------------------------------
select
ib.branch_no,
p.weighted_av,
p.item_code,
p.x_value,
p.y_value,
ib.on_hand,
p.on_hand as PhysicalOH,
ip.price,
i.item_code as StyleCode,
i.description,
i.cat1,
i.cat2,
i.cat3,
i.cat4,
np.is_style_yn,
si.supplier_code ,
ysv.sort as YSort
from physical as p
left outer JOIN
item_branch as ib on p.item_code = ib.item_code -- and ib.on_hand <> 0
INNER JOIN
item_price as ip on p.item_code = ip.item_code and ip.price_type = 'P1'
INNER JOIN
style_values as sv on p.style_code = sv.style_code and p.x_value = sv.value
INNER JOIN
style_values as ysv on p.style_code = ysv.style_code and p.y_value = ysv.value and ysv.axis = 'Y'
INNER JOIN
ITEM as i on p.style_code = i.item_code
INNER JOIN
NON_PHYSICAL as np ON i.item_code = np.item_code and np.is_style_yn = 1
INNER JOIN
supplier_item as si ON i.item_code = si.item_code and si.pref_supp_no = 1
where --ib.on_hand <> 0 and
sv.axis = 'X' and
i.item_code in
(SELECT ITEM.item_code
FROM ITEM
INNER JOIN
NON_PHYSICAL ON ITEM.item_code = NON_PHYSICAL.item_code
LEFT JOIN
supplier_item ON Item.item_code = supplier_item.item_code and pref_supp_no = 1
WHERE NON_PHYSICAL.is_style_yn = 1 and ITEM.cat1 = 'Verge Sportswear Ltd')
order by
si.supplier_code,
i.cat4,
i.cat3,
i.cat2,
i.cat1,
sv.sort
不'SELECT DISTINCT ib.branch_no,p.weightedav,p.item_code从物理p INNER JOIN item_branch作为IB的p.item_code = ib.item_code'做什么你如果你不关心'on_hand'的值,它的唯一价值是不会带回这么多的重复项? –
我仍然需要where子句,因为这限制了我的结果,并且只获得了那些on_hand = 0的branchnos。 – aMazing
你加入了两个表,所以无论如何,你只会得到那些branch_no的on_hand值不是0.我试过在sqlfiddle和我也得到所有branch_no当我删除where条件。你给你的查询演示,以便我们可以看到你的表? –