Do not execute the query for each identifier separately. Instead, run one query for all identifiers, using a group to get the p and s values ββfor each id and parameterized in clause (or, even better, a stored procedure with a table parameter).
Here is the version of the in request:
select Id, sum(convert(decimal(18,3),tbl.Price)) p, sum(convert(decimal(18,2),tbl.Sale)) s from table1 tbl Where Id IN(<1,2,3,4....>) group by Id
Replace <1,2,3,4....> parameters described in this answer.
The following is a table of query parameter values:
select tbl.Id, sum(convert(decimal(18,3),tbl.Price)) p, sum(convert(decimal(18,2),tbl.Sale)) s from table1 tbl inner join @items i on tbl.Id = i.Id group by tbl.Id
For a detailed explanation of using table parameters, read this answer.
source share