| Author |
Topic |
|
mashfield
Starting Member
7 Posts |
Posted - 2005-09-20 : 07:59:12
|
| Hi All,I have a table with (lets say) 2 fields: ACTIONED_TO & COMMENTSThere will be multiple rows with the same ACTIONED_TO value some of the COMMENTS will be nulleg:ACTIONED_TO,COMMENTS"Matt","This is a comment""Dave","This is a comment too""Matt",what I'm looking for my query to return is:ACTIONED_TO, NO COMMENTS, COMMENTSMATT, 1,1Dave,1,0Do I need a CROSSTAB Query, How is it implemented?RegardsMatt A |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-09-20 : 08:31:36
|
| select actioned_to, sum(case (comments) when not null then 1 end), sum(case (comments) when null then 1 end) from yourtablegroup by actioned_towill be close........ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-20 : 09:56:12
|
quote: Originally posted by AndrewMurphy select actioned_to, sum(case (comments) when not null then 1 end), sum(case (comments) when null then 1 end) from yourtablegroup by actioned_towill be close........
Slight changeselect actioned_to, sum(case (comments) when not null then 1 else 0 end), sum(case (comments) when null then 1 else 0 end) from yourtableso that No counts have 0 instead of NullMadhivananFailing to plan is Planning to fail |
 |
|
|
mashfield
Starting Member
7 Posts |
Posted - 2005-09-20 : 10:33:14
|
| Nearly there folks, Howeverthe word NOT as in NOT NULL is not liked.PS Using VS2005 and building a web site of off SQL2000 |
 |
|
|
LarsG
Constraint Violating Yak Guru
284 Posts |
Posted - 2005-09-20 : 11:23:58
|
| [code]sum(case when comments is not null then 1 else 0 end), sum(case when comments is null then 1 else 0 end)[/code] |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-09-20 : 11:24:04
|
This should do it:select actioned_to, [NO COMMENTS] = sum(case when comments is null then 1 else 0 end), [COMMENTS] sum(case when comments is null then 0 else 1 end) from yourtablegroup by actioned_toorder by actioned_to CODO ERGO SUM |
 |
|
|
mashfield
Starting Member
7 Posts |
Posted - 2005-09-20 : 11:49:54
|
| Hi All,Tried WHEN NULL THEN 0 ELSE 1 END & WHEN NULL THEN 1 ELSE 0 ENDhowever, both columns return the same dataI am clear that the colums should be diffent.Ahhhhhhh |
 |
|
|
mashfield
Starting Member
7 Posts |
Posted - 2005-09-20 : 12:23:31
|
| ok, so i was using count instead of sum...now when I use sum 1 get 1 column with all the records and 1 column with 0SELECT rca_review_by, SUM(CASE (rca_root_cause) WHEN NULL THEN 1 ELSE 0 END) AS Expr1, SUM(CASE (rca_root_cause) WHEN NULL THEN 0 ELSE 1 END) AS Expr2 FROM dbo.BugTracking GROUP BY rca_review_by HAVING (NOT (rca_review_by IS NULL)) ORDER BY rca_review_byPlease help somebody!!! |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-09-20 : 19:25:30
|
I answered your question already. Just to make it clear, your are using the wrong form of CASE. Did you try what I posted? Why are the column names different in you latest post?quote: Originally posted by mashfield ok, so i was using count instead of sum...now when I use sum 1 get 1 column with all the records and 1 column with 0SELECT rca_review_by, SUM(CASE (rca_root_cause) WHEN NULL THEN 1 ELSE 0 END) AS Expr1, SUM(CASE (rca_root_cause) WHEN NULL THEN 0 ELSE 1 END) AS Expr2 FROM dbo.BugTracking GROUP BY rca_review_by HAVING (NOT (rca_review_by IS NULL)) ORDER BY rca_review_byPlease help somebody!!!
CODO ERGO SUM |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-20 : 20:20:59
|
>>(from MJV MVJ) you're are using the wrong form of CASE.Here is something that is definately NOT too obvious about nulls and Simple CASE compared with Searched CASE:select col ,case when col is null then 0 else 1 end as [SearchedCASE] ,case col when null then 0 else 1 end as [SimpleCASE]from ( select 1 col union all select null union all select 2 union all select null union all select 3 union all select null ) a Be One with the OptimizerTG |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-09-20 : 22:52:16
|
That's why I posted an answer with the other form of case.I almost always use this form of CASE statement:casewhen condition1 is truethen value1when condition2 is truethen value2else value3endI hardly ever use this form of CASE statement:case valuewhen value1then value2when value3then value4else value5endquote: Originally posted by TG..Here is something that is definately NOT too obvious about nulls and Simple CASE compared with Searched CASE...
CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-21 : 00:31:13
|
But I think both form will work correctly although first is more readable MadhivananFailing to plan is Planning to fail |
 |
|
|
mashfield
Starting Member
7 Posts |
Posted - 2005-09-21 : 04:31:14
|
| Hi People,I now have my Query Working! Many Thanks to all.Michael, The latest post from me was direcly from my script, using the real row names.All, I may be coming back for additional assistance as I'm new to SQL, so please bear with me.Thanks again |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-09-21 : 07:25:53
|
I don't think this form will work for this problem:case valuewhen value1then value2when value3then value4else value5endThe point is that NULL never equals NULL, so the following will alway evaluate to zero.case commentswhen NULLthen 1else 0endThis simple test will show you what I mean:select result = case null when null then 1 else 0 endresult ----------- 0(1 row(s) affected)quote: Originally posted by madhivanan But I think both form will work correctly although first is more readable MadhivananFailing to plan is Planning to fail
CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-21 : 07:43:15
|
| WellIt returns Null instead of 0Declare @t table(i int)insert into @t values(Null)insert into @t values(3)insert into @t values(456)Select case i when Null then 0 else i end from @tSelect case when i is Null then 0 else i end from @tMadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-21 : 08:09:49
|
quote: Originally posted by madhivanan WellIt returns Null instead of 0Declare @t table(i int)insert into @t values(Null)insert into @t values(3)insert into @t values(456)Select case i when Null then 0 else i end from @tSelect case when i is Null then 0 else i end from @tMadhivananFailing to plan is Planning to fail
If you are trying to just return a 0 or 1 based on null or not null then that form has a problem:select SimpleCASE ,count(SimpleCASE) [counts]from ( select col ,case when col is null then 0 else 1 end as [SearchedCASE] ,case col when null then 0 else 1 end as [SimpleCASE] from ( select 1 col union all select null union all select 2 union all select null union all select 3 union all select null ) a ) agroup by SimpleCASEselect SearchedCASE ,count(SearchedCASE) [counts]from ( select col ,case when col is null then 0 else 1 end as [SearchedCASE] ,case col when null then 0 else 1 end as [SimpleCASE] from ( select 1 col union all select null union all select 2 union all select null union all select 3 union all select null ) a ) agroup by SearchedCASE >>(from MVJ): That's why I posted an answer with the other form of case. I almost always use this form of CASE statement:Me too.Be One with the OptimizerTG |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-21 : 08:17:12
|
Well. I also prefer to use Case when col is null then .. method but didnt realise there is this much difference MadhivananFailing to plan is Planning to fail |
 |
|
|
|