多表连接查询取消重复元祖但不能使用distinct

我是三峡移民 2016-12-26 03:59:44

select CusChange.*,Area.Area_Name,cus.Customer_Code,cus.Customer_Name,Cus.Address CusAddress,cus.Telphone CusTelphone,DicChangeType.FieldValue Change_Type,Rep.Actual_Fee Open_Cus_Price,Repcard.Actual_Fee Card_Price
from CB_CustomerChange CusChange
left join CB_Customer Cus on Cus.ID=CusChange.Customer_ID
left join CB_Area Area on Area.ID=Cus.Area_ID
left join (select * from SYS_Dictionary where FieldName='Change_Type') DicChangeType on DicChangeType.FiledExplain=CusChange.Change_Type
left join (select * from CI_ReplaceCard where Fee_Type='1') Rep on Rep.Customer_ID=Cus.ID
left join (select * from CI_ReplaceCard where Fee_Type='2') Repcard on Repcard.Customer_ID=Cus.ID
where Change_Type='1'



select distinct CusChange.*,Area.Area_Name,cus.Customer_Code,cus.Customer_Name,Cus.Address CusAddress,cus.Telphone CusTelphone,DicChangeType.FieldValue Change_Type,Rep.Actual_Fee Open_Cus_Price,Repcard.Actual_Fee Card_Price
from CB_CustomerChange CusChange
left join CB_Customer Cus on Cus.ID=CusChange.Customer_ID
left join CB_Area Area on Area.ID=Cus.Area_ID
left join (select * from SYS_Dictionary where FieldName='Change_Type') DicChangeType on DicChangeType.FiledExplain=CusChange.Change_Type
left join (select * from CI_ReplaceCard where Fee_Type='1') Rep on Rep.Customer_ID=Cus.ID
left join (select * from CI_ReplaceCard where Fee_Type='2') Repcard on Repcard.Customer_ID=Cus.ID
where Change_Type='1'
...全文
508 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiangqing 2016-12-27
  • 打赏
  • 举报
回复
不能用distinct,那就用group by
我是三峡移民 2016-12-27
  • 打赏
  • 举报
回复
问题的关键好像发现了,主表那边好像本身就是重复的,谢谢各位的解答
道素 2016-12-27
  • 打赏
  • 举报
回复
引用 6 楼 qq_34082025 的回复:
加上top 1后
第一步确定重复造成的原因,下一步就像想对策,你直接加top 会有问题,假如恰好取出的一条customer_id不等于Cus.ID呢 应该是在Repcard.Customer_ID=Cus.ID 的基础上取一条,有两种方式,一种是用

cross apply (select top 1 * from  CI_ReplaceCard as rc where rc.Fee_Type='2' and rc.Customer_ID=Cus.ID ) as Repcard

---或者是
left join (select Customer_ID,sum(Actual_Fee)/*不清楚你实际应该怎么处理*/ as Actual_Fee from  CI_ReplaceCard as rc where rc.Fee_Type='2'  group by Customer_ID) as Repcard on Repcard.Customer_ID=Cus.ID 


--另外其他Leftjoin ,如
left join (select * from SYS_Dictionary where FieldName='Change_Type') DicChangeType on DicChangeType.FiledExplain=CusChange.Change_Type

--也可以写成
left join  SYS_Dictionary as DicChangeType on FieldName='Change_Type'  AND DicChangeType.FiledExplain=CusChange.Change_Type

  • 打赏
  • 举报
回复
能用distinct 就 应该能用group by
steven8098 2016-12-26
  • 打赏
  • 举报
回复
不清楚具体表结构,从查询语句看是查询客户信息修改内容吧,查询结果有重复,一般都是外键重复引起的,如果分析不出来,把关联表的所有字段显示出来看,具体哪一个字段不一样造成了重复,这个方法比较快。
我是三峡移民 2016-12-26
  • 打赏
  • 举报
回复
加上top 1后
我是三峡移民 2016-12-26
  • 打赏
  • 举报
回复
问题在这里,如果不要那个属性查询出来的话就是正确的,如果需要的话,就需要连表,但是没有唯一关联,出来的数据是个笛卡尔积,客户又要这条数据,只能去掉重复,但是又不能用distinct
  • 打赏
  • 举报
回复
有些东西 在 某张表不唯一造成的吧
道素 2016-12-26
  • 打赏
  • 举报
回复
首先你先弄清楚,是什么原因造成的重复数据,是主表CB_CustomerChange本身有重复数据,还是因为关联的子表关联出多行造成 按照你的数据是关联表造成的,那么你要确定哪个表关联出多少,然后想办法让子表只返回一行 比如如果不考虑业务逻辑CI_ReplaceCard,SYS_Dictionary中都加上top 1看看返回的记录数是不是有变化
我是三峡移民 2016-12-26
  • 打赏
  • 举报
回复
使用dictinct后得到的,期望得到的是这种数据,不能使用group by去做,只能靠连表筛选,只能使用where
我是三峡移民 2016-12-26
  • 打赏
  • 举报
回复

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧