我试图写在下面的数据的查询,这给了我多少类型的学校在每个school_leaT-SQL子查询用的GroupBy
所以对于旺兹沃思我会
4 Secondary Schools
0 Foundation Schools
0 Community Schools
0 Primary Schools
0 Other
0 Academdy
对于斯塔福德我会
2 Secondary School
1 Community School
2 Primary Schools
0 Other
0 Academdy
我到目前为止是这样的,但它不是p产生正确的数字,并包含重复学校利尔的
任何人都可以提醒我缺少什么?
感谢
select school_lea,
(select count(s.school_id) from school inner join school s on s1.school_id = s.school_id where (s.[type] = 'Secondary School') ) as noofSecondary,
(select count(s.school_id) from school inner join school s on s1.school_id = s.school_id where (s.[type] = 'Nursery/Pre-School') ) as noooPrimarys
from school s1
Group by school_lea, school_id
数据
| school_id | type | school_name | school_lea |
|-----------|--------------------|---------------------------------|-------------------------|
| 1 | Secondary School | Ashcroft Technology Academy | Wandsworth |
| 2 | Secondary School | Abbey Grange C of E High School | Wandsworth |
| 3 | Secondary School | Abbey Wood School | Wandsworth |
| 4 | Secondary School | Abbeydale Grange School | Wandsworth |
| 5 | Foundation school | Jubilee High School | Surrey |
| 6 | Secondary School | Abbot Beyne School | Staffordshire |
| 7 | Community school | Acland Burghley School | Staffordshire |
| 8 | Primary School | Agnes Stewart CE High School | Staffordshire |
| 9 | Primary School | Aith Junior High School | Staffordshire |
| 10 | Secondary School | Albany Academy | Staffordshire |
| 11 | Nursery/Pre-School | Oasis Academy Hadley | Warwickshire |
| 12 | Secondary School | Alcester Grammar School | Warwickshire |
| 13 | Secondary School | Alva Academy | Warwickshire |
| 14 | Secondary School | Amery Hill School | Warwickshire |
| 15 | Secondary School | Ysgol Dyffryn Aman | Warwickshire |
| 16 | Secondary School | Endeavour High School | Warwickshire |
| 17 | Other | Priory Witham Academy | Warwickshire |
| 18 | Secondary School | Anderson High School | Warwickshire |
| 19 | Secondary School | Andrew Marvell School | Kingston upon Hull City |
| 20 | Other | North Liverpool Academy | Liverpool |
| 21 | Secondary School | The Highweald Academy | Liverpool |
| 22 | Secondary School | Anglo European School | Liverpool |
| 23 | Secondary School | Annan Academy | Liverpool |
| 24 | Academy | Ansford Academy Trust | Liverpool |
| 25 | Secondary School | Anthony Gell School | Liverpool |
| 26 | Other | Antrim Grammar School | Liverpool |
| 27 | Secondary School | Appleby Grammar School | Cumbria |
| 28 | Secondary School | Applemore College | Cumbria |
1)摆脱子查询的'选择'并使用普通的'JOIN' 2)首先准备数据(例如使用'CTE'),然后是'GRO UP BY' – lad2025