将结果加总为全部

时间:2019-02-04 23:44:00

标签: sql sql-server

我在SQL中有以下查询。

SELECT cast(A.CodigoArticulo as varchar) CodArticulo
  , cast(ap.Codigo as varchar ) CodArtProveedor
  , dlp.Precio Publico
  , 0 Interior 
from Articulos a 
join ArticuloProveedores ap on ap.ArticuloId = a.Id 
join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
where lp.Id in ('1') 
union all
SELECT cast(A.CodigoArticulo as varchar) CodArticulo
  , cast(ap.Codigo as varchar ) CodArtProveedor
  , 0 
  , dlp.Precio  
FROM Articulos a
join ArticuloProveedores ap on ap.ArticuloId = a.Id
join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
where lp.Id in ('4')

我得到的结果如下:

CodArticulo CodArtProveedor Publico     Interior

44380       K-7             697         0 
44380       K-7             0           767
00003       IM2757          0           2030
00003       IM2757          1845        0
00006       MTRJ6           156         0
00006       MTRJ6           0           172
00010       BERJ6           156         0
00010       BERJ6           0           172

我需要SUM ()PUBLICO列。我想要的输出如下所示。

INTERIOR

建议使用什么方法获得预期的输出?

5 个答案:

答案 0 :(得分:1)

我建议将这些UNION结果输入到临时表##tempResults中,然后使用Group BySum

select CodArticulo, CodArtProveedor,SUM(Publico) ,SUM(Interior) from ##tempResults
group by CodArticulo,CodArtProveedor

答案 1 :(得分:1)

我对您的数据模型并不完全熟悉,但是根据查询,在SUM中放置一个条件可能使您可以一次完成查询。在下面的示例中,将执行一次单选(无并集),并且在SUM中使用了CASE语句以获取所需的结果:

SELECT   cast(A.CodigoArticulo as varchar)  CodArticulo
        ,cast(ap.Codigo as varchar )        CodArtProveedor
        ,SUM(CASE WHEN '1' THEN dlp.Precio ELSE 0 END)                      Publico
        ,SUM(CASE WHEN '4' THEN dlp.Precio ELSE 0 END)                      Interior 
FROM    Articulos a 
            JOIN ArticuloProveedores ap     ON ap.ArticuloId    = a.Id 
            JOIN DetallesListaPrecios DLP   ON DLP.ArticuloId   = A.Id 
            JOIN ListasPrecios lp           ON lp.Id            = dlp.ListaPreciosId
WHERE lp.Id in ('1', '4') 
GROUP BY A.CodigoArticulo, ap.Codigo

答案 2 :(得分:1)

我建议使用CTE

;WITH cte(CodArticulo,CodArtProveedor,Publico,Interior) AS
(SELECT cast(A.CodigoArticulo as varchar) CodArticulo
  , cast(ap.Codigo as varchar ) CodArtProveedor
  , dlp.Precio Publico
  , 0 Interior 
from Articulos a 
join ArticuloProveedores ap on ap.ArticuloId = a.Id 
join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
where lp.Id in ('1') 
union all
SELECT cast(A.CodigoArticulo as varchar) CodArticulo
  , cast(ap.Codigo as varchar ) CodArtProveedor
  , 0 
  , dlp.Precio  
FROM Articulos a
join ArticuloProveedores ap on ap.ArticuloId = a.Id
join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
where lp.Id in ('4'))
select CodArticulo,CodArtProveedor,SUM(Publico),Sum(Interior) from cte group by CodArticulo,CodArtProveedor

答案 3 :(得分:0)

第2个可能的答案中的第一个:

将您的UNION查询放入“通用表表达式”或“子查询”中,然后按条件将组放在外面。下面的示例:

SELECT   CodArticulo
        ,CodArtProveedor
        ,SUM(Publico)           as TotalPublico
        ,SUM(Interior)          as TotalInterior
FROM    (
            SELECT   cast(A.CodigoArticulo as varchar)  CodArticulo
                    ,cast(ap.Codigo as varchar )        CodArtProveedor
                    ,dlp.Precio                         Publico
                    ,0                                  Interior 
            FROM    Articulos a 
                        JOIN ArticuloProveedores ap     ON ap.ArticuloId    = a.Id 
                        JOIN DetallesListaPrecios DLP   ON DLP.ArticuloId   = A.Id 
                        JOIN ListasPrecios lp           ON lp.Id            = dlp.ListaPreciosId
            WHERE lp.Id in ('1') 

            UNION ALL

            SELECT   cast(A.CodigoArticulo as varchar)  CodArticulo
                    ,cast(ap.Codigo as varchar )        CodArtProveedor
                    ,0 
                    ,dlp.Precio  
            FROM    Articulos a
                    JOIN ArticuloProveedores ap         ON ap.ArticuloId    = a.Id
                    JOIN DetallesListaPrecios DLP       ON DLP.ArticuloId   = A.Id 
                    JOIN ListasPrecios lp               ON lp.Id            = dlp.ListaPreciosId
            WHERE lp.Id in ('4')
        ) d
GROUP BY CodArticulo, CodArtProveedor

答案 4 :(得分:0)

这是您对#Temp

的查询
SELECT cast(A.CodigoArticulo as varchar) CodArticulo
      , cast(ap.Codigo as varchar ) CodArtProveedor
      , dlp.Precio Publico
      , 0 Interior 
    INTO #TEMP  
    from Articulos a 
    join ArticuloProveedores ap on ap.ArticuloId = a.Id 
    join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
    join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
    where lp.Id in ('1') 
    union all
    SELECT cast(A.CodigoArticulo as varchar) CodArticulo
      , cast(ap.Codigo as varchar ) CodArtProveedor
      , 0 
      , dlp.Precio  
    FROM Articulos a
    join ArticuloProveedores ap on ap.ArticuloId = a.Id
    join DetallesListaPrecios DLP on DLP.ArticuloId = A.Id 
    join ListasPrecios lp on lp.Id = dlp.ListaPreciosId
    where lp.Id in ('4')

这就是您要做的,仅此而已。

 select sum(Publico),sum(Interior), CodArticulo, CodArtProveedor
 from #TEMP 
 group by CodArticulo, CodArtProveedor