2013-02-15 78 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