UPDをネストされています。ああ、市〜!私はちょうどOracleに関連する質問を述べました。私はrank()
関数の構文を知らないが、私はそれが本当にSQL Serverに近いべきだと思う。
ここにある:あなたが使用している[DBMS](https://en.wikipedia.org/wiki/DBMS)
;with Department(DEPARTMENT_ID, DEPARTMENT_NAME) as (
select 1, 'first' union all
select 2, 'second' union all
select 3, 'third'
)
, Student(STUDENT_ID, DEPARTMENT_ID) as (
select 1, 1 union all
select 2, 2 union all
select 3, 2 union all
select 4, 2 union all
select 5, 3 union all
select 6, 3 union all
select 7, 3
)
, DepOrdered as (
select
d.DEPARTMENT_ID,
d.DEPARTMENT_NAME,
s.StudentCnt,
-- rank departments by the number of students
rank() over (order by s.StudentCnt desc) as Rnk
from Department d
cross apply (
-- for every department count its students
select
count(s.STUDENT_ID) StudentCnt
from Student s
where
d.DEPARTMENT_ID = s.DEPARTMENT_ID
) s
)
select
DEPARTMENT_ID,
DEPARTMENT_NAME,
StudentCnt
from DepOrdered
where
-- Rnk = 1 would have all departments with max number of students
Rnk > 1
? **正確な**エラーメッセージ(** [編集] **あなたの質問 - ** **投稿コードやコメントに追加情報はありません) –
私はSUMの代わりにCOUNTを使用する必要があると思います質問の一部を持っている –
あなたは遭遇した問題(エラーメッセージ)を投稿してください。 – Susang