Solved

Need to count rows in case statement

Posted on 2009-05-05
4
550 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 400 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 26

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 100 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Written by Valentino Vranken. A while ago I wrote an article called Chart Optimization Tips (http://www.experts-exchange.com/articles/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Chart-Optimization-Tips.html).  This article explained how …
Introduction As you'll probably know, a data region in a SQL Server Reporting Services report can be linked to only one dataset.  This makes it troublesome when you need to display data from more than one dataset in the same data region.  SQL Serve…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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