SQL与Oracle中的group by区别开来

时间:2012-02-17 06:39:53

标签: sql oracle group-by distinct

我有以下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

当我在Oracle中运行此SQL时,它会提供ORA-00936:缺少表达式。如果我删除distinct关键字,它运行正常。谁能告诉我这两件事之间的区别呢?

1 个答案:

答案 0 :(得分:14)

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