We help IT Professionals succeed at work.
Get Started

Filtering a CASE statement

pposton
pposton asked
on
465 Views
Last Modified: 2012-08-16
I had some help with the following code, but I forgot to mention that I needed to add some parameters and joins.  I thought I could figure that out, but evidently thought more of myself than I should!  

Anyway I put the procedure below but I need to add something like this:

FROM client inner join activity on client.CharityCkID=activity.CharityCkID
WHERE activity.username = @username AND activity.date >= @dstart AND activity.date <= @dEnd)

I need to include a client's income where they have at least one record in the activity table, but only consider them one time, basically based on a DISTINCT activity record between the  start date and end date.

Thanks in advanced!!

ALTER PROC [dbo].[demoIncome]
@username varchar(15),
@dStart datetime = 'mm/dd/yyyy',
@dEnd datetime ='mm/dd/yyyy'



AS


SELECT CASE
		   WHEN income = 0 THEN
			   'a. No Income'
		   WHEN income > 0 AND income <= 200 THEN
			   'b. 0 to 200'
		   WHEN income > 200 AND income <= 500 THEN
			   'c. 201 to 500'
		   WHEN income > 500 AND income <= 1000 THEN
			   'd. 501 to 1000'
		   WHEN income > 1000 AND income <= 1500 THEN
			   'e. 1001 to 1500'
		   WHEN income > 1500 AND income <= 2000 THEN
			   'f. 1501 to 2000'
		   ELSE
			   'g. Greater than 2000'
	   END AS category
	 , count(*) AS [count]
FROM
	(SELECT cast(income AS NUMERIC) AS income
	 FROM
		 client
	 WHERE
		 isnumeric(income) = 1) a
GROUP BY
	CASE
		WHEN income = 0 THEN
			'a. No Income'
		WHEN income > 0 AND income <= 200 THEN
			'b. 0 to 200'
		WHEN income > 200 AND income <= 500 THEN
			'c. 201 to 500'
		WHEN income > 500 AND income <= 1000 THEN
			'd. 501 to 1000'
		WHEN income > 1000 AND income <= 1500 THEN
			'e. 1001 to 1500'
		WHEN income > 1500 AND income <= 2000 THEN
			'f. 1501 to 2000'
		ELSE
			'g. Greater than 2000'
	END
ORDER by category  

Open in new window

Comment
Watch Question
Project Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE