[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need to count rows in case statement

Posted on 2009-05-05
4
Medium Priority
?
553 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.   …
Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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