现在有这样四个表
表1:                         表2:                   表3:                      表4:       
oid  levelId               id      Level           oid    groupid      id     group
1    1                      1      f                 1     1                1      cj
2    1                      3      s                 1     1                2      so
3    2                                               3     2                3      op要求显示这样的结果:
level    cj      so      
  f       2      0          
  s       0      1        
group by 按groupid就可以
我不知道怎样把表4的记录变为显示结果的字段名谢谢大家!

解决方案 »

  1.   

     select   name,sum(case   subject   when   '数学'   then   source   else   0   end)   as   '数学',   
        
                          sum(case   subject   when   '英语'   then   source   else   0   end)   as   '英语',   
        
                              sum(case   subject   when   '语文'   then   source   else   0   end)   as   '语文'     
        
      from   test     
        
      group   by   name   
      
    类似这样的交叉表例子
      

  2.   

    select name as n where employee;
    这样就可以把字段名name改为n了,这就是sql字段的重命名方法``
    sql语句怎么写还要想想
      

  3.   

    表1:                                         表2:
    oppid      levelId                       LevelId       Level
    1            1                            1              f
    2            1                            2              s
    3            2
    表3:                                          表4:
    oppId        GroupId             GroupId        Group
    1               1                              1           cj
    1               1                              2           so
    3               2                              3           op结果:
    level              cj             so             
        f               2             0                     
        s               0             1  cj 和 so 下面的都是按groupId分组后统计的表1的记录数
      

  4.   

    select  (case Abbrev when 'ADvisory' then ADvisory END) as ADvisory,
    (case Abbrev when 'BR' then BR END) as BR

    FROM situsgroup我这么写有什么错误?
      

  5.   

    正确答案:(存储过程)
    SELECT
        CASE (opps.GroupId)
    WHEN 1 THEN COUNT(opps.GroupId)
    END 'cj'
    INTO #temp1
    FROM
        表3 opps
    INNER JOIN 
        表1 o
    on 
        opps.oppid=o.oppid
    inner join    表2 s
    on 
        o.levelId=s.levelid
    group by 
        o.levelid,
        opps.groupid,
        s.level--
    SELECT 
        SUM(ISNULL(a.cj, 0)) AS 'cj'
    FROM
    表2 s
    LEFT JOIN
    #temp1 a
    ON
    s.LevlelId=a.levelId
    GROUP BY 
    s.LevlelId,
    s.level