Query with two summation results from a condition

Asked

Viewed 23 times

1

I need to perform a query in a table of sold items where there is a Tipoitem field that says if the item is Product(1) or Service(2), I would like it to bring me two SUM’s from one of the Ocisubtot field, where if it was product(1) it brings me a SUM and if it is service(2) he brings me another column with SUM for services. Would it be possible?

Example:

SELECT 
  SUM(Ocisubtot) AS 'Total Produto', 
  SUM(Ocisubtot) AS 'Total Serviço'
FROM Orvendalevel1

Tipoitem has 1 and 2 for condition.

I am using SQL Server, I do not know if you should use IF or CASE in this case and how to apply.

  • Pivot com case https://stackoverflow.com/questions/5846007/sql-query-to-pivot-a-column-using-case-when

1 answer

0

This would be possible using sub-query for each attribute, for example

SELECT 
    (
        SELECT SUM(Ocisubtot) FROM Orvendalevel1 WHERE cod_product = 1
    ) AS 'Total Produto'
    (
        SELECT SUM(Ocisubtot) FROM Orvendalevel1 WHERE cod_product = 2
    ) AS 'Total Serviço'
;
  • Thank you very much, that’s exactly what I needed !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.