您的位置:程序门 -> db2 -> 数据库开发



有没有更好的查询方法


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


有没有更好的查询方法[无满意答案结贴,结贴人:windlysnowly]
发表于:2007-12-12 13:09:57 楼主
表结构如下:
币种 金额(本币) 金额(外币)
001 100 500
001 200 600
013 300 700
014 400 800
     
希望显示的结果:
币种 金额
001 100
001 200
013 700
001 300
014 800
001 400

注:001代表本币,其他的都是外币,检索结果中,外币币种都生成2条记录,一条是外币信息一条是本币信息
        可以使用任何方法来实现(包括建临时表等)


发表于:2007-12-12 13:22:021楼 得分:0
select   币种   ,金额(本币)   from   table1  
union   all
select   币种   ,金额(外币)   from   table1

你要的是这个意思吗?
发表于:2007-12-12 13:30:272楼 得分:0
币种   金额(本币)   金额(外币)
001   100   500
001   200   600
013   300   700
014   400   800
         
希望显示的结果:
币种   金额
001   100--------001是本币,所以只查询金额(本币)                  
001   200--------
013   700--------003是外币,外币查询要查询两条记录,本币信息和外币信息
001   300--------这行的001本来应该是003,但是金额300是指本币,也就是指向了本币所代表的001编号
014   800--------同003一个意思
001   400  
我已经写了一中语句,感觉不好
发表于:2007-12-12 15:57:363楼 得分:0
013       700--------003是外币,外币查询要查询两条记录,本币信息和外币信息
001       300--------这行的001本来应该是003,但是金额300是指本币,也就是指向了本币所代表的001编号
014       800--------同003一个意思  

这几句没看明白
发表于:2007-12-12 17:09:334楼 得分:0
select   币种,   金额(本币)   from   xxx   where   币种='001'     --本币数据
union
select   '001'   as   币种,   金额(本币)   from   xxx   where   币种 <> '001'     --非本币数据的本币信息
union
select   币种,   金额(外币)   from   xxx   where   币种 <> '001'     --非本币数据的外币信息

满足条件了吗?
发表于:2007-12-13 16:53:325楼 得分:0
with   temp(money_type,money)   as
                                                      (select     money_type,local_money   money  
                                                        from   test13  
                                                        where   money_type   <>   '001'
                                                        union   all
                                                        select     money_type,
case   when   money_type   ='001'  
then   local_money   else   foreign_money  
end   money  
                                                        from   test13)
select   case   when   money   in(select   local_money  
                                                                                from   test13  
                                                                                where   money_type   <>   '001')  
                                                    then   '001'  
                                                    else   money_type   end,   money  
from(select   *   from   temp   order   by   money_type)   table


上面我写的这个需要进一步改进下   我不知道别的方法是否比他效率更好
发表于:2007-12-13 17:05:356楼 得分:0
很感谢上面几位的回答,其实仔细看我给的需求,对比注解和前后表还是比较清楚的


快速检索

最新资讯
热门点击