SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Where clause within select statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

windtr
Starting Member

2 Posts

Posted - 04/24/2012 :  13:43:39  Show Profile  Reply with Quote
Hello, I want to add contitional logic into my select statement but not sure of the syntax and would appreciate any help.

The below code is what i am trying but obviously the syntax is wrong

select count(distinct email) where new='Y' as new,
count(distinct email),
(count(distinct email) where new='Y')/count(distinct email) as new_rate

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/24/2012 :  14:24:26  Show Profile  Reply with Quote
If you post the information on your tables with some sample input data and the output you expect to get, people on the forum would be in a better position to help. See here if you need some help posting: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


I am guessing you need something like this:
SELECT
	COUNT(DISTINCT email) AS TotalEmails,
	COUNT(DISTINCT CASE WHEN new = 'Y' THEN email END) AS NewEmails,
	1.0* COUNT(DISTINCT CASE WHEN new = 'Y' THEN email END)/COUNT(DISTINCT email)
FROM
	YourTable
Go to Top of Page

windtr
Starting Member

2 Posts

Posted - 04/24/2012 :  14:32:07  Show Profile  Reply with Quote
thank you so much! that is exactly what i needed.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000