Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5841969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:57:26+00:00 2026-05-22T11:57:26+00:00

Here total two union binding three queries, first query retrieving records from a virtual

  • 0

Here total two union binding three queries, first query retrieving records from a virtual table(Inner query) and two simple queries retrieving records from a physical tables.
All three queries making a virtual table named “vertulatable” retrieving all records from “vertualtable” with group by and order by clause.
But the problem is its not doing a proper descending order by final price. Some records retrieved from mediater query has value 150 but showing after 0 or less then 150,but should be top from 0 or less then 150 in resultant records.

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 

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:57:27+00:00Added an answer on May 22, 2026 at 11:57 am

    I made some small modifications to your query – try to see if the result is the one you expected

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.