I heartily recommend McNets' answer. However, I thought I'd try to explain what the problem with your original query was.
That query again (in case it changes as you work through the question):
select sum(vit_codigotiposexames) from
(
select * from tb_vitima where vit_codigotiposexames
in (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22 )
)Corpo_Delito,
sum(vit_codigotiposexames) from
(
select * from tb_vitima where vit_codigotiposexames = 21 )
)Cadaverico
Your entire SELECT
list (the columns to return) must proceed your FROM
clause, and you can only have one FROM
clause in a query.
You can break this into two queries:
select sum(vit_codigotiposexames) from
(
select * from tb_vitima where vit_codigotiposexames
in (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22 )
)Corpo_Delito
;
select sum(vit_codigotiposexames) from
(
select * from tb_vitima where vit_codigotiposexames = 21 )
)Cadaverico
To turn this into a single query, I would use McNets' solution. However, there is a solution that looks more like what you were going for.
You can JOIN
the two queries. Since each of the separate queries will return only one row, a CROSS JOIN (which matches each row in the first dataset to each row in the second [with a count of M rows in the first and N in the second, the results are M*N rows]) will work fine (1 * 1 = 1). First, we simplify the original two queries, and give the results the names you gave the two queries:
select sum(vit_codigotiposexames) as Corpo_Delito
from tb_vitima
where vit_codigotiposexames in (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22 )
;
select sum(vit_codigotiposexames) as Cadaverico
from tb_vitima
where vit_codigotiposexames = 21
;
Then, we turn the queries into subqueries, and join them:
select Corpo_Delito, Cadaverico
from (select sum(vit_codigotiposexames) as Corpo_Delito
from tb_vitima
where vit_codigotiposexames in (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22 )
) sq1
CROSS JOIN -- This can just be a comma, but this make the intent clearer when you look at the code in the future.
(select sum(vit_codigotiposexames) as Cadaverico
from tb_vitima
where vit_codigotiposexames = 21
) sq2
;
Like I said, I would use McNets' solution, but hopeful this helps you understand why what you first tried didn't work as planned.
vit_codigotiposexames <> 21
orvit_codigotiposexames not in (21)
?