[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Nested Case Statement Issue - over level 10....??

Posted on 2008-11-07
6
Medium Priority
?
1,908 Views
Last Modified: 2013-11-05
Experts:

Can someone give me a work-around or tell me what the real issue is with this SQL Script..? When I uncomment the rest of my agent's names, I get this error:

Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 125, Level 15, State 4, Line 1
Case expressions may only be nested to level 10.

--KPI SALES AGENT CALL COUNTS
select distinct
 (case when extension = '210' then 'PAULA'
           when extension = '204' then 'LINA'
           when extension = '208' then 'BEN'
           when extension = '202' then 'RETHA'
           when extension = '221' then 'QUINTIN'
           when extension = '220' then 'MARCUS'
           when extension = '219' then 'ROBERT'
           when extension = '213' then 'RICHARD'
--           when extension = '212' then 'MARIAN'
--           when extension = '206' then 'CURTIS'
--           when extension = '214' then 'MIGUEL'
--           when extension = '215' then 'ALONZO'
--           when extension = '216' then 'NATINA'
                 else 'OTHER' end ) as [SALES AGENT]
 , date as [DATE]
 , COUNT(CALLID) as [CALL COUNT]
from dell2850.report_smdrdata.dbo.tblsmdrdata
where date between '2008-10-20' and getdate()
  and importfilename like 'acd%'
  and ani like '9720%'
 and extension not in  ('***', 'S35','SYS')
group by  
  (case when extension = '210' then 'PAULA'
           when extension = '204' then 'LINA'
           when extension = '208' then 'BEN'
           when extension = '202' then 'RETHA'
           when extension = '221' then 'QUINTIN'
           when extension = '220' then 'MARCUS'
           when extension = '219' then 'ROBERT'
           when extension = '213' then 'RICHARD'
--           when extension = '212' then 'MARIAN'
--           when extension = '206' then 'CURTIS'
--           when extension = '214' then 'MIGUEL'
--           when extension = '215' then 'ALONZO'
--           when extension = '216' then 'NATINA'
                 else 'OTHER' end )
 ,date
0
Comment
Question by:MIKE
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Cedric_D
ID: 22906801
Try rewrite it in form:

CASE extension
   WHEN '111' THEN 'NNN'
   WHEN '222' THEN 'MMM'
  .........
END
0
 
LVL 7

Expert Comment

by:Cedric_D
ID: 22906836
Secon suggestion: to not group by so complex field, rewrite as sub-query:

SELECT name, [date], COUNT(CALLID) as [CALL COUNT]
(
SELECT
  (CASE ........ ) AS name
 , date as [DATE]
 FROM ...
) as TT
GROUP BY name, date

0
 
LVL 9

Accepted Solution

by:
Ernariash earned 1200 total points
ID: 22906922
This is a limitation of the CASE statement; you cannot have more than 10 WHENs if it is running on a linked server.
You could have a sub query with a temporary table or Table variable (@Extension, or  #Extensions) as:
Extension, Name
'210',  'PAULA'
'204' , 'LINA'
'208', 'BEN'& and so on
Then use the following query: hope it help

select distinct e.Name  as [SALES AGENT]
 , date as [DATE]
 , COUNT(CALLID) as [CALL COUNT]
from dell2850.report_smdrdata.dbo.tblsmdrdata d
left outer join #Extensions e
on d.Extension = e.Extension
where date between '2008-10-20' and getdate()
  and importfilename like 'acd%'
  and ani like '9720%'
 and extension not in  ('***', 'S35','SYS')
group by  e.Name,date

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Ernariash
ID: 22906985

For others use the IsNull function, and the Extension field for the temp table has to be unique or PK
Thanks.

--KPI SALES AGENT CALL COUNTS
select distinct isnull(e.Name, 'OTHER')  as [SALES AGENT]
 , date as [DATE]
 , COUNT(CALLID) as [CALL COUNT]
from dell2850.report_smdrdata.dbo.tblsmdrdata d
left outer join #Extensions e
on d.Extension = e.Extension
where date between '2008-10-20' and getdate()
  and importfilename like 'acd%'
  and ani like '9720%'
 and extension not in  ('***', 'S35','SYS')
group by isnull(e.Name, 'OTHER') ,date

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 800 total points
ID: 22909844
>>Can someone give me a work-around or tell me what the real issue is with this SQL Script..? <<
Yes.  You need to stop hardcoding you values and place them in a table.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22909847
Clarification:
You need to stop hardcoding you values and place them in a well indexed table.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 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