例如,查询test表中的全部记录,其中test表中有Tid,Tname,Tpassword等列,表中Tname列中有部分值相等,一般Tname列值相同的都能返回两行,我要把两行的列合并为一行记录,而user表中有UserId,Uname,Utype,created_date等列,其中Uname列的值相同的也可以返回两行数据,同样还是要合并成一行,test表的Tname和user表的Uname列是关联的,现在要把相同列的结果合并为一条记录,显示结果为Tid1,Tid2,UserId1,UserId2,Tname,Tpassword,created_date几列,Utype列不显示,怎么写查询语句?如果是有条件的,我大概知道怎么写,但关键是现在是全表查询,没有特定条件,不知道怎么写了,崩溃… …
假设test表的数据有:  Tid   Tname    Tpassword  1     张三      aaa  2     张三      aaa  3     李四      ccc  4     李四      ccc 
  user表的数据有:UserId   Uname   Utype   created_date1001     张三     TI    2008-05-29 10:23:45
1002     张三     TO    2008-05-29 10:23:45
1003     李四     BI    2008-05-30 14:56:27
1004     李四     BO    2008-05-30 14:56:27
 我要的查询结果显示为: Tid1    Tid2    UserId1     UserId2     Tname    Tpassword     created_date1       2       1001       1002        张三      aaa      2008-05-29 10:23:453       4       1003       1004        李四      ccc      2008-05-30 14:56:27
注:当Utype的值为TI或者BI的时候,user表中的UserId的值给UserId1,
    当Utype的值为TO或者BO的时候,user表中的UserId的值给UserId2    而且两表中的数据顺序不一定这样,也就是说name列相同的行不一定在一块,其中user表中肯定有有两行Uname列值相同的数据,而test表中大部分有两行Tname列值相同的,但有少量Tname列的值没有相同值,这时候就将这列的值既给Tid1,也给Tid2

解决方案 »

  1.   

    select min(b.tid) Tid1,
           max(b.tid) tid2,
           max(decode(a.Utype,'TI',a.UserId,'BI',a.UserId,null)) UserId1,
           max(decode(a.Utype,'TO',a.UserId,'BO',a.UserId,null)) UserId2,
           b.Tname,b.Tpassword,
           max(a.created_date) created_date 
         from user a,test b
         where a.uname=b.Tname(+)
         group by b.Tname,b.Tpassword 
          
    你去试试看
      

  2.   

    select b.tid,
           d.tid,
           b.tname,
           b.tpassword (select a.tid,
                               a.tname,
                               a.tpassword,
                               row_num() over(partition by Tname order by tid) rn
                          from test a) b,
           (select c.tid,
                   c.tname,
                   c.tpassword,
                   row_num() over(partition by c.Tname order by c.tid) rn
              from test c) d
     where b.tname = d.tname
       and (b.rn - 1) = d.rn(+)我只是做个简单的说明,就是把第一个表作成 1      2  张三      aaa  
                                       3      4  李四      ccc      记为表E
    这样,第二个表按照上面的写法再作成  1001      1002  张三    2008-05-29 10:23:45 
                                  1003      1004   李四    2008-05-30 14:56:27  记为表F
    select E.tid1,E.tid2,F.UserId1,F.UserId2,E.Tname,E.Tpassword,F.created_date
    from E,F
    where E.tname=F.tname
    就可以了(E,F为查询出来的中间表,具体方法自己看上面的代码)
      

  3.   

    select b.tid,
           d.tid,
           b.tname,
           b.tpassword (select a.tid,
                               a.tname,
                               a.tpassword,
                               row_num() over(partition by Tname order by tid) rn
                          from test a) b,
           (select c.tid,
                   c.tname,
                   c.tpassword,
                   row_num() over(partition by c.Tname order by c.tid) rn
              from test c) d
     where b.tname = d.tname
       and (b.rn - 1) = d.rn(+)