Solved

Case statement to sum values - T-SQL

Posted on 2016-10-13
3
55 Views
Last Modified: 2016-10-13
I'm trying to write a case statement  that sums up values, but the one below is producing null values. What am I doing wrong? *In a nutshell, (NL * 3%) + (DP * 1%) + (PR) = NR.

 sum((case
  when METRIC_CD = ''NL'' then
  MTD_ACT * 0.03 else null end) +
  (case when METRIC_CD = ''DP'' then
  MTD_ACT * 0.01 else null end) +
  (case when METRIC_CD = ''PR'' then
  MTD_ACT * 1 else null end)) as NR

Below is the data  as it appears in the table:

METRIC_CD      MTD_ACT
NL                       1200
DP                         500
PR                      3150
DP                      2250
PR                        550
NL                    11000


Thank you for your tremendous assistance.
0
Comment
Question by:saved4use
3 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 450 total points
ID: 41842669
Instead of else null use else 0 (that's zero).

Adding 0 won't affect your total but adding NULL will yield a NULL result. Take the following examples:

SELECT 1 + 3 + 0 + 5 => returns 9

SELECT 1 + 3 + NULL + 5 => returns NULL

The same holds true for the SUM() function. A NULL value in the aggregate will yield NULL.
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 50 total points
ID: 41842678
Russ is correct, as NULL added to anything else will equal NULL.  And to make this a hell of a lot easier to read...
 SUM(CASE METRIC_CD 
   WHEN 'NL' THEN MTD_ACT * 0.03 
   WHEN 'DP' THEN MTD_ACT * 0.01 
   WHEN 'PR' THEN MTD_ACT * 1 ELSE 0 END) as NR

Open in new window

0
 

Expert Comment

by:mbazar
ID: 41842969
Try this

sum((case
  when METRIC_CD = ''NL'' then MTD_ACT * 0.03
  when METRIC_CD = ''DP'' then MTD_ACT * 0.01
  when METRIC_CD = ''PR'' then MTD_ACT * 1
  else 0 end)) as NR
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now