2013-02-15 46 views
0
  1. 列表項

我試圖連接兩個表濾除具有唯一值的ITEM_ID,並顯示任何 ITEM_ID那包含相同的值。我使用的SQL語句似乎只有一半的工作,除了它返回所有我認爲我有這個數字,但我猜不是。組通過柱具有COUNT在單個列(列)> 1

此聲明拉動所有內容。

--join two tables filtering out and item_id that has a unique value and displaying any 
--item_id that contains the same value 

SELECT inv_bin.bin, inv_bin.quantity, inv_bin.inv_bin_uid, inv_bin.inv_mast_uid, 
     inv_mast.inv_mast_uid,inv_mast.item_desc, inv_mast.item_id 
FROM inv_bin left join inv_mast on inv_bin.inv_mast_uid = inv_mast.inv_mast_uid 
WHERE inv_mast.item_id in (SELECT item_id from inv_mast 
          GROUP BY item_id HAVING COUNT (item_id) >= 1) 
     AND inv_bin.bin not like 'rec' 
     AND inv_bin.bin not like 'DEFBIN' 
     AND inv_bin.bin not like 'DEFAULTBIN' 
ORDER BY inv_mast.item_id; 

但是,如果我從Group By item_id Having COUNT (item_id) >= 1)刪除'='那麼該查詢返回任何內容。我知道我在ITEM_ID列數據是一樣的:

bin  item_id 
07C-C15 002-09-121 
Z07-OS  002-09-121 

反正能有人告訴我,我要去的地方錯了這一點,在我看來,這期運用> 1將顯示每一件事的ITEM_ID與相同的價值。

感謝, 佈雷特

bin   quantity inv_bin_uid inv_mast_uid inv_mast_uid item_id 
07C-C15  0   135   70    70    002-09-121 
Z07-OS  10   130277  70    70    002-09-121 
04C-B21  0   354   289   289   032-36-26 
04C-B04  0   356   291   291   032-38-26 
02A-B01  2   101   48    48    5-40050720L 
Z29-SKID00 0   117   48    48    5-40050720L 

這裏是產生期望reults成品聲明。

/*join two tables filtering out and item_id that has a unique value and displaying any 
item_id that contains the same value */ 

SELECT inv_bin.bin, inv_bin.quantity, inv_bin.inv_bin_uid, inv_bin.inv_mast_uid, 
inv_mast.item_desc, inv_mast.item_id 

from inv_bin left join inv_mast on inv_bin.inv_mast_uid = inv_mast.inv_mast_uid 

where inv_bin.inv_mast_uid in ( 
SELECT inv_mast_uid FROM inv_bin 
WHERE inv_bin.bin NOT IN ('REC','DEFBIN','DEFAULTBIN') 
GROUP BY inv_mast_uid HAVING COUNT(inv_mast_uid)>1) 

and inv_bin.bin not like 'rec' 
and inv_bin.bin not like 'defbin' 
and inv_bin.bin not like 'Defaulbin' 
/*look up filtering out ids based on not like statements*/ 

ORDER BY inv_bin.inv_mast_uid; 

再次感謝您的所有幫助。

+1

我認爲一些示例數據會有所幫助。 – 2013-02-15 19:00:39

+0

你確定你要違反正確的數據庫嗎?這應該工作... – xandercoded 2013-02-15 19:06:07

+1

註釋掉你的'AND inv_bin.bin不喜歡'rec' AND inv_bin.bin不喜歡'DEFBIN' AND inv_bin.bin不喜歡'DEFAULTBIN''和看? – Kaf 2013-02-15 19:06:27

回答

1

這不是一個答案,只是一個問題/評論:

不知道你的數據庫模式的更多細節,我如果你只是使用了錯誤的表你的小組by語句疑惑?

如果你的「inv_mast」表只包含唯一的「item_id」,那麼你永遠不會得到一個高於該特定表的值。

上的ID嘗試計數的「inv_bin」 - 表代替:

SELECT item_id 
FROM inv_bin 
GROUP BY item_id HAVING COUNT(item_id) > 1 
+0

你是絕對正確的瘦我需要計算看起來像inv_mast_uid – Zajac 2013-02-15 20:04:26

+0

它越來越近 – Zajac 2013-02-15 20:09:58

+0

看來,因爲和inv_bin.bin不喜歡'rec' 和inv_bin.bin不像'defbin' 和inv_bin。bin不喜歡'Defaulbin' – Zajac 2013-02-15 20:14:48