left join SM_SOLine soline on soline.SO=so.ID and soline.DocLineNo=(select MAX(DocLineNo) from SM_SOLine where so=so.ID)

create table #test8
(
  id int,
  name varchar(50)
)

drop table #test9
create table #test9
(
  id int,
  name varchar(50)
)

insert into #test8
select 1,'aaa' union all
select 2,'bbb' union all
select 3,'ccc' union all
select 4,'ddd'

insert into #test9
select 3,'abc' union all
select 3,'ddd' union all
select 3,'eee' union all
select 3,'eee' union all
select 2,'ccc' union all
select 1,'ghd' union all
select 7,'hgd'

select A.id,A.name,B.name from #test8 A
inner join #test9 B on A.ID=B.ID
where not exists(select 1 from #test9 where id=B.id and name>B.name)

此处B表的Name字段必须是在B表中不重复的某字段,不然,会出现重复结果

use TestDB
drop table test8
create table test8
(
      id int,
      name1 varchar(50),
      name2 varchar(50),
      name3 varchar(50)
)

drop table test9
create table test9
(
      id int,
      name1 varchar(50),
      name2 varchar(50),
      name3 varchar(50)

)

insert into test8 --ItemMaster
select  1,'a','aaa','111' union all
select  2,'b','bbb','222' union all
select  3,'c','ccc','333' union all
select  4,'d','ddd','444' union all
select  5,'e','eee','555' union all
select  6,'f','fff','666' union all
select  7,'g','ggg','777' union all
select  8,'h','hhh','888'

insert into test9 --Gprice
select  1,'a','aaa','567' union all
select  2,'a','aaa','567' union all
select  3,'i','ccc','641' union all
select  4,'c','ccc','981' union all
select  5,'e','eee','126' union all
select  6,'f','fff','873' union all
select  7,'c','ccc','946' union all
select  8,'h','ddd','767'

--insert into test9 --Gprice
--select  1,'a','aaa','A' union all
--select  2,'a','aaa','A' union all
--select  3,'i','ccc','B' union all
--select  4,'c','ccc','C' union all
--select  5,'e','eee','D' union all
--select  6,'f','fff','E' union all
--select  7,'c','ccc','F' union all
--select  8,'h','ddd','G'

/*
  1,a  --
  3,c  --
  5,e
  6,f
*/

此写法会导致test8中的ID重复

select A.id,A.name1,A.name3,B.name2,B.name3 B_name3 from test8 A
inner join test9 B on A.name1=B.name1 and A.name2=B.name2

使用此方法可以解决问题:

select A.id,A.name1,A.name3,B.name2, from test9 where id=B.id and name3>B.name3)
group by A.id,A.name1,A.name3,B.name2

PS. B表里的name3可以是test8表里的除关键字段以外任意一个前提是有值的字段,

可以是整型,也可是字符串,都可显示正常,如下列结果

SQLServer 表连接时使用top 1 去除重复数据的更多相关文章

  1. C# List集合去除重复数据

    实例如下: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; ...

  2. oracle 相关查询和非相关查询,oracle 去除重复数据,以及oracle的分页查询!

    一.oracle中的相关查询?和非相关查询? 二.oracle去除重复数据 1. 2. 3.oracle 实现分页? 利用rownum的唯一性,和子查询,将rownum从伪列变成实际列!

  3. SQlserver表连接

    连接是两元运算,可以对两个或多个表进行查询,结果通常是含有参加连接运算的两个表或多个表的指定列的表. 在T-SQL中,连接查询有两类: 第一类:符合SQL标准的连接谓词表示形式: 第二类:T-SQL扩 ...

  4. SQL Server中多表连接时驱动顺序对性能的影响

    本文出处:http://www.cnblogs.com/wy123/p/7106861.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...

  5. 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误

    标题: 连接到服务器 ------------------------------ 无法连接到 (local). ------------------------------ 其他信息: 在与 SQL ...

  6. SqlServer 在查询结果中如何过滤掉重复数据

    问题背景 在一个多表查询的sql中正常情况下产生的数据都是唯一的,但因为数据库中存在错误(某张表中存在相同的外键ID)导致我这边查询出来的数据就会有重复的问题 下面结果集中UserID:15834存在 ...

  7. Java实现数组去除重复数据的方法详解

    一.用List集合实现 int[] str = {5, 6, 6, 6, 8, 8, 7,4}; List<Integer> list = new ArrayList<Integer ...

  8. JAVA数组去除重复数据

    一.用List集合实现   , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str. ...

  9. oracle去除重复数据与oracle分页

    一.去除oracle中重复数据,可以使用rowid列,rowid列是一个伪列,该列在数据库中灭一个表中都有,但是我们查询数据库的时候,默认都没有给我们返回这一列,这一列用来区分数据库中的每一行时间,可 ...

随机推荐

  1. 09--c++ 类的继承与派生

    c++ 类的继承与派生   一.基本概念 1.类的继承,是新的类从已有类那里得到已有的特性.或从已有类产生新类的过程就是类的派生.原有的类称为基类或父类,产生的新类称为派生类或子类.   2.派生类的 ...

  2. Altova MapForce AMS/ACI/ISF自定义模板

    目前为止,我在百度上得到关于MapForce的信息少之又少,所以把自己的一些经验写下来,与大家分享. 如果要生成xml的话,就可以直接创建xml架构当作数据的目标文件. 以下是我做的AMS&A ...

  3. 在滑动3D模型(Cube)时,手指点击进入相应的某一部分

    public void OnClickRay() { Ray ray = new Ray(); RaycastHit shootHit; ray.origin = cameras.transform. ...

  4. 基于MATLAB的语音信号处理

    一.图形界面设计 1.新建GUI界面 2.新建空白页 3.命名为"yydsp",打开界面 4.拖放控件 5.按预定功能修改界面 6.填写Callback函数 未填写前的代码: fu ...

  5. linux邮件服务

    linux本地常见邮件服务有: Centos5:默认使用sendmail邮件服务,开启方式/etc/init.d/sedmail start Centos6:默认使用postfix邮件服务,开启方式/ ...

  6. Redis 应用场景【商品拼团抢购】

    使用到 redis-list llen 命令:获取列表的长度,如果列表key不存在那么也是返回0,如果给的key不是一个列表类型会返回一个错误 lpush命令:将值插入到列表头部,如果 key 不存在 ...

  7. js对对象的校验技巧,随时更新

    js中,字符串长度用length. 若不确定一个Map里,是否存在某个对象,则用underfind 去校验

  8. Easyphp让其他电脑访问

    1.将httpd.conf中的Listen 127.0.0.1:80,修改为Listen 80. 2.重启

  9. Spring MVC灵活控制返回json的值(自定义过滤字段)

    在使用spring MVC开发过程中,为了提高项目执行效率,所以在一些外键字段的实体中会注解”@ManyToOne(fetch = FetchType.LAZY)”以实现延迟加载的效果. 但是,在使用 ...

  10. 第一次训练 密码:acmore

    #include <cstdio> #include <cstring> #define M 100010 #define INF 0x7FFFFFFF #define Min ...