Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server Query sum case

Posted on 2014-12-10
10
Medium Priority
?
219 Views
Last Modified: 2014-12-10
I have a problem with a particular query that I am working on.  The following two queries work fine:

SELECT
    sum(case when ((r3000+r4000+r6000) / 3) < '26' then 1 else 0 end) as 'NORMAL',
    sum(case when ((r3000+r4000+r6000) / 3) between '26' and '40' then 1 else 0 end) as 'MILD',
    sum(case when ((r3000+r4000+r6000) / 3) between '41' and '55' then 1 else 0 end) as 'MODERATE',
    sum(case when ((r3000+r4000+r6000) / 3) between '56' and '70' then 1 else 0 end) as 'MODERATELY-SEVERE',
    sum(case when ((r3000+r4000+r6000) / 3) between '71' and '90' then 1 else 0 end) as 'SEVERE',
    sum(case when ((r3000+r4000+r6000) / 3) > '90' then 1 else 0 end) as 'PROFOUND'
FROM Audio

SELECT
    sum(case when ((l3000+l4000+l6000) / 3) < '26' then 1 else 0 end) as 'NORMAL',
    sum(case when ((l3000+l4000+l6000) / 3) between '26' and '40' then 1 else 0 end) as 'MILD',
    sum(case when ((l3000+l4000+l6000) / 3) between '41' and '55' then 1 else 0 end) as 'MODERATE',
    sum(case when ((l3000+l4000+l6000) / 3) between '56' and '70' then 1 else 0 end) as 'MODERATELY-SEVERE',
    sum(case when ((l3000+l4000+l6000) / 3) between '71' and '90' then 1 else 0 end) as 'SEVERE',
    sum(case when ((l3000+l4000+l6000) / 3) > '90' then 1 else 0 end) as 'PROFOUND'
FROM Audio

But, I want to combine these two into one, with a condition, so that the sums are performed on the larger of r3000+r4000+r6000 or l3000+l4000+l6000

I cant seem to fine the right query to do this.  Below is what I want, but I am getting an error..
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'as'.


SELECT
      case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
      sum(case when ((r3000+r4000+r6000) / 3) < '26' then 1 else 0 end) as 'NORMAL',
        sum(case when ((r3000+r4000+r6000) / 3) between '26' and '40' then 1 else 0 end) as 'MILD',
        sum(case when ((r3000+r4000+r6000) / 3) between '41' and '55' then 1 else 0 end) as 'MODERATE',
        sum(case when ((r3000+r4000+r6000) / 3) between '56' and '70' then 1 else 0 end) as 'MODERATELY-SEVERE',
        sum(case when ((r3000+r4000+r6000) / 3) between '71' and '90' then 1 else 0 end) as 'SEVERE',
        sum(case when ((r3000+r4000+r6000) / 3) > '90' then 1 else 0 end) as 'PROFOUND'
else             
      sum(case when ((l3000+l4000+l6000) / 3) < '26' then 1 else 0 end) as 'NORMAL',
        sum(case when ((l3000+l4000+l6000) / 3) between '26' and '40' then 1 else 0 end) as 'MILD',
        sum(case when ((l3000+l4000+l6000) / 3) between '41' and '55' then 1 else 0 end) as 'MODERATE',
        sum(case when ((l3000+l4000+l6000) / 3) between '56' and '70' then 1 else 0 end) as 'MODERATELY-SEVERE',
        sum(case when ((l3000+l4000+l6000) / 3) between '71' and '90' then 1 else 0 end) as 'SEVERE',
        sum(case when ((l3000+l4000+l6000) / 3) > '90' then 1 else 0 end) as 'PROFOUND'
end
FROM Audio
0
Comment
Question by:dpinchot
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40491849
>< '26'

must be
< 26

otherwise you are doing a implicit data type conversion ...
the quotes is ONLY for text values, not for numerical values

as 'NORMAL',

must be either
as [NORMAL],
as NORMAL,
as "NORMAL",

again, the single quotes are for string values
the double quotes or [ ] is for names (identifiers), and they are optional unless you have special characters/reserved keywords etc...
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40491865
also, you cannot build 2 column lists with a included CASE construct like this:::

so you must restructure like this:
SELECT
        sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
                           case when ((r3000+r4000+r6000) / 3) < 26  then 1 else 0 end
                   else case when ((l3000+l4000+l6000) / 3) < 26 then 1 else 0 end
              )
        as [NORMAL]

FROM Audio
0
 

Author Comment

by:dpinchot
ID: 40491885
Guy, Thanks for the assistance on the syntax issues.  I modifies the query, but I am getting the same error.

Incorrect syntax near the keyword 'as'.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 60

Expert Comment

by:HainKurt
ID: 40491887
try this

with x as (
select a.*, case when r3000+r4000+r6000 >= l3000+l4000+l6000 then r3000+r4000+r6000/3 else l3000+l4000+l6000/3 end as average
from audio a
)
select *,
case 
when average between 0 and 25 then 'NORMAL'
when average between 26 and 40 then 'MODERATE'
...
end as Status
from x

Open in new window

0
 
LVL 60

Expert Comment

by:HainKurt
ID: 40491895
or if you just need counts

with x as (
select case when r3000+r4000+r6000 >= l3000+l4000+l6000 then r3000+r4000+r6000/3 else l3000+l4000+l6000/3 end as average
from audio a
)
select 
sum(case when average between 0 and 25 then 1 else 0 end ) as 'NORMAL',
sum(case when average between 26 and 40 then 1 else 0 end) as 'MODERATE',
...
from x

Open in new window

0
 

Author Comment

by:dpinchot
ID: 40491905
Guy,

I made a slight modification to your solution, and it ran just fine. t needed an extra end before the last )

SELECT
        sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((r3000+r4000+r6000) / 3) < 26 then 1 else 0 end
              else
            case when ((l3000+l4000+l6000) / 3) < 26 then 1 else 0 end
              end ) as NORMAL
FROM Audio
0
 

Author Comment

by:dpinchot
ID: 40491936
The final solution with Guy's code looks like this, and it works great. Thanks for the help on this.

SELECT
        sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((r3000+r4000+r6000) / 3) < 26 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) < 26 then 1 else 0 end
              end ) as [NORMAL],
        sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((l3000+l4000+l6000) / 3) between 26 and 40 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) between 26 and 40 then 1 else 0 end
              end ) as [MILD],
            sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((l3000+l4000+l6000) / 3) between 41 and 55 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) between 41 and 55 then 1 else 0 end
              end ) as [MODERATE],
            sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((l3000+l4000+l6000) / 3) between 56 and 70 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) between 56 and 70 then 1 else 0 end
              end ) as [MODERATELY-SEVERE],
            sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((l3000+l4000+l6000) / 3) between 71 and 90 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) between 71 and 90 then 1 else 0 end
              end ) as [SEVERE],
            sum(case when r3000+r4000+r6000 >= l3000+l4000+l6000 then
              case when ((l3000+l4000+l6000) / 3) > 90 then 1 else 0 end
              else
                    case when ((l3000+l4000+l6000) / 3) > 90 then 1 else 0 end
              end ) as [PROFOUND]
FROM Audio
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 40491943
the one I posted above (ID: 40491895) should do the same and it is very simple & readable...

did you try that?
0
 

Author Comment

by:dpinchot
ID: 40492296
I am  boarding a flight, but I will try it out when I can get back to my laptop.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40493272
I agree with HainKurt, I think the use of case within case leads to overly complex code.

Just as a tip however you don't have to use WITH AS (...) instead you can use CROSS APPLY like this:
SELECT
      SUM(CASE
            WHEN ca.average BETWEEN 0 AND 25 THEN 1
            ELSE 0
      END) AS 'NORMAL'
    , SUM(CASE
            WHEN ca.average BETWEEN 26 AND 40 THEN 1
            ELSE 0
      END) AS 'MODERATE'--,
FROM audio
      CROSS APPLY (
            SELECT
                  CASE
                        WHEN r3000 + r4000 + r6000 >= l3000 + l4000 + l6000 THEN r3000 + r4000 + r6000 / 3
                        ELSE l3000 + l4000 + l6000 / 3
                  END AS average
      ) AS ca

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

604 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