已有一个表 
datetime    shiftno    groupno  
20080101    1          1
20080101    2          2
20080101    3          3
20080101    2          4
20080101    1          1
20080101    2          2
20080101    3          3
20080101    3          4
20080101    1          1
20080101    2          2
20080101    3          3
20080101    2          4
20080101    1          1
20080101    2          2
20080101    3          3
20080101    1          4
....
共有 5000行 
shiftno对应关系
1 夜
2 早
3 中
groupno对应关系
1 甲
2 乙
3 丙
4 丁现求一段代码 :
要求建一个视图 
然后视图显示为 
datetime    shiftno    groupno    shift    group
20080101    1          1          夜         甲
20080101    2          2          早         乙
20080101    3          3          中         丙
20080101    2          4          早         丁
20080101    1          1          夜         甲
20080101    2          2          早         乙 
20080101    3          3          以下省略
20080101    3          4
20080101    1          1
20080101    2          2
20080101    3          3
20080101    2          4
20080101    1          1
20080101    2          2
20080101    3          3
20080101    1          4
....高手赐教 
谢谢 
做完这个就可以放假了 谢谢 在线等  

解决方案 »

  1.   

    select a.* , b.col , c.col
    from [已有一个表] a 
    left join shiftno b 
    on a.shiftno = b.shiftno
    left join groupno c 
    on a.groupno = b.groupno
      

  2.   

    或者select m.* , n.col from
    (
      select a.* , b.col from [已有一个表] a left join shiftno b on a.shiftno = b.shiftno 
    ) m 
    left join groupno n
    on m.groupno = n.groupno
      

  3.   

    谢谢  dawugui ,我现在是想在原来表的基础上创建一个视图,然后视图在原来表的基础上再增加两列 shift 和 group
    因为 那个表每天都有数据增加 所以想 通过视图来处理
    增加的两列的 内容要求变成中文的 ,下面是对应关系 
    shiftno对应关系 
    1 夜 
    2 早 
    3 中 
    groupno对应关系 
    1 甲 
    2 乙 
    3 丙 
    4 丁
    shift 的值为 夜  早  中
    group 的值为 甲  乙  丙  丁谢谢 请问如何实现 
      

  4.   

    还有 对应 关系 不是 表,是给出的需求文档中的内容
    可能是我描述错误了,dawugui 把它当成表了我的意思是 当shiftno 的值为1 时 shift的值为 夜
                 shiftno 的值为2 时 shift的值为 早
                 groupno 也是一样 
    以此类推。 谢谢 
      

  5.   

    create table A
    (
    datetime varchar(10),
    shiftno int,
    groupno int
    )
    insert into A values('20080101',1,1);
    insert into A values('20080101',2,2);
    insert into A values('20080101',3,3);
    insert into A values('20080101',1,4);
    insert into A values('20080101',2,1);
    select datetime,shiftno,groupno,
           decode(shiftno,1,'夜',2,'早',3,'中','null') as shift,
           decode(groupno,1,'甲',2,'乙',3,'丙',4,'丁','null') as groups 
           from A
           group by datetime,groupno,shiftno
      

  6.   

    执行结果:
    DATETIME      SHIFTNO    GROUPNO SHIF GROU
    ---------- ---------- ---------- ---- ----
    20080101            1          1 夜   甲
    20080101            2          1 早   甲
    20080101            2          2 早   乙
    20080101            3          3 中   丙
    20080101            1          4 夜   丁
      

  7.   

    select t.* , 
           case when shiftno = 1 then '夜'
                when shiftno = 2 then '早'
                when shiftno = 3 then '中'
           end shift,
           case when groupno = 1 then '甲'
                when groupno = 2 then '乙'
                when groupno = 3 then '丙'
                when groupno = 4 then '丁'
           end [group]
    from tb t
      

  8.   

    9楼的对的不需要group by 的