我有一个表role和和一个role_user,还一个user表
role表中有
role_id role_name
1 增加
2 修改
3 删除
表role_user中有
role_id user_id
1 001
2 001
1 002
1 003
2 003
3 003user表中
user_id user_name
001 张三
002 李四
003 王五
现在要的结果是这样的:
增加 修改 删除   
张三 张三 王五
李四 王五
王五

解决方案 »

  1.   


    select case when role_name = '增加' then user_name end as "增加",
           case when role_name = '修改' then user_name end as "修改",
           case when role_name = '删除' then user_name end as "删除"
      from (select user_name, role_name
              from role_tab a, role_user b, user_tab c
             where a.role_id = b.role_id
               and c.user_id = b.user_id);
      

  2.   

    select  
    max(case when c.role_id=1 then a.user_name end) 增加,
    max(case when c.role_id=2 then a.user_name end) 删除,
    max(case when c.role_id=3 then a.user_name end)  修改
     from 
    [user] a,role_use b,role c
    where a.user_id=b.user_id and b.role_id=c.role_id
      

  3.   

    我想到的有两种方法:(不对的勿喷)
    法一:
    1、先用批量函数查询出你要的这句话  case when role_name = '增加' then user_name end) "增加",select 'max(case when role_name = '||chr(39)||状态名||chr(39)||' then user_name end) "状态名",'
    from (select 状态名 from 你要搜索的状态表);2、再在编辑器中粘贴上写全就好了法二:
    实用游标cursor对状态表扫描,用&状态列接收即可
      

  4.   


    去找个支持交叉报表的客户端去做个交叉报表就行了,不要把所有的工作都交给SQL,你这个需求不适合用SQL来做。
      

  5.   

    行转列,主要用到case...when 语句
      

  6.   


    他那有200多个列,case when会写到手软。
      

  7.   

    推荐下吧,很经典的帖子
    http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?93286
      

  8.   

    经典中的经典
    http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?13274