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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Need to count rows in case statement

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
lcalabro
Asked:
lcalabro
  • 2
2 Solutions
 
EmesCommented:
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
 
lcalabroAuthor Commented:
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
 
Chris LuttrellSenior Database ArchitectCommented:
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
 
EmesCommented:
yes take out the alias it is messing it up

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now