如何将以下两个查询组合成一个结果?

时间:2014-06-18 20:00:25

标签: sql union

我有两个问题。

我希望将它们组合在一起作为一个结果。他们有prfa_type作为共同ID。

第一个查询返回18个结果,第二个查询返回4个结果。

我想将它们组合在一起,这样就可以得到18个结果。

第一次查询:

SELECT prfa_nametr, 
   prfa_type, 
   Sum(orit_quantity) AS Miktar, 
   Sum(CASE 
         WHEN orit_currencyid = 1 THEN orit_sumnetamount - 
                                       orit_sumnetamount * 
                                       oram_discountrate 
         WHEN orit_currencyid = 2 THEN orit_sumnetamount * 2 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 2 
         WHEN orit_currencyid = 3 THEN orit_sumnetamount * 3 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 3 
         WHEN orit_currencyid = 4 THEN orit_sumnetamount * 4 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 4 
       END)           AS Toplam 
FROM   orderitem_crt 
   INNER JOIN product_crt 
           ON orit_productid = prod_id 
   INNER JOIN productfamily_crt 
           ON prod_productfamilyid = prfa_id 
   INNER JOIN order_crt 
           ON orit_orderid = orde_id 
   INNER JOIN orderamount_crt 
           ON oram_orderid = orde_id 
WHERE  orit_sumnetamount <> 0 
   AND orit_alternativeno = 0 
   AND orde_deleted IS NULL 
   AND orde_stageid <> 5 
   AND orit_deleted IS NULL 
GROUP  BY prfa_nametr, 
      prfa_type 
ORDER  BY prfa_nametr </i>

第二个查询:

SELECT prfa_type, 
   Sum(orit_quantity) AS Miktar, 
   Sum(CASE 
         WHEN orit_currencyid = 1 THEN orit_sumnetamount - 
                                       orit_sumnetamount * 
                                       oram_discountrate 
         WHEN orit_currencyid = 2 THEN orit_sumnetamount * 2 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 2 
         WHEN orit_currencyid = 3 THEN orit_sumnetamount * 3 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 3 
         WHEN orit_currencyid = 4 THEN orit_sumnetamount * 4 - 
                                       orit_sumnetamount * oram_discountrate 
                                       * 4 
       END)           AS Toplam 
FROM   orderitem_crt 
   INNER JOIN product_crt 
           ON orit_productid = prod_id 
   INNER JOIN productfamily_crt 
           ON prod_productfamilyid = prfa_id 
   INNER JOIN order_crt 
           ON orit_orderid = orde_id 
   INNER JOIN orderamount_crt 
           ON oram_orderid = orde_id 
WHERE  orit_sumnetamount <> 0 
   AND orit_alternativeno = 0 
   AND orde_deleted IS NULL 
   AND orde_stageid <> 5 
   AND orit_deleted IS NULL 
GROUP  BY prfa_type 
ORDER  BY prfa_type 

1 个答案:

答案 0 :(得分:1)

执行此操作的几种方法,如果将query1中的所有内容都粘贴到临时表中,然后对query2执行相同操作,则可以执行以下查询

select query1.prfa_nametr,
  query1.prfa_type,
  query1.Miktar,
  query1.Toplam,
  query2.Miktar,
  query2.Toplam
from #TempTableForQuery1 as query1
left join #TempTableForQuery2 as query2 
  on query1.prfa_type = query2.prfa_type
order by query1.prfa_nametr

你也可以通过做一些更混乱的事情来逃避,比如

from (entire content of query 1) as query1
join (entire content of query 2) as query2 on ...

还有其他方法不那么混乱,但可能有点复杂。

-Update-请求完整代码,完整代码如下(注意from子句之前的into,这为我们创建了临时表)。您应该能够将以下所有内容粘贴到管理工作室并执行。

--first query SELECT prfa_nametr, prfa_type, Sum(orit_quantity) AS Miktar, Sum(CASE WHEN orit_currencyid = 1 THEN orit_sumnetamount - orit_sumnetamount * oram_discountrate WHEN orit_currencyid = 2 THEN orit_sumnetamount * 2 - orit_sumnetamount * oram_discountrate * 2 WHEN orit_currencyid = 3 THEN orit_sumnetamount * 3 - orit_sumnetamount * oram_discountrate * 3 WHEN orit_currencyid = 4 THEN orit_sumnetamount * 4 - orit_sumnetamount * oram_discountrate * 4 END) AS Toplam into #TempTableForQuery1 FROM orderitem_crt INNER JOIN product_crt ON orit_productid = prod_id INNER JOIN productfamily_crt ON prod_productfamilyid = prfa_id INNER JOIN order_crt ON orit_orderid = orde_id INNER JOIN orderamount_crt ON oram_orderid = orde_id WHERE orit_sumnetamount <> 0 AND orit_alternativeno = 0 AND orde_deleted IS NULL AND orde_stageid <> 5 AND orit_deleted IS NULL GROUP BY prfa_nametr, prfa_type --second query SELECT prfa_type, Sum(orit_quantity) AS Miktar, Sum(CASE WHEN orit_currencyid = 1 THEN orit_sumnetamount - orit_sumnetamount * oram_discountrate WHEN orit_currencyid = 2 THEN orit_sumnetamount * 2 - orit_sumnetamount * oram_discountrate * 2 WHEN orit_currencyid = 3 THEN orit_sumnetamount * 3 - orit_sumnetamount * oram_discountrate * 3 WHEN orit_currencyid = 4 THEN orit_sumnetamount * 4 - orit_sumnetamount * oram_discountrate * 4 END) AS Toplam into #TempTableForQuery2 FROM orderitem_crt INNER JOIN product_crt ON orit_productid = prod_id INNER JOIN productfamily_crt ON prod_productfamilyid = prfa_id INNER JOIN order_crt ON orit_orderid = orde_id INNER JOIN orderamount_crt ON oram_orderid = orde_id WHERE orit_sumnetamount <> 0 AND orit_alternativeno = 0 AND orde_deleted IS NULL AND orde_stageid <> 5 AND orit_deleted IS NULL GROUP BY prfa_type --joining query select query1.prfa_nametr, query1.prfa_type, query1.Miktar, query1.Toplam, query2.Miktar, query2.Toplam from #TempTableForQuery1 as query1 left join #TempTableForQuery2 as query2 on query1.prfa_type = query2.prfa_type order by query1.prfa_nametr --drop temp tables drop table #TempTableForQuery1 drop table #TempTableForQuery2