3
I have a table with the following data:
+---------------------------+
| Cod_Art | Armazem | Quant |
+---------------------------+
| 11430001 | 1 | 0 |
| 11430001 | 2 | 3 |
| 11430001 | 3 | 0 |
| 11430001 | 4 | 1 |
+---------------------------+
What I need is to execute a query
with the sum of the quantities of:
Armazem 1 + Armazem 2 + Armazem 3 - Armazem 4
In that case the result is 2.
My query
this way:
select cod_Art, (sum(quant) - (select quant from ccartigos_stock where armazem = 4 and Cod_Art = '11430001')) as 'quant'
from ccartigos_stock
where (armazem = 1 or Armazem = 2 or Armazem = 3) and Cod_Art = '11430001'
group by cod_art
order by cod_Art
However I don’t want to limit the search only to the article 11430001
but yes to all.
However, when removing the where Cod_Art = '11430001'
I get the following error:
Msg 512, Level 16, State 1, Line 6 Subquery returned more than 1 value. This is not permitted when the subquery Follows =, != , <, <= ,> , >= or when the subquery is used as an Expression.
You want to get just one result line that adds up the total amount (in this case it will be 4) of the items containing ids 1, 2, 3 and 4, that’s it?
– Pedro Camara Junior