使用CASE
表達式在派生表中執行主題替換。然後在它的結果做GROUP BY
:(核心ANSI SQL-99,加上F591, 「派生表」 功能)
select subject, avg(marks)
from
(
select case when Subject <> 'Maths' then 'Rest(of all subj)' else subject end,
Marks
from tablename
) dt
group by subject
沒有一個CASE
:
select Subject, avg(marks)
from
(
select Subject, marks from tablename where Subject = 'Maths'
union all
select 'Rest(of all subj)', marks from tablename where Subject <> 'Maths'
)
group by Subject
甚至更簡單,正如dnoeth所建議的那樣:
select 'Maths' as Subject, avg(marks) from tablename where Subject = 'Maths'
union all
select 'Rest(of all subj)', avg(marks) from tablename where Subject <> 'Maths'
'選擇主題,AVG(引號)聚集函數,其中受試者!= 'xxx' 的組由subject'? –
@AlexK。猜測不需要按主題使用**組 – StanislavL
使用標記dbms。 (某些產品特定答案在下面...) – jarlh