Solved

Need to count rows in case statement

Posted on 2009-05-05
4
548 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
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.   …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

777 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