2011-05-19 67 views
2

这里总共有两个联合绑定三个查询,第一个查询从虚拟表(内部查询)检索记录,两个简单查询从物理表中检索记录。 所有这三个查询生成一个名为“vertulatable”的虚拟表,通过group by和order by子句检索“vertictable”中的所有记录。 但问题是它没有按最终价格做适当的降序。从媒体查询中检索到的一些记录的值为150,但在0或小于150之后显示,但在结果记录中应该从0或小于150的顶部开始。通过mysql中的问题排序

SELECT 
sum(vertualtable.WyoPrice) as WyoPrice, 
     sum(vertualtable.normalPrice) as normalPrice, 
     vertualtable.sbnam, 
     vertualtable.sbwynum, 
     if(vertualtable.sbwynum in(25,43), 
      sum(vertualtable.WyoPrice), sum(vertualtable.normalPrice)) as finalPricePrev, 
     sum(vertualtable.finalPrice) as finalPrice, 
    vertualtable.BuilderStatus 

FROM (


SELECT sum(vertualtbl.WyoPrice) as WyoPrice, 
     sum(vertualtbl.normalPrice) as normalPrice, 
     vertualtbl.sbnam, 
     vertualtbl.sbwynum, 
     if(vertualtbl.sbwynum in(25,43), 
      sum(vertualtbl.WyoPrice), sum(vertualtbl.normalPrice)) as finalPrice, 
    'Main' as BuilderStatus 
FROM(

    select 
     b.sbnam, 
     b.sbwynum, 
     'g'  as g, 
     sum(if (s.adjprice > 0, s.adjprice, if (s.price > 0, s.price, 
     s.estprice))) as normalPrice, 
     st.forsearchwoy, 
     SUM(if (st.forsearchwoy = 'Y', 
       F_offshorePrice(s.topsbwynum, s.hulsbwynum, s.sbwynum, s.adjprice, 
      s.price, s.estprice, s.pricehulint, s.pricetopint, s.pricehulcons, 
      s.pricetop, s.priceint, s.pricetht),0)) as WyoPrice 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.sbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 

group by s.tblshipwynum 



) vertualtbl group by vertualtbl.sbwynum 




UNION 

    select 
     '0' as WyoPrice, 
     '0' as normalPrice, 
     b.sbnam as sbnam,   
     s.hulsbwynum as sbwynum, 
     SUM(if (st.forsearchwoy = 'Y',s.pricehulcons,0)) as finalPrice, 
     'HUL' as BuilderStatus 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.hulsbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum != s.hulsbwynum and 
     s.hulsbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 
group by s.hulsbwynum 



    UNION 

    select 
     '0' as WyoPrice, 
     '0' as normalPrice, 
     b.sbnam as sbnam,   
     s.topsbwynum as sbwynum,   
     SUM(if (st.forsearchwoy = 'Y',s.pricetop,0)) as finalPrice, 
     'TOP' as BuilderStatus 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.topsbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum != s.topsbwynum and 
     s.topsbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 
    group by s.hulsbwynum 

) as vertualtable 

group by vertualtable.sbnam 
order by vertualtable.finalPrice desc 

非常感谢。

回答

0

我做了一些小的修改,查询 - 试试,看看结果是你预料

SELECT 
sum(vertualtable.WyoPrice) as WyoPrice, 
     sum(vertualtable.normalPrice) as normalPrice, 
     vertualtable.sbnam AS sbnam, 
     vertualtable.sbwynum, 
     if(vertualtable.sbwynum in(25,43), 
      sum(vertualtable.WyoPrice), sum(vertualtable.normalPrice)) as finalPricePrev, 
     sum(vertualtable.finalPrice) as finalPrice, 
    vertualtable.BuilderStatus 

FROM (


SELECT sum(vertualtbl.WyoPrice) as WyoPrice, 
     sum(vertualtbl.normalPrice) as normalPrice, 
     vertualtbl.sbnam AS sbnam, 
     vertualtbl.sbwynum, 
     if(vertualtbl.sbwynum in(25,43), 
      sum(vertualtbl.WyoPrice), sum(vertualtbl.normalPrice)) as finalPrice, 
    'Main' as BuilderStatus 
FROM(

    select 
     b.sbnam AS sbnam, 
     b.sbwynum, 
     'g'  as g, 
     sum(if (s.adjprice > 0, s.adjprice, if (s.price > 0, s.price, 
     s.estprice))) as normalPrice, 
     st.forsearchwoy, 
     SUM(if (st.forsearchwoy = 'Y', 
       F_offshorePrice(s.topsbwynum, s.hulsbwynum, s.sbwynum, s.adjprice, 
      s.price, s.estprice, s.pricehulint, s.pricetopint, s.pricehulcons, 
      s.pricetop, s.priceint, s.pricetht),0)) as WyoPrice 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.sbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 

group by s.tblshipwynum 



) vertualtbl group by vertualtbl.sbwynum 




UNION 

    select 
     '0' as WyoPrice, 
     '0' as normalPrice, 
     b.sbnam as sbnam,   
     s.hulsbwynum as sbwynum, 
     SUM(if (st.forsearchwoy = 'Y',s.pricehulcons,0)) as finalPrice, 
     'HUL' as BuilderStatus 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.hulsbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum != s.hulsbwynum and 
     s.hulsbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 
group by s.hulsbwynum 



    UNION 

    select 
     '0' as WyoPrice, 
     '0' as normalPrice, 
     b.sbnam as sbnam,   
     s.topsbwynum as sbwynum,   
     SUM(if (st.forsearchwoy = 'Y',s.pricetop,0)) as finalPrice, 
     'TOP' as BuilderStatus 
from tblship s 
     left join tblbuilder b on b.sbwynum = s.topsbwynum and b.deleted = 'N' 
     left join tblshiptype as st on st.wytypid = s.wytypid 
where s.deleted != 'Y' and 
     (s.sbwynum != s.topsbwynum and 
     s.topsbwynum > 0) and 
     status in ('O', 'FO', 'JV', 'PR', 'C', 'F') 
    group by s.hulsbwynum 

) as vertualtable 

group by sbnam 
order by finalPrice desc 
+0

非常感谢,都铎康斯坦丁的人,正是我期待,谢谢again..yet ... – Bajrang 2011-05-19 07:53:17