SQL:合并两个不同的查询

时间:2017-01-04 15:21:26

标签: sql sql-server database merge

我是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上无法将表与主键链接!查询一和二的结果很好。我想要做的是使用“clientet”列结合结果。我不得不说你可以得到不同数量的行。

目标是将所有这些信息都放在同一个结果上。例如,如果“clientet”在两个查询中都有结果,则最终结果应该是第一个查询中的所有列+第二个查询中的“FACTURACION”和“CC_facturacio”。

希望你能帮助我,我一直在尝试内联和子查询但是没有办法得到这个。

1 个答案:

答案 0 :(得分:1)

两个选项:

  1. 正如jarlh建议的那样,只要你在每个查询中都放置了不存在的列,并确保它们的顺序相同,你就可以建立联盟。

  2. 您可以对两个查询进行完全外连接。您可能希望为每个其他列添加合并,具体取决于当两个表中不存在行时是否希望为列显示空值。

  3. 以下示例假设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)