求写一个sql语句(200分)

  • 主题发起人 主题发起人 newdreamer
  • 开始时间 开始时间
N

newdreamer

Unregistered / Unconfirmed
GUEST, unregistred user!
存货档案表 中有两个字段:存货代码和 供应商编码,现在要取出相同“存货代码”,不同“供应商编码”的记录来<br>例如: &nbsp;存货代码 &nbsp; &nbsp; &nbsp;供应商编码<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 002<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 003<br>&nbsp; &nbsp; &nbsp; &nbsp; 002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 004<br>&nbsp; &nbsp; &nbsp; &nbsp; 003 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 005<br>&nbsp; &nbsp; &nbsp; &nbsp; 004 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 006<br>&nbsp; &nbsp; &nbsp; &nbsp; 004 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 007<br>要得到结果:<br>&nbsp; &nbsp; &nbsp; &nbsp; 存货代码 &nbsp; &nbsp; 供应商编码<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;001<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;002<br>&nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;003<br>&nbsp; &nbsp; &nbsp; &nbsp; 004 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;006<br>&nbsp; &nbsp; &nbsp; &nbsp; 004 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;007<br>请大家帮忙,谢谢。
 
SELECT *<br>FROM table<br>WHERE (存货代码 IN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (SELECT 存货代码<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM table<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GROUP BY 存货代码<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HAVING (COUNT(存货代码) &gt; 1)))<br><br>在SQLSERVER 上验证通过
 
给分吧,大大
 
SELECT distinct a.存货代码,a.供应商编码<br>FROM table a<br>WHERE a.存货代码 IN (SELECT b.存货代码 &nbsp;FROM table b GROUP BY b.存货代码 HAVING (COUNT(b.存货代码) &gt; 1))<br>order by a.存货代码,a.供应商编码<br><br>这样应该不会重复吧
 
两种方法都是一样的
 
多人接受答案了。
 
后退
顶部