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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 COUNT(CASE THEN (COUNT DISTINCT)) ? (SOLVED)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

apnw7931
Starting Member

9 Posts

Posted - 12/26/2010 :  18:10:50  Show Profile  Reply with Quote
Hi,

I have problem to code what i want to.

Please find attached my table and also the result i want to obtain.

I'm not capable of having the two last columns of my table Resultats (Compte Unique Jan, Compte Unique Fev).

I would like to count the number of difféerent (unique) account for January, the same for February.

Hère is what i already have :


Select
	Produit,
	Count(Compte),
	Count(CASE Date WHEN '01/01/2010' THEN 1 ELSE 0 END) AS "Compte Janv",
 	Count(CASE Date WHEN '01/02/2010' THEN 1 ELSE 0 END) AS "Compte Fév",
        ?? AS "Comptes Unique Jan",
        ?? AS "Comptes Unique Fév"

FROM
	ma_table

Where
	Date Between '01/01/2010' AND '01/02/2010'

GROUP BY
	Produit


I'll need something like :

CASE Date WHEN '01/01/2010' THEN (Count Distinct Compte)


Could you please help me ?




SOLUTION BROUGHT BY nigelrivett

Count(distinct CASE Date WHEN '01/01/2010' THEN Compte ELSE null END) AS [Comptes Unique Jan],
Count(distinct CASE Date WHEN '01/02/2010' THEN Compte ELSE null END) AS [Comptes Unique Fév]

Edited by - apnw7931 on 12/27/2010 03:52:35

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 12/26/2010 :  20:54:29  Show Profile  Reply with Quote

Count(distinct CASE Date WHEN '01/01/2010' THEN 1 ELSE 0 END) AS "Compte Janv",



KH
Time is always against us

Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 12/26/2010 :  22:12:33  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
sum(CASE Date WHEN '01/01/2010' THEN 1 ELSE 0 END) AS "Compte Janv",





==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 12/27/2010 :  00:06:04  Show Profile  Visit SwePeso's Homepage  Reply with Quote
I agree with Nigel that sum is a better choice, but you vcan do this with COUNT too.

count(CASE Date WHEN '01/01/2010' THEN 1 ELSE NULL END) AS [Compte Janv],





N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

apnw7931
Starting Member

9 Posts

Posted - 12/27/2010 :  03:10:44  Show Profile  Reply with Quote
Thank you all of you, but you misunderstood my problem.
I need the two last columns (Compte Unique Janv and Compte Unique Fév).
I need to count the unique accounts of each product:


Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 12/27/2010 :  03:40:16  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Maybe
Count(distinct CASE Date WHEN '01/01/2010' THEN Compte ELSE null END) AS [Comptes Unique Jan],


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

apnw7931
Starting Member

9 Posts

Posted - 12/27/2010 :  03:51:15  Show Profile  Reply with Quote
quote:
Originally posted by nigelrivett

Maybe
Count(distinct CASE Date WHEN '01/01/2010' THEN Compte ELSE null END) AS [Comptes Unique Jan]




Thanks a lot Niguel. That is exactly what i needed.
I wish you a nice day.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 12/27/2010 :  08:16:05  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by khtan


Count(distinct CASE Date WHEN '01/01/2010' THEN 1 ELSE 0 END) AS "Compte Janv",



KH
Time is always against us




Note that this will count everything. CASE WHEN is of no use

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 12/27/2010 :  10:11:02  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
>> Note that this will count everything. CASE WHEN is of no use
Try it and you will probably see what distinct does.

Note the op has tried it and it solves his problem.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 12/28/2010 :  02:51:12  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by nigelrivett

>> Note that this will count everything. CASE WHEN is of no use
Try it and you will probably see what distinct does.

Note the op has tried it and it solves his problem.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



FYI,


select Count(distinct CASE Date WHEN '01/01/2010' THEN 1 ELSE 0 END) "Compte Janv"
from
(
select GETDATE() as date
union all
select '01/01/2010' 
union all
select GETDATE()
) as t


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 12/28/2010 :  05:28:31  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Need to read the whole thread. That was discarded long ago.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
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.17 seconds. Powered By: Snitz Forums 2000