Oracle 中与 group by 不同的 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9323973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:27:00  来源:igfitidea点击:

SQL distinct with group by in Oracle

sqloraclegroup-bydistinct

提问by pathum83

I have following SQL:

我有以下 SQL:

select 
  origin,destination,to_char(to_date(substr(ship_date,1,6),'YYMMDD'),
  'YYYY-MM-DD'),ship_date,trip_number, distinct ship_number  
from shipment a 
where 
  a.scc_code in ('xxxxx','xxxxx','xxxxx') 
  and load_status = 'S' and ship_date like '11%' 
  and shipper_id = XXXXXX
group by origin,destination,ship_date,trip_number, ship_number

When I run this SQL in Oracle it gives ORA-00936: missing expression. If I remove the distinct keyword, it runs fine. Can anybody tell me the difference between those two things?

当我在 Oracle 中运行此 SQL 时,它给出 ORA-00936: missing expression。如果我删除 distinct 关键字,它运行良好。谁能告诉我这两个东西的区别?

回答by narek.gevorgyan

Distinct keyword is for all selected columns, so you have to put it before select

Distinct 关键字用于所有选定的列,因此您必须将其放在 select 之前

select distinct 
  origin,destination,to_char(to_date(substr(ship_date,1,6),'YYMMDD'),
  'YYYY-MM-DD'),ship_date,trip_number, ship_number  
from shipment a 
where 
  a.scc_code in ('xxxxx','xxxxx','xxxxx') 
  and load_status = 'S' and ship_date like '11%' 
  and shipper_id = XXXXXX
group by origin,destination,ship_date,trip_number, ship_number