简体   繁体   English

结合两个SQL查询成一个?

[英]combining two sql queries into one?

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. 如您所见,查询位于同一表的archdetails并且两者的条件也相同。

query 1 will output something like 查询1将输出类似

12
47

query 2 will output something like 查询2将输出类似

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. 这些查询中的每个查询都在x时间内运行。 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. 我知道我可以将这些查询放入新查询的from子句中并获得所需的结果,但是新查询将以2倍的时间运行。

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 谢谢

Select the results of the first query into a temp table. 将第一个查询的结果选择到临时表中。

Then to get the first result set, select * from that temp table. 然后,要获取第一个结果集,请从该临时表中select *

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. 要获得第二个结果集,请将临时表连接到第二个查询,而没有其他where子句语句,也不会从临时表中选择任何列。

(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) (如果优化器设法以某种方式设法执行了第二次查询,则将第二次查询的结果存储到第二个临时表中并联接2个临时表)

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

UPDATE: 更新:

You might be able to gain some by removing one of the 'select distinct compname from svn3 where revno='r270294'' 您可能可以通过'select distinct compname from svn3 where revno='r270294''删除'select distinct compname from svn3 where revno='r270294''之一来获得一些'select distinct compname from svn3 where revno='r270294''

by 通过

SELECT ad.flatpos , 
   totals.compname, 
   totals.inFlat, 
   totals.flatlooopindex

FROM
    archdetails ad
    INNER JOIN 
    (select count(distinct compname) compname,
        sum(CASE WHEN inFlat=1 THEN 1 ELSE 0 END) inFlat,
        min(flatLoopIndex) flatlooopindex
    from archdetails
    where version='1.3'
     AND compname IN (
          select distinct compname from svn3 where revno='r270294'
    )) totals 
    ON ad.compname = totals.compname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM