# Query and sum a SQL table based on multiple parameters

Hello Experts,

I have the following table: Tb_1
Date      Num      Code      LotA      LotB
1/22      10            110        8                1L
1/22      12.5         110        10              2L
1/22      13             112         8              L
1/22      2.5            112         6              2L
1/23      3.5            114         8              M
1/23      0.5            114         8              L
1/23      1               114        32           2L

Code is type INT
LotA is type INT
LotB is type varchar (50)

I would like to summarize Tb_1 in the following way:

Select SUM(Num) As NumTotal  From Tbl_1
Where Code = 110 and LotA = 8 and LotB= ‘L’ (NumTotal will = 10)
+
Where Code = 114 and LotA IN(8,32) and LotB = ‘M’ or ‘2L’ (NumTotal will = 3.5 + 1 = 4.5)

The query should return  14.5  for NumTotal.

Does anyone know the proper syntax to do this?

Thanks!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SQL Server Data DudeCommented:
Give this a whirl..
``````SELECT SUM(Num) as num_sum
FROM Tb_1
WHERE
(Code = 110 AND LotA = 8 AND LotB = 'L') OR
(Code = 114 AND LotA IN (8, 32) AND LotB IN ('M', '2L') )
``````
> LotB= ‘L’ (NumTotal will = 10)
btw might be a typo, based on the sample data 1L would = 10
DeveloperCommented:
SELECT
(SELECT SUM(Num) AS NumTotal  FROM Tbl_1
WHERE Code = 110 AND LotA = 8 AND LotB= ‘L’ )
+
(SELECT SUM(Num) AS NumTotal  FROM Tbl_1
WHERE Code = 114 AND LotA IN(8,32) AND LotB IN ( ‘M’ , ‘2L’ ))
AS NumTotal

Experts Exchange Solution brought to you by