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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:21:11+00:00 2026-05-17T23:21:11+00:00

I have two similar queries on same table and same where condition but different

  • 0

I have two similar queries on same table and same where condition but different selects on it.

Select flatpos from archdetails
where version='1.3' AND compname IN (
    select distinct compname from svn3 where revno='r270294'
)

AND

select count(distinct compname),
    sum(CASE WHEN inFlat=1 THEN 1 ELSE 0 END),
    min(flatLoopIndex)
from archdetails
where version='1.3'
AND compname IN (
    select distinct compname from svn3 where revno='r270294'
)

As you can see the query is on the same table archdetails and where condition is same for both as well.

query 1 will output something like

12
47

query 2 will output something like

396 43 1

I would like the output to be

12 396 43 1
47 396 43 1

I cannot obviously combine them by a group by.

Each one of these query runs in x amount of time. I know I can just put these queries into the from clause of a new query and get the desired result but then the new query runs in 2x amount of time.

Is there a faster way around since database essentially has to be scanned just once and then it is just a matter of formatting.

Thanks

  • 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-17T23:21:12+00:00Added an answer on May 17, 2026 at 11:21 pm

    Select the results of the first query into a temp table.

    Then to get the first result set, select * from that temp table.

    To get the second result set, join the temp table to the second query with no additional where clause statements and no columns selected from temp table.

    (If the optimizer somehow manages to execute the second query N times, stash the results of second query into second temp table and join 2 temp tables)

    create temporary table tmp1
    Select flatpos from archdetails
    where version='1.3' AND compname IN (
        select distinct compname from svn3 where revno='r270294'
    )
    
    create temporary table tmp2
    select count(distinct compname) as c,
        sum(CASE WHEN inFlat=1 THEN 1 ELSE 0 END) as s,
        min(flatLoopIndex) as m
    from archdetails
    where version='1.3'
    AND compname IN (
        select distinct compname from svn3 where revno='r270294'
    )
    
    select * from tmp1
    
    select tmp2.c, tmp2.s, tmp2.m from tmp1, tmp2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two documents both are similar but I need to find an elegant
I have two XML files. They are similar, but there are two nodes of
I have two identical tables and need to copy rows from table to another.
I have two tables with similar columns - let's say table A with column
I have two tables with similar columns. I would simply like to select both
I have two computers. Both running WinXP SP2 (I don't really know ho similar
I'm trying to speed up SELECT queries in MySQL tables which already have some
I have a query similar to this FROM products AS p, .. LEFT JOIN
I have two Javascript objects similar to so.... var Object2 = new (function() {
I have two extension methods that are very similar. I'd like to remove the

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.