2015-09-04 55 views
1

我试图写在下面的数据的查询,这给了我多少类型的学校在每个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     | 
+0

1)摆脱子查询的'选择'并使用普通的'JOIN' 2)首先准备数据(例如使用'CTE'),然后是'GRO UP BY' – lad2025

回答

2

与CASE的帮助下,你可以做这样的:

select 
    school_lea, 
    sum(case type when 'Secondary School' then 1 end) noofSecondary, 
    *(the rest of your types)* 
from myTable 
group by 
    school_lea 
+0

关闭,你想'GROUP BY school_lea'。事实上,你已经分组了一个独特的ID ... – MatBailie

+0

Thx,是的就是这样。 @MatBailie – mxix

1
 

i think you need dynamic insted of static value 

DECLARE @cols AS VARCHAR(MAX) 
DECLARE @query AS VARCHAR(MAX) 

select @cols = STUFF((SELECT ',' + QUOTENAME(cast(type as varchar(50))) 
        from 
        ( 
        select a1.type from (
         SELECT DISTINCT type 
         from dbo.school 
        )a1 

        ) t 

      FOR XML PATH(''), TYPE 
      ).value('.', 'VARCHAR(MAX)') 
     ,1,1,'') 

    PRINT @cols 

    EXEC(' SELECT school_lea,' + @cols + '  
      from 
      ( 
       select 
       school_lea , 
       type 
       from 
       ( 
       select school_lea, type 
       from school 
      ) src 
      ) x 
      pivot 
      ( 
       count(type) 
       for type in (' + @cols + ') 
      ) p where 1=1 ')