2017-01-04 218 views
0

我是SQL新手! 我有兩個來自不同表格的查詢。這是我第一次查詢:SQL:合併兩個不同的查詢

SELECT cli.CLIENTE as clientet,cli.RAZON as nom, 
     SUM(distinct par.[RECAPTACIO]*0.9) AS recaudacionSINIVA, 
     SUM(distinct par.canvi) as cambiocargado, 
     SUM(distinct par.consum) as CC_recaptacio,count(distinct par.DATA) as visita, 
     SUM((c.quantitat + c.caixes * c.uxcaja) * c.pvp) as CC_Caducados 

FROM [V30].[dbo].[parmaq] as par,[V30].[dbo].[clientes] as cli , V30.dbo.parcons as c 
    where cli.CLIENTE=par.EMPRESA and par.enllas=c.enllas 
    and par.data > '13-11-2015' and par.data < '20-11-2015' 
    group by cli.CLIENTE,cli.RAZON 

這是結果: enter image description here

這是我的第二個查詢:

SELECT cli.cliente as clientet, cli.RAZON, SUM(distinct m1.IMPORTE) as FACTURACION, 
     SUM(distinct m2.coste * m2.canti) as CC_facturacio 
FROM [V30].[dbo].[clientes] as cli, [V30].[dbo].[MESTA1] as m1,V30.dbo.MESTA2 as m2 
     where m2.albaran=m1.albaran and m1.CLIENTE=cli.CLIENTE 
     and m1.FECHA > '13-11-2015' and m1.FECHA < '30-11-2015' 
     group by cli.CLIENTE,cli.razon order by cli.cliente 

而第二個查詢的結果: enter image description here

在sql上沒有辦法將表與prim ary鍵!並且查詢結果1和2都很好。我想要做的就是使用「clientet」列結合結果。我不得不說,你可以得到不同數量的行。

目標是讓所有這些信息具有相同的結果。例如,如果「clientet」在兩個查詢中都有結果,則最終結果應該是來自第一個查詢的所有列+第二個查詢中的「FACTURACION」和「CC_facturacio」。

希望你能幫助我,我一直在嘗試Inner Joins和子查詢,但是沒有辦法得到這個。

+0

UNION ALL? (在第二個查詢中需要一些NULL列) – jarlh

+0

作爲這兩個查詢的結果,是否會有相同的客戶端(相同的ID)?兩個查詢的結果中是否可能出現clientit爲101500的客戶端? –

+0

**附註:**停止使用舊式連接。顯式連接是ANSI-SQL的一部分,已有20多年了。 –

回答

1

兩個選項:

  1. 由於jarlh建議,你把佔位每個查詢不存在的,並確保他們在同一個缺少列,你可以做一個工會,只要訂購。

  2. 你可以做你的兩個查詢的完整外連接。您可能需要將coalesce添加到每個其他列,具體取決於在兩個表中不存在行時是否要爲列顯示空值。下面

該示例假定clientet是接合柱:

SELECT COALESCE(TBL_1.clientet, TBL_2.clientet) as clientet 
    , TBL_1.nom 
    , TBL_1.recaudacionSINIVA 
    , TBL_1.cambiocargado 
    , TBL_1.CC_recaptacio 
    , TBL_1.visita 
    , TBL_1.CC_Caducados 
    , TBL_2.FACTURACION 
    , TBL_2.CC_facturacio 
FROM 

     (SELECT cli.CLIENTE as clientet 
      , cli.RAZON as nom 
      , SUM(distinct par.[RECAPTACIO]*0.9) AS recaudacionSINIVA 
      , SUM(distinct par.canvi) as cambiocargado 
      , SUM(distinct par.consum) as CC_recaptacio 
      , count(distinct par.DATA) as visita 
      , SUM((c.quantitat + c.caixes * c.uxcaja) * c.pvp) as CC_Caducados  
     FROM [V30].[dbo].[parmaq] as par,[V30].[dbo].[clientes] as cli , V30.dbo.parcons as c 
      where cli.CLIENTE=par.EMPRESA and par.enllas=c.enllas 
      and par.data > '13-11-2015' and par.data < '20-11-2015' 
      group by cli.CLIENTE,cli.RAZON) TBL_1 

FULL OUTER JOIN 

     (SELECT cli.cliente as clientet 
      , cli.RAZON 
      , SUM(distinct m1.IMPORTE) as FACTURACION 
      , SUM(distinct m2.coste * m2.canti) as CC_facturacio 
     FROM [V30].[dbo].[clientes] as cli, [V30].[dbo].[MESTA1] as m1,V30.dbo.MESTA2 as m2 
      where m2.albaran=m1.albaran and m1.CLIENTE=cli.CLIENTE 
      and m1.FECHA > '13-11-2015' and m1.FECHA < '30-11-2015' 
      group by cli.CLIENTE,cli.razon) TBL_2 

    ON TBL_1.clientet = TBL_2.clientet 
ORDER BY COALESCE(TBL_1.clientet, TBL_2.clientet) 
+0

這完全有效,只需複製粘貼!非常感謝! – talendguy