create view view2 as
 select CEOCount=( select count(1)  from view1 where Position='ceo') ,
ExecuteCount= (select count(1)  from view1 where Position='Execute')  ......

解决方案 »

  1.   

    select sum(case when position='ceo' then 1 end) as CEOCount,
    sum(case when position='Execute' then 1 end) as ExecuteCount,
    sum(case when position='Support' then 1 end) as SupportCount,
    sum(case when position='Support' then 1 end) as ManagerCount
    from view 
      

  2.   

    --如果 Position 固定,则可以写出view
    create view view_name
    as
    select 
    CEOCount=sum(case Position when 'CEO' then 1 else 0 end),
    ExecuteCount=sum(case Position when 'xecute' then 1 else 0 end),
    SupportCount=sum(case Position when 'Support' then 1 else 0 end),
    ManagerCount=sum(case Position when 'Manager' then 1 else 0 end)
    from [view]
      

  3.   


    --否则的话要用动态SQL语句,即写成存储过程--如果 Position 固定,则可以写出view
    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+Position+'count]=sum(case Position when '''+Position+''' then 1 else 0 end)'
    from [view]
    group by Position
    set @s=stuff(@s,1,1,'')
    exec('select'+@s+' from [view]')
      

  4.   

    恩,好的,谢谢各位,现在又有新问题,如果我想新创建的view只有3个字段[ID],[PositionName],[PositionCount],因为职位名称在编程前要到库里面去查询的,所以最好能动态制定职位名称,这样该怎样实现呢??
      

  5.   

    基本的职位表就在第一个view里面的
      

  6.   

    --你的描述不太清晰,用中文给你写,你自己再对应你的表结构调整declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+职位名称+'count]=sum(case 职位编号 when '''+cast(职位编号 as varchar)+''' then 1 else 0 end)'
    from 职位名称表set @s=stuff(@s,1,1,'')
    exec('select'+@s+' from 视图')
      

  7.   

    是这样,现在有一个名字叫PersonnelView的View,里面包含了一个PositionName字段,可PositionName字段存储的并不是PositionID,现在要创建的新view就像我刚才说的那样实现,就有3个字段[ID],[PositionName],[PositionCount],像这样:
    ID     PositionName   PositionCount
    1      CEO            1
    2      Manager        16
    3      Support        5
    4      Execute        20

    问题是现在怎样能实现这个View呢?
      

  8.   

    现在给的只有PersonnelView,再也没有其他的view了