虹 :
だから何イムをやろうとすると、このですが、これは動作しません。
Select count(staffno)as total_staff,avg(salary),branchno
From staff
Where total_staff > 1
Group by branchno
ここでもう一つの例は以下のとおりです。
Select salary,salary*1.015 as proposed_salary
From staff
Where proposed_salary > 50000
GMB:
あなたは(集計式を使用することはできませんsum()
、count()
で、...)where
クエリの句。この句が評価された前の行がで一緒にグループ化されているgroup by
句、その凝集体はまだ利用できません。
SQLでは、それはの目的だhaving
句:
select count(staffno) as total_staff, avg(salary), branchno
from staff
having count(staffno) > 1 -- MySQL also allows "total_staff > 1"
group by branchno
第二のクエリのとおりに定義されたエイリアスselect
句はで利用できないwhere
句のいずれか(上記と同じ理由のために)。あなたは、表現を繰り返すことができます:
select salary, salary * 1.015 as proposed_salary
from staff
where salary * 1.015 > 50000
それとも、派生テーブル(CTEまたはサブクエリ)を使用することができます。
select *
from (
select salary, salary * 1.015 as proposed_salary
from staff
) t
where proposed_salary > 5000