作连接多张表后,有很多重复记录,如何筛选,去掉重复记录
通过多张表连接查询,找出想要的记录,可是插叙结果中有很多重复记录
下面是我的查询语句,有点复杂,希望各位高手耐心看一下,帮我想出解决的办法
select transHeader.transnumber,transHeader.transNumberReal, -----运单流水号,公路运输单的单号
transHeader.transType,--运单类型
case when transHeader.TrainNumber is null then '(公路) ' + transType1.typename else '(铁路) ' + transType1.typename end as TransMode, ----运输类型,
trainNumber,CarriageNumber, -------车次,机次位
replace(convert(varchar(16),transHeader.DepartTime,120), '-0 ', '- ') as DepartTime,-----发车时间
transHeader.StartAddress,transHeader.EndAddress, -----对应的始发机构,终到机构
typelist1.typelistdesc1 as TailOrganization, ---运单运作机构
typelist2.typelistdesc1 as SignOrganization, ----运单签收机构
round(convert(float,isNull(ShipQty,0)),0) as ShipQty,round(convert(float,isNull(ShipWeight,0)),3) as ShipWeight,round(convert(float,isNull(ShipVolume,0)),3) as ShipVolume, ----件数,重量,体积
StartTime, -----开单时间
case when OperationMode= 'JTL ' and CarrierPickup=1 and CarrierDispatch=0 then '门到站 ' when OperationMode= 'JTL ' and CarrierDispatch=1 and CarrierPickup=0 then '站到门 '
when OperationMode= 'JTL ' and CarrierDispatch=1 and CarrierPickup=1 then '门到门 ' when OperationMode= 'JTL ' and CarrierDispatch=0 and CarrierPickup=0 then '站到站 '
else '非行邮门到门 ' end as 运作方式,
case transStatus when 5 then isnull(typelist7.typelistdesc1, ' ')+ '运输中 ' when 3 then isnull(typelist4.typelistdesc1, ' ')+ '已发送 '
when 7 then isnull(typelist5.typelistdesc1, ' ')+ '已接收 ' when 9 then isnull(typelist6.typelistdesc1, ' ')+ '已签收 ' else ' ' end as transStatus,
transHeader.Remark1,-----备注
transHeader.vendorName, ---供应商
case when replace(convert(varchar(10),transHeader.DeliverTime,120), '-0 ', '- ')= '1900-1-1 ' then ' '
else replace(convert(varchar(16),transHeader.DeliverTime,120), '-0 ', '- ') end as DeliverTime, ---派发时间
transHeader.FourgonNumber,---车厢号
transHeader.APayable,transHeader.realExpense,----应付,实付
replace(convert(varchar(16),transHeader.closeTime,120), '-0 ', '- ') as closeTime, -----签收时间
case when transHeader.APayable-transHeader.realExpense> 0 then ' <font color=red> '+convert(varchar(20),transHeader.APayable-transHeader.realExpense)+ ' </font> '
else convert(varchar(20),transHeader.APayable-transHeader.realExpense) end as notAPayable,-----未付红色显示
-- //滞留类型
case when transHeader.TrainNumber is null and transType1.typename= '基地提货 ' and transHeader.AddWho is null then '未提货 '
when transHeader.TrainNumber is null and transType1.typename= '基地提货 ' and transHeader.signWho is null then '提货未签收 '
when transHeader.TrainNumber is null and transType1.typename= '基地送货 ' and transHeader.AddWho is null then '未送货 '
when transHeader.TrainNumber is null and transType1.typename= '基地送货 ' and transHeader.signWho is null then '送货未签收 '
when transHeader.TrainNumber is null and transType1.typename= '公路站到站 ' and transHeader.AddWho is null then '未发运 '
when transHeader.TrainNumber is null and transType1.typename= '公路站到站 ' and transHeader.AddWho is not null and transHeader.sendwho is null then '公路运单未发送 '
when transHeader.TrainNumber is null and transType1.typename= '公路站到站 ' and transHeader.AddWho is not null and transHeader.sendwho is not null and transHeader.receiptwho is null then '公路运单未接收 '
when transHeader.TrainNumber is null and transType1.typename= '公路站到站 ' and transHeader.signWho is null and transHeader.AddWho is not null and transHeader.sendwho is not null and transHeader.receiptwho is not null then '未签收 '
when transHeader.TrainNumber is not null and transHeader.AddWho is null then '装车单未录入 '
when transHeader.TrainNumber is not null and transHeader.receiptwho is null then '装车单未接收 '
when transHeader.TrainNumber is not null and transHeader.sendwho is null then '装车单未发送 '
when transHeader.TrainNumber is not null and transHeader.signWho is null then '卸车单未签收 ' else ' ' end as StopType,--滞留类型,
' ' as orderArea -----订单位置区域表
from transHeader
left join typelist as typelist1 on Typelist1.TypelistCode=transHeader.TailOrganization and Typelist1.TypeCode= 'Organization '
left join typelist as typelist2 on Typelist2.TypelistCode=transHeader.SignOrganization and Typelist2.TypeCode= 'Organization '
left join typelist as typelist3 on Typelist3.TypelistId=transHeader.transStatus and Typelist3.TypeCode= 'TransStatus '
left join transType as transType1 on transHeader.transType=transType1.typecode
left join [user] as userSend on userSend.userid=transHeader.sendwho
left join typelist as typelist4 on typelist4.typecode= 'organization ' and typelist4.typelistcode=userSend.userBelong
left join [user] as userReceipt on userReceipt.userid=transHeader.receiptwho
left join typelist as typelist5 on typelist5.typecode= 'organization ' and typelist5.typelistcode=userReceipt.userBelong
left join [user] as userSign on userSign.userid=transHeader.signWho
left join typelist as typelist6 on typelist6.typecode= 'organization ' and typelist6.typelistcode=userSign.userBelong
left join [user] as userProcess on userProcess.userid=transHeader.modifyWho
left join typelist as typelist7 on typelist7.typecode= 'organization ' and typelist7.typelistcode=userProcess.userBelong
--left join OrderProcess on OrderProcess.TransNumber=transHeader.TransNumber
--left join OrderHeader on OrderHeader.OrderNumber=OrderProcess.OrderNumber
inner join transdetail on transdetail.TransNumber=transHeader.TransNumber
inner join OrderHeader on OrderHeader.OrderNumber=transdetail.OrderNumber
where 1=1
order by transHeader.starttime
用到表transHeader,transdetail,OrderHeader
语句现在可以执行,可是执行后,同一个流水号(transnumber transHeader主键)对应的记录有很多条,每个里流水号对应记录我只要一条就可以了。出现这种情况我想是因为左连接后,存在不同字段。怎么解决阿,冲这些记录中去掉重复的。
我用了distinct,只能去掉部分重复的。
很急,在线等,求助各位高手,不吝赐教。谢谢啦。
[解决办法]
加我MSN,这样太慢 MSN:brother2605@hotmail.com