簡體   English   中英

Oracle SQL連接三個表並按列分組

[英]Oracle SQL join three tables and group by column

我有三個表,我想查詢一個選擇教師姓名和每個教師保留的班級數量的信息。

老師:

| idt | name |

類:

| idc | name |

保留:

| idc | idt |

我的查詢:

select
  t.name, count(distinct(r.idc))
  from
  teacher t
  join 
  reserve r
  on
  r.idt = t.idt
  join
  class c
  on
  c.idc = r.idc
  group by r.idc

當我運行此命令時,出現以下錯誤: not a group by expression.

group by子句需要包含select語句中的所有非聚合列; 在您的情況下,應為t.name 另外, distinct不是函數而是關鍵字,並且不應帶有括號。

select
  t.name, 
  count(distinct r.idc) as number_of_classes
from
  teacher t
join 
  reserve r on r.idt = t.idt
join
  class c on c.idc = r.idc
group by 
  t.name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM