June 28, 2010 at 7:54 am
Hi all,
I have to form a case statement in which I have write like this:
count( case when column1 is NULL then ( column2 ) else null end) AS 'countofCOL2'
But this statement is not working as my column1 contains a null value but still it is not counting column2...
sample code
,
create table #t
(
i int
)
insert into #t(i)
values (null)
SELECT CASE i WHEN NULL THEN 'Is Null' -- This will never be returned
WHEN 0 THEN 'Is Zero' -- This will be returned when i = 0
WHEN 1 THEN 'Is One' -- This will be returned when i = 1
END
FROM #t;
any help, where am I wrong in this code...'
Thanks
Thanks [/font]
June 28, 2010 at 8:04 am
Why do you need a case statement?
create table #t
(i int)
insert into #t(i) values (null)
insert into #t(i) values (1)
SELECT Count(*)
FROM #t
WHERE i IS NULL
As for the case statement you have, you're trying to do an equality check with null, that doesn't work, you need to check for nulls with IS NULL.
SELECT CASE WHEN i IS NULL THEN 'Is Null' -- This will never be returned
WHEN i = 0 THEN 'Is Zero' -- This will be returned when i = 0
WHEN i = 1 THEN 'Is One' -- This will be returned when i = 1
END
FROM #t;
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 28, 2010 at 8:04 am
create table #t
(
i int
)
insert into #t(i)
values (null)
SELECT CASE WHEN i IS NULL THEN 'Is Null' -- This will never be returned
WHEN i = 0 THEN 'Is Zero' -- This will be returned when i = 0
WHEN i = 1 THEN 'Is One' -- This will be returned when i = 1
END
FROM #t;
-- Gianluca Sartori
June 28, 2010 at 8:05 am
The simple CASE uses equivalence: CASE i WHEN NULL is the same as CASE WHEN i = NULL...
To check for NULLS you must use the searched CASE: CASE WHEN i IS NULL THEN...
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 28, 2010 at 8:05 am
Sorry for echoing, Gail...
-- Gianluca Sartori
June 28, 2010 at 8:06 am
[withdrawn, as too many answers already :-D]
June 28, 2010 at 8:08 am
Gianluca Sartori (6/28/2010)
Sorry for echoing, Gail...
Et tu, Gianluca.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 28, 2010 at 8:09 am
Thanks for quick response,
I need a case statement and need to count another column based in first column is null or not, I have many cases but this NULL case is not counting, I tried with IS NULL it is still not counting....
select
KEY
, count( case WHEN col1 IS NULL THEN col2 else null end) AS 'nullcnt'
, count( case when col1= 1 then col2 else null end) AS '1_cnt'
, count( case when col1= 2 then col2 else null end ) AS '2_cnt'
from table1
something like this , i want to achieve
Any help
Thanks [/font]
June 28, 2010 at 8:11 am
your case statement is not structured quite right. Try the below it should work fine.
SELECT
CASE
WHEN i is NULL THEN 'Is Null' -- This will never be returned
WHEN i=0 THEN 'Is Zero' -- This will be returned when i = 0
WHEN i=1 THEN 'Is One' -- This will be returned when i = 1
END
FROM #t;
wow we all responded at about the same time.
Dan
If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
June 28, 2010 at 8:20 am
thank you all it is counting fine,,
actually I was joining witha table for this col1 so it was excluding NULLS there...but now i am using coalese so it works...thanks again
Thanks [/font]
June 28, 2010 at 8:21 am
priya__ (6/28/2010)
Thanks for quick response,I need a case statement and need to count another column based in first column is null or not, I have many cases but this NULL case is not counting, I tried with IS NULL it is still not counting....
select
KEY
, count( case WHEN col1 IS NULL THEN col2 else null end) AS 'nullcnt'
, count( case when col1= 1 then col2 else null end) AS '1_cnt'
, count( case when col1= 2 then col2 else null end ) AS '2_cnt'
from table1
something like this , i want to achieve
Any help
Ooo!
The simplest way to get such counts - is using SUM!
select
KEY
, SUM (case WHEN col1 IS NULL THEN 1 ELSE 0 END) AS NullCNT
, SUM (case WHEN col1 = 1 THEN 1 ELSE 0 END) AS CNT_1
, SUM (case WHEN col1 = 2 THEN 1 ELSE 0 END) AS CNT_2
from table1
group by KEY
don't forget the GROUP BY!
June 28, 2010 at 8:21 am
I would do it this way:
select
KEY
, SUM( case WHEN col1 IS NULL THEN 1 else 0 end) AS 'nullcnt'
, SUM( case when col1= 1 then 1 else 0 end) AS '1_cnt'
, SUM( case when col1= 2 then 1 else 0 end ) AS '2_cnt'
from table1
-- Gianluca Sartori
June 28, 2010 at 8:22 am
Echoing again...
-- Gianluca Sartori
June 28, 2010 at 8:33 am
Or this way:
;WITH FastAggregate AS (
SELECT , col1, COUNT(*) AS RowCounts
FROM table1
GROUP BY , col1
)
SELECT ,
SUM(CASE WHEN col1 IS NULL THEN RowCounts END) AS 'nullcnt',
SUM(CASE WHEN col1 = 1 THEN RowCounts END) AS '1_cnt',
SUM(CASE WHEN col1 = 2 THEN RowCounts END) AS '2_cnt'
FROM FastAggregate
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 28, 2010 at 9:58 am
Thanks all for your reply,
I cannot use SUM as I am counting a Key column here and if I use sum it will sum up the columne instead...
I have to use count only...
Thanks [/font]
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply