日期:2014-05-18  浏览次数:20581 次

sql 查询中 非主键不能相同的问题。帮忙解决非常感谢。
员工表结构:
id (唯一), name , organ(机构), branch(部门),ordernumber(订单)
001 张三 北京 财务 20
005 李四 北京 财务 42
007 赵六 上海 财务 56

订单大于10的,这三条都符合条件,但是要求查询结果中,同一个机构部门也不能重复。也就是张三、李四只能取其中一个,怎么去都成。例如结果是:
001 张三 北京 财务 20
007 赵六 上海 财务 56

求高手解决啊。谢谢了

------解决方案--------------------
SQL code
select t.* from tb t where ordernumber = 10 and id = (select min(id) from tb where organ = t.organ and branch = t.branch and ordernumber = 10) 

select t.* from tb t where ordernumber = 10 and id = (select max(id) from tb where organ = t.organ and branch = t.branch and ordernumber = 10) 

select t.* from tb t where ordernumber = 10 and not exists (select 1 from tb where organ = t.organ and branch = t.branch and ordernumber = 10 and id < t.id)
 
select t.* from tb t where ordernumber = 10 and not exists (select 1 from tb where organ = t.organ and branch = t.branch and ordernumber = 10 and id > t.id)

------解决方案--------------------
SQL code
select * from tb t where ordernumber = 10 and id = (select max(id) from tb where organ=t.organ and branch=t.branch and ordernumber=10)

------解决方案--------------------
探讨

我已经实现了,看错数据了,还是谢谢大家了