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 8486227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:53:09+00:00 2026-06-10T20:53:09+00:00

I saw a few posts about get columns from subqueries, most of them suggest

  • 0

I saw a few posts about get columns from subqueries, most of them suggest use a single select for each column, but this is my case

SELECT CAST(p.idvendor AS VARCHAR(10)), CAST(p.description AS VARCHAR(50)),  
(SELECT CAST(NVL(SUM(CASE WHEN locals_sales.cantidad >=0
                           AND locals_sales.cantidad <locals_sales.pqlocals
                     THEN 1 ELSE 0 END), '0') AS VARCHAR(10)) 
   FROM locals_sales, agents, vendors, locals, locals_sales, history
  WHERE locals.idvendor = vendors.idvendor 
    AND locals_sales.IDlocals = locals.idlocals
    AND agents.codagente=vendors.codagente
    AND agents.idagent!=1
    AND locals_sales.number_n_line = locals_sales.n_line
    AND locals.estado=1
    AND vendors.idvendor = p.idvendor
    AND history.number_line(+) = locals_sales.n_line
    AND history.date(+) >= to_date('4/9/2012 00:00', 'dd/mm/yy HH24:mi:ss')
 ) AS critic, 
(SELECT CAST(NVL(SUM(CASE WHEN locals_sales.cantidad >=locals_sales.pqlocals
                           AND locals_sales.cantidad <locals_sales.media
                     THEN 1 ELSE 0 END), '0') AS VARCHAR(10))
   FROM locals_sales, agents, vendors, locals, locals_sales, history
  WHERE locals.idvendor = vendors.idvendor
    AND locals_sales.IDlocals = locals.idlocals
    AND agents.codagente=vendors.codagente
    AND agents.idagent!=1
    AND locals_sales.number_n_line = locals_sales.n_line
    AND locals.estado=1
    AND vendors.idvendor = p.idvendor
    AND history.number_line(+) = locals_sales.n_line
    AND history.date(+) >= to_date('4/9/2012 00:00', 'dd/mm/yy HH24:mi:ss')
 ) AS medium, 
(SELECT CAST(NVL(SUM(CASE WHEN locals_sales.cantidad >=locals_sales.media
                     THEN 1 ELSE 0 END), '0') AS VARCHAR(10))
   FROM locals_sales, agents, vendors, locals, locals_sales, history
  WHERE locals.idvendor = vendors.idvendor
    AND locals_sales.IDlocals = locals.idlocals
    AND agents.codagente=vendors.codagente
    AND agents.idagent!=1
    AND locals_sales.number_n_line = locals_sales.n_line
    AND locals.estado=1
    AND vendors.idvendor = p.idvendor
    AND history.number_line(+) = locals_sales.n_line
    AND history.date(+) >= to_date('4/9/2012 00:00', 'dd/mm/yy HH24:mi:ss')
 ) AS NORMAL 
 FROM vendors p, agents ag
WHERE ag.codagente=p.codagente

As you can see I have three SUM for each column, these columns are from the same tables, the difference comes from my preconditions in each CASE of each select

Also, almost the end of each select I have this as condition

vendors.idvendor = p.idvendor

p is the alias of the table outside of the subquery

How can I avoid use three selects almost similar and use instead a single select for the 3 columns in order to improve the performance?

  • 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-06-10T20:53:11+00:00Added an answer on June 10, 2026 at 8:53 pm

    You can handle this by moving the subqueries into the FROM clause and doing a proper join.

    The final query will look like:

    select CAST(p.idvendor AS VARCHAR(10)), CAST(p.description AS VARCHAR(50)),
           s.val1, s.val2, sl.val3
    from vendors p join
         agents ag 
         on p.codeagent = ag.codeagent join
         (<a variation of your subquery here with all three columns defined and idvendor in the select list>
         ) s
         on p.idvendors= s.idvendor
    

    Also, to make your queries more readable, you should really use proper join syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked into a few posts and saw some related posts on it
I have read a few posts about people putting in either their own icons
I saw quite a few other posts related to EXC_BAD_ACCESS signals right on glDrawArrays,
I saw a few answers to this issue, and I get it — you
I saw a few libraries through a quick Google search. What's generally the most
I saw a few posts similar to my problem and tried the solutions offered,
I saw few examples over internet of Hibernate using transaction.commit() for select statements. Below
So I have looked around and saw a few posts on similar things but
I saw a few posts on this but they were for PHP (I need
i saw few android apps got the interface similar with itune(as attached), user can

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.