日期:2014-05-17  浏览次数:20640 次

在线等 sql联查 数据重复

SELECT RecordNo,OperationName,StartTime,EndTIme,Memo FROM Hai_Incision  
原表显示数据
0000222225 75 01.02 2012-08-29 2012-08-30 1002
0000222239 78 02.31 2012-09-19 2012-09-20 0001

SELECT a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name as ONName
 FROM Hai_Incision as a 
 left outer join operations_dict i on a.OperationName=i.data_code  
显示数据
0000222225 75 01.02 2012-08-29 2012-08-30 1002 手术1
0000222239 78 02.31 2012-09-19 2012-09-20 0001 手术2
0000222239 78 02.31 2012-09-19 2012-09-20 0001 手术2

operations_dict 的data_code不是唯一的 02.31 有两条数据
想让他显示的时候 重复数据只显示一条


------解决方案--------------------
SELECT distinct a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name as ONName
FROM Hai_Incision as a
left outer join operations_dict i on a.OperationName=i.data_code
------解决方案--------------------
SELECT distinct(RecordNo) as a ,OperationName,StartTime,EndTIme,Memo FROM Hai_Incision order by a
------解决方案--------------------
SELECT a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo,(select distinct i.data_name from operations_dict as i where i.data_code=a.OperationName)as ONName
 FROM Hai_Incision 
不知道正不正确 你试试
------解决方案--------------------
SQL code


SELECT a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name as ONName
 FROM Hai_Incision as a  
 left outer join operations_dict i on a.OperationName=i.data_code  
group by a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name

------解决方案--------------------
小改一下,希望能帮助LZ
SELECT a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name as ONName
 FROM Hai_Incision as a
where exists(select 1 from operations_dict i where i.data_code = a.OperationName)
------解决方案--------------------
如果你要取那么多列,那不存在重复的问题,如果你只要a.RecordNo,a.OperationName,那才算重复,用个distinct就可以拉
------解决方案--------------------
SELECT distinct a.RecordNo,a.OperationName,a.StartTime,a.EndTIme,a.Memo
,i.data_name as ONName
 FROM Hai_Incision as a
 left outer join operations_dict i on a.OperationName=i.data_code