Solved

Query and sum a  SQL table based on multiple parameters

Posted on 2014-11-20
3
169 Views
Last Modified: 2014-12-18
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!
0
Comment
Question by:Saxitalis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40455226
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') )

Open in new window

> LotB= ‘L’ (NumTotal will = 10)
btw might be a typo, based on the sample data 1L would = 10
0
 
LVL 9

Accepted Solution

by:
sarabhai earned 500 total points
ID: 40464284
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
0
 

Author Closing Comment

by:Saxitalis
ID: 40508497
thank you
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question