2012-12-18 82 views
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 
+1

不'SELECT DISTINCT ib.branch_no,p.weightedav,p.item_code從物理p INNER JOIN item_branch作爲IB的p.item_code = ib.item_code'做什麼你如果你不關心'on_hand'的值,它的唯一價值是不會帶回這麼多的重複項? –

+0

我仍然需要where子句,因爲這限制了我的結果,並且只獲得了那些on_hand = 0的branchnos。 – aMazing

+1

你加入了兩個表,所以無論如何,你只會得到那些branch_no的on_hand值不是0.我試過在sqlfiddle和我也得到所有branch_no當我刪除where條件。你給你的查詢演示,以便我們可以看到你的表? –

回答

0

試試這個:

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 AND ib.on_hand <> 0)