| Author |
Topic |
|
Karen426
Starting Member
3 Posts |
Posted - 2008-11-12 : 17:42:12
|
| Hi. I was hoping someone could help me. I have the following query where I am trying to get a count for each education level. It gives me all my results except where the field is Null or blank. In the Unknown column I should have a count of 174. I have listed my query and the results. I am sure I am just missing something but can't see it. Any help would be appreciated. Thanks.USE [Warehouse]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOBEGIN SET NOCOUNT ON;IF Exists(select name from [tempdb].[dbo].[Sysobjects] where name like '#Education%')begin drop table #Educationendcreate Table #Education( RowName varchar(50), OrderNumber int)Insert Into #Educationvalues ('Less than grade 9', 1)Insert Into #Educationvalues ('Grades 9 to 11, no diploma', 2)Insert Into #Educationvalues ('High school diploma', 4)Insert Into #Educationvalues ('GED', 3)Insert Into #Educationvalues ('Some college or university', 5)Insert Into #Educationvalues ('College or university degree', 6)Insert Into #Educationvalues ('Graduate Degree', 7)Insert Into #Educationvalues ('Do not wish to answer', 8)Insert Into #Educationvalues ('Unknown', 9)select OrderNumber, case when RowName = 'Do not wish to answer' then 'Refuse' when RowName is null then 'Unknown' else RowName end as [Education],(select count(*) from obj_person a where RowName = EducationalAttainmentTypeNameand d.startdate >= '10/01/2008'and d.startdate <= '10/31/2008') from #Education group by RowName, OrderNumberorder by OrderNumberDROP TABLE #EducationENDResults:OrderNumber Education Count1 Less than grade 9 252 Grades 9 to 11, no diploma 153 GED 84 High school diploma 565 Some college or university 1076 College or university degree 1967 Graduate Degree 08 Refuse 09 Unknown 0 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-11-12 : 18:25:16
|
| Change RowName = EducationalAttainmentTypeName toRowName = IsNull(EducationalAttainmentTypeName, 'Unknown')"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 23:19:34
|
quote: Originally posted by jhocutt Change RowName = EducationalAttainmentTypeName toRowName = IsNull(EducationalAttainmentTypeName, 'Unknown')"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
better to use coalesce instead of IsNull. see belowhttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-11-12 : 23:47:37
|
| Better is subjective, IsNull is faster and I know the limitations.In my mind faster is better"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 23:55:24
|
quote: Originally posted by jhocutt Better is subjective, IsNull is faster and I know the limitations.In my mind faster is better"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
even if it doesnt give you desired result always? then its upto u |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-13 : 01:47:25
|
quote: Originally posted by jhocutt Change RowName = EducationalAttainmentTypeName toRowName = IsNull(EducationalAttainmentTypeName, 'Unknown')"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
declare @EducationalAttainmentTypeName char(1)select IsNull(@EducationalAttainmentTypeName, 'Unknown')select Coalesce(@EducationalAttainmentTypeName, 'Unknown') MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-13 : 02:07:38
|
quote: Originally posted by madhivanan
quote: Originally posted by jhocutt Change RowName = EducationalAttainmentTypeName toRowName = IsNull(EducationalAttainmentTypeName, 'Unknown')"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
declare @EducationalAttainmentTypeName char(1)select IsNull(@EducationalAttainmentTypeName, 'Unknown')select Coalesce(@EducationalAttainmentTypeName, 'Unknown') MadhivananFailing to plan is Planning to fail
|
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-11-13 : 09:23:45
|
| :) Yes I understand that limitation.And I make sure that the value I am returning is the same length or shorter than the variable, or column I am using. Hmmm kinda like dont put 10 chars into a 5 char variabledeclare @a varchar(5)set @a = 'Hello World'select @a"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 01:12:01
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2008-11-14 : 05:16:36
|
I've used ISNULL for ages without problems...and illustrating your advice by forcing a 7 character word into a 1 character variable is just insane. Which ones of these inserts fails?? ->declare @EducationalAttainmentTypeName char(1)declare @table table ( EducationalAttainmentTypeName char(1))insert into @table select IsNull(@EducationalAttainmentTypeName, 'Unknown')insert into @table select Coalesce(@EducationalAttainmentTypeName, 'Unknown') select * from @table - Lumbago |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-14 : 05:25:20
|
Maybe a better example will convince someone?DECLARE @Table TABLE ( name VARCHAR(100) )DECLARE @name VARCHAR(3)INSERT @TableSELECT ISNULL(@name, 'Unknown') UNION ALLSELECT COALESCE(@name, 'Unknown') SELECT *FROM @Table E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2008-11-14 : 05:37:02
|
This is a far better example and illustrates the differences alot better. I'd still use ISNULL any day over COALESCE for these things but of course you need to have control over your datatypes/lengths.And madhi; I just reread my post and I realised I sort of called you insane. Clearly that was not my intention at all, I just didn't like your example. I hope you were not offended - Lumbago |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 07:32:10
|
<<And madhi; I just reread my post and I realised I sort of called you insane. Clearly that was not my intention at all, I just didn't like your example. I hope you were not offended >>No problem MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 08:01:23
|
<<I just didn't like your example.>>Consider other exampleYou have a table with column named status of char(1) datatypeWhat if you want to show 'Unknown' if status is NULL?COALESCE is best suitable MadhivananFailing to plan is Planning to fail |
 |
|
|
|