Solved

Need to count rows in case statement

Posted on 2009-05-05
4
545 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yes take out the alias it is messing it up

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This code started out as a fix for a customer that had incoming data that was hunderds of numbers and words long that was to fit in one column. The problem was that the customer did not want to split words or numbers when wrapping in the column. …
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now