带GroupBy的T-SQL子查询

时间:2015-09-04 10:05:09

标签: sql sql-server tsql

我正在尝试对下面的数据写一个查询,它给出了每个school_lea中有多少种类型的学校

所以对于 Wandsworth 我会有

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
到目前为止我所拥有的是这个,但它没有产生正确的数字,并且包含重复的学校lea&#39>

任何人都可以就我缺少的东西提出建议吗?

感谢

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                 |

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 ')