Help with a SQL query, sums

0

I'm doing an SQL in which I want to add whose product is similar that is.

I have my sql, I attach them to you.

select distinct cat.Nombre as Categoria, pro2.Nombre as Producto, InventarioInicial, ocd.CostoUnitario
from Inventario as inv2
inner join (
        select
        inv.ProductoID,
        pro.Nombre,
        count(inv.ProductoID) as InventarioInicial
        from Inventario as inv
        inner join Productos as pro on pro.ID = inv.ProductoID
        where inv.Consumido <> 1
        group by pro.Nombre, inv.ProductoID

        ) as c2 on c2.ProductoID = inv2.ProductoID
inner join Productos as pro2 on pro2.ID = inv2.ProductoID
inner join Categorias as cat on cat.ID = pro2.CategoriaID
left join OrdenDeCompraDetalles as ocd on ocd.ProductoID = pro2.ID

This is the result.

I would like to add similar products, that is, the CostoUnitario field Whose Product Name is the same, so as not to have duplicates the products with different unit cost. I hope you help me or advise me. Thanks.

    
asked by JuanL 12.07.2018 в 19:04
source

1 answer

0

You only have to group the fields with a GROUP BY and the fields that are numeric values the sums with a SUM , Asi

select distinct 
    cat.Nombre as Categoria, 
    pro2.Nombre as Producto, 
    SUM(InventarioInicial) as InventarioInicial, 
    SUM(ocd.CostoUnitario) as CostoUnitario
from Inventario as inv2
inner join (
        select
        inv.ProductoID,
        pro.Nombre,
        count(inv.ProductoID) as InventarioInicial
        from Inventario as inv
        inner join Productos as pro on pro.ID = inv.ProductoID
        where inv.Consumido <> 1
        group by pro.Nombre, inv.ProductoID

        ) as c2 on c2.ProductoID = inv2.ProductoID
inner join Productos as pro2 on pro2.ID = inv2.ProductoID
inner join Categorias as cat on cat.ID = pro2.CategoriaID
left join OrdenDeCompraDetalles as ocd on ocd.ProductoID = pro2.ID
GROUP BY cat.Nombre,pro2.Nombre
    
answered by 12.07.2018 в 19:19