SQL query that adds the STOCK of CodeArticulo equal

2

I'm running a system on VB.net where I have a datagridview (MultiSelect) with the company's sales record. In another datagridview I show the articles of the selected sale (s) in the first DGV

"Select IdVenta, CodigoArticulo, Descripcion, Precio, Cantidad from Articulo_Venta WHERE " & ventas & " ORDER BY CodigoArticulo ASC"

The variable Sales is a chain of OR concatenations (eg SalesID = 3 OR SalesID = 2 OR SalesID = 6) from the following code:

Dim ventas As String = String.Empty
Dim contador As Integer = 1
For Each row As DataGridViewRow In IdVenta
    If contador = IdVenta.Count Then
        ventas = ventas & "IdVenta = " & row.Cells("IdVenta").Value
    Else
        ventas = ventas & "IdVenta = " & row.Cells("IdVenta").Value & " OR "
        contador += 1
    End If
Next

Everything works perfectly but in the resulting query, CodeArticle is repeated and it shows me the same article several times (Sold at different times or by different vendors), that is:

  

CodeArticle 1 | ... | Quantity = 3

     

CodeArticle 4 | ... | Quantity = 2

     

CodeArticle 1 | ... | Quantity = 6

and I need that when there is more than one CodeArticulo repeated I show it only once but with its summed amounts

  

CodeArticle 1 | ... | Quantity = 9

    
asked by Gonzalo 18.05.2016 в 15:53
source

1 answer

0

You need to use Group By

SELECT IdVenta, CodigoArticulo, SUM(Cantidad) 'Cantidad', Descripcion, Precio 
FROM Articulo_Venta
WHERE " & ventas & " 
GROUP BY CodigoArticulo, IdVenta, Descripcion, Precio
ORDER BY CodigoArticulo ASC"
    
answered by 18.05.2016 / 16:07
source