?
Solved

Need to count rows in case statement

Posted on 2009-05-05
4
Medium Priority
?
552 Views
Last Modified: 2012-05-06
I want to count the rows that are unique to each other but need some assistance.  I've renamed the rows in the case statement  so the group by will not work as I'm trying it.  Please help to count the number of unique rows.  THANKS!!!
SELECT  
	CASE	
		WHEN m.answer_value = 'Yes'  THEN m.question_txt  
		ELSE m.list_value_txt 
	END AS 'Total_Closed_Cases_Where_the_CM',
0 as 'Number_Of_Closed_Cases'
INTO #counts  
FROM #members  
 
OUTPUT FROM ABOVE CODE:
Total_Closed_Cases_Where_the_CM	Number_Of_Closed_Cases
Worked AAA	0
Worked AAA	0
Worked AAA	0
Extended BBB	0
Extended BBB	0
Extended BBB	0
Extended BBB	0
Negotiated CCC	0
Negotiated CCC	0
Provided DDD	0
Provided DDD	0
Provided DDD	0
 
I WANT THE OUTPUT TO BE:
Worked AAA        3
Extended BBB      4
Negotiated CCC    2
Provided DDD       3

Open in new window

0
Comment
Question by:lcalabro
[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
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Emes earned 1600 total points
ID: 24307491
need to add a group by

select count(m.question_txt) ,
CASE    
                WHEN m.answer_value = 'Yes'  THEN m.question_txt  
                ELSE m.list_value_txt
        END AS 'Total_Closed_Cases_Where_the_CM'
       
                    INTO #counts  
FROM #members  
group by
            CASE    
                WHEN m.answer_value = 'Yes'  THEN m.question_txt  
                ELSE m.list_value_txt
        END AS 'Total_Closed_Cases_Where_the_CM'
0
 

Author Comment

by:lcalabro
ID: 24307752
It's giving me an error:
Msg 156, Level 15, State 1, Line 163
Incorrect syntax near the keyword 'AS'.  on the END AS in the group by.

I need to group it by the 'Total_Closed_Cases_Where_the_CM'
but that column is a combination of m.question_txt and m.list_value_txt.
Any ideas of what to try???
0
 
LVL 27

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 400 total points
ID: 24307832
you do not put an alias on the group by
select count(m.question_txt) ,
CASE    
                WHEN m.answer_value = 'Yes'  THEN m.question_txt  
                ELSE m.list_value_txt 
        END AS 'Total_Closed_Cases_Where_the_CM'
        
                    INTO #counts  
FROM #members  
group by 
            CASE    
                WHEN m.answer_value = 'Yes'  THEN m.question_txt  
                ELSE m.list_value_txt 
        END

Open in new window

0
 
LVL 14

Expert Comment

by:Emes
ID: 24308011
yes take out the alias it is messing it up

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Have you ever had to extract data from a Microsoft SQL Server database and export it to an Excel file, but did not want to use a DTS package? The concept in this article is not new, but it is the answer and will also work on 64-bit SQL boxes.   …
A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

770 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