SQLServer 表连接时使用top 1 去除重复数据
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 去除重复数据的更多相关文章
- C# List集合去除重复数据
实例如下: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; ...
- oracle 相关查询和非相关查询,oracle 去除重复数据,以及oracle的分页查询!
一.oracle中的相关查询?和非相关查询? 二.oracle去除重复数据 1. 2. 3.oracle 实现分页? 利用rownum的唯一性,和子查询,将rownum从伪列变成实际列!
- SQlserver表连接
连接是两元运算,可以对两个或多个表进行查询,结果通常是含有参加连接运算的两个表或多个表的指定列的表. 在T-SQL中,连接查询有两类: 第一类:符合SQL标准的连接谓词表示形式: 第二类:T-SQL扩 ...
- SQL Server中多表连接时驱动顺序对性能的影响
本文出处:http://www.cnblogs.com/wy123/p/7106861.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...
- 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
标题: 连接到服务器 ------------------------------ 无法连接到 (local). ------------------------------ 其他信息: 在与 SQL ...
- SqlServer 在查询结果中如何过滤掉重复数据
问题背景 在一个多表查询的sql中正常情况下产生的数据都是唯一的,但因为数据库中存在错误(某张表中存在相同的外键ID)导致我这边查询出来的数据就会有重复的问题 下面结果集中UserID:15834存在 ...
- Java实现数组去除重复数据的方法详解
一.用List集合实现 int[] str = {5, 6, 6, 6, 8, 8, 7,4}; List<Integer> list = new ArrayList<Integer ...
- JAVA数组去除重复数据
一.用List集合实现 , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str. ...
- oracle去除重复数据与oracle分页
一.去除oracle中重复数据,可以使用rowid列,rowid列是一个伪列,该列在数据库中灭一个表中都有,但是我们查询数据库的时候,默认都没有给我们返回这一列,这一列用来区分数据库中的每一行时间,可 ...
随机推荐
- 题解 P3258 【[JLOI2014]松鼠的新家】(From luoguBlog)
唯一能得分的题也被自己搞炸了,好的. 考场上读完题基本认定和lca脱不了干系,想了一会确认是树剖. 那么问题来了,考前一节课刚发现自己之前打的树剖是错的. 而且就算是错的我也没信心考场调出来. 于是打 ...
- position:搜索框显示历史浏览记录
absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", "righ ...
- Day 25 面向对象
面向对象基础 面向对象编程 面向过程编程:类似于工厂的流水线 优点:逻辑清晰 缺点:扩展性差 面向对象编程:核心是对象二字,对象属性和方法的集合体,面向对象编程就是一堆对象交互 优点:扩展性强 缺点: ...
- BOS工具之BOS应用框架
大纲: 应用框架概述,bos应用框架总体,bos应用框架详细设计,代码结构以及常用应用,开发常用接口 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象组件及组件实例间交互的 ...
- 【剑指Offer】6、旋转数组的最小数字
题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5 ...
- UNIX C 文件权限 Part2_day01
1.文件访问测试 测试调用进程对指定文件是否拥有足够的访问权限 #include <unistd.h> int access(const char* pathname,int mode); ...
- WPF通过鼠标滑轮缩放显示图片
如果你使用WinForm比较难实现通过滚动鼠标滑轮来对图片进行缩放显示,那么,你应该考虑一下使用WPF,既然是下一代Windows客户端开发平台,明显是有一定优势的,不然,MS是吃饱了撑着. 首先 ...
- 【hihocoder 1473】小Ho的强迫症
[题目链接]:http://hihocoder.com/problemset/problem/1473 [题意] [题解] 假定初始为在在0位置(相对它左边那条线); 则考虑; 多少步之后,人又能这到 ...
- hdu 2089 数位dp入门题
#include<stdio.h> //dp[i][0]代表不存在不吉利数字 //dp[i][1]代表不存在不吉利数字但是以2开头 //dp[i][2]代表存在不吉利数字 #define ...
- 开源分布式MySQL中间件探究与应用 dba+