distinct 和 order by 的 矛盾

本文介绍了一种使用SQL语句去除查询结果中重复记录的方法。通过对比不同SQL语句的效果,展示如何利用GROUP BY结合聚合函数MAX来正确地消除重复记录,从而得到每个cont_id对应的最高点击数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select distinct cont_id from cms_contentiteminfo t order by cast (t.click as int) desc

上面这句报错ORA-1791,not a SELECTED expression

select distinct cont_id ,cast (t.click as int) as cfrom cms_contentiteminfo t order by cast (t.click as int) desc

查出的结果是有重复记录的

cont_id c

1853371517
1856391003
185586985
185337967
185641937
185337899
185639832
185641832
185641727
185283649
185144605
185586588
185639564
185216547
185586508
185283492
185144422
185283421
185216378
185217369
185144346
185626309
185217303
192278254

修改为

select distinct cont_id,max(cast (t.click as int)) c from cms_contentiteminfo t group by cont_id order by c desc

查询结果

cont_id c

1853371517
1856391003
185586985
185641937
185283649
185144605
185216547
185217369
185626309
192278254
185145234
191809214
185286191
190484190
185642190
190487180
185287171
190486162
192483146
189151138
191253118
177607117
192704116
185140115

cont-id重复的记录就可以去掉了

select SUM(t1.Issue_Num) Issue_Num, t1.Short_Name, t1.Subsidiary, dateadd(month, datediff(month, 0,t1.Apply_Time), 0) as Start_Date, dateadd(month, datediff(month, 0, dateadd(month, 1, t1.Apply_Time)), -1) as End_Date, cast("Year" as varchar)+'年'+cast("Month" as varchar)+'月报' as '报告期', case when t2.Issue_NumT = 0 then 0 else (t1.Issue_Num - t2.Issue_NumT)/ t2.Issue_NumT *100 end as YOY_Number, '同比' as YOY, t1.SignY, null Issue_Reson, distinct t1.Responsibility_Judgment from (select Complaint.Status, Order_Num, Apply_Time, count(distinct ComplaintList.Responsibility_Judgment) as Issue_Num, ComplaintList.Subsidiary, ComplaintList.Responsibility_Judgment, ComplaintList.SignY, sub.Short_Name, YEAR(Complaint.Apply_Time) Year, MONTH(Complaint.Apply_Time) Month from [dbo].[IMP_MidDataCustomerComplaint] as Complaint left join [dbo].[IMP_MidDataComplaintList] as ComplaintList on Complaint.Serve_Order = ComplaintList.Serve_Order left join Mid_witsky.dbo.Base_Subsidiary as sub on sub.Code = ComplaintList.Subsidiary where --ComplaintList.Responsibility_Judgment = 'DT001' --and Complaint.Status = 'OP002' Complaint.Del_Flag = 0 and YEAR(Apply_Time) = 2025 and MONTH(Apply_Time) = 2 --and ComplaintList.Responsibility_Judgment = '' and (Subsidiary is NULL or Subsidiary = '') group by Complaint.Status, Order_Num, Apply_Time, ComplaintList.Subsidiary, ComplaintList.Responsibility_Judgment, ComplaintList.SignY, sub.Short_Name, YEAR(Complaint.Apply_Time) , MONTH(Complaint.Apply_Time))t1 left join (select Complaint.Status as StatusT, Order_Num as Order_NumT, Apply_Time as Apply_TimeT, count(distinct Complaint.Serve_Order) as Issue_NumT, ComplaintList.Subsidiary as SubsidiaryT, ComplaintList.Responsibility_Judgment as RJ, ComplaintList.SignY as SignYT, sub.Short_Name as NameT, YEAR(Complaint.Apply_Time) YearT, MONTH(Complaint.Apply_Time) MonthT from [dbo].[IMP_MidDataCustomerComplaint] as Complaint left join [dbo].[IMP_MidDataComplaintList] as ComplaintList on Complaint.Serve_Order = ComplaintList.Serve_Order left join Mid_witsky.dbo.Base_Subsidiary as sub on sub.Code = ComplaintList.Subsidiary where --ComplaintList.Responsibility_Judgment = 'DT001' --and Complaint.Status = 'OP002' Complaint.Del_Flag = 0 --and ComplaintList.Responsibility_Judgment = '' and Subsidiary is NULL or Subsidiary = '' group by Complaint.Status, Order_Num, Apply_Time, ComplaintList.Subsidiary, ComplaintList.Responsibility_Judgment, ComplaintList.SignY, sub.Short_Name, YEAR(Complaint.Apply_Time) , MONTH(Complaint.Apply_Time))t2 on t1.year - 1 = t2.YearT and t1.month = t2.MonthT and t1.Subsidiary = t2.SubsidiaryT where t1.Month in('2') and t1.SignY = 'CA002' and t1.Year > ( case when '3年' = '3年' then cast('2025' as INTEGER) -3 when '3年' = '5年' then cast('2025' as INTEGER) -5 when '3年' = '10年' then cast('2025' as INTEGER) -10 when '3年' = '15年' then cast('2025' as INTEGER) -15 else cast('2025' as INTEGER) -20 end ) group by t1.Short_Name, t1.Subsidiary, dateadd(month, datediff(month, 0,t1.Apply_Time), 0), dateadd(month, datediff(month, 0, dateadd(month, 1, t1.Apply_Time)), -1) , cast("Year" as varchar)+'年'+cast("Month" as varchar)+'月报' , case when t2.Issue_NumT = 0 then 0 else (t1.Issue_Num - t2.Issue_NumT)/ t2.Issue_NumT *100 end , t1.SignY, t1.Year, t1.Month, t1.Responsibility_Judgment order by Year desc,Month desc 为什么会在添加distinct t1.Responsibility_Judgment后报错
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值