Author |
Topic |
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:37:09
|
Here is all my sql, why are my counter variables null?declare @startdate datetime , @enddate datetimeselect @startdate ='10/01/2011' , @enddate = '12/08/2011'declare @csqname varchar(20)declare @MondayCnt int=0,@TuesdayCnt int=0,@WednesdayCnt int=0,@ThursdayCnt int=0,@FridayCnt int=0,@SaturdayCnt int=0,@SundayCnt int=0select distinct(ccd.startdatetime),cqd.disposition,casewhen datepart(DW, ccd.startdatetime) = 1 then @SundayCnt +1when datepart(DW, ccd.startdatetime) = 2 then @Mondaycnt +1when datepart(DW, ccd.startdatetime) = 3 then @TuesdayCnt +1when datepart(DW, ccd.startdatetime) = 4 then @WednesdayCnt +1 when datepart(DW, ccd.startdatetime) = 5 then @ThursdayCnt +1when datepart(DW, ccd.startdatetime) = 6 then @FridayCnt +1 when datepart(DW, ccd.startdatetime) = 7 then @SaturdayCnt +1endfrom dbo.contactcalldetail ccd inner join contactqueuedetail cqd on ccd.sessionid = cqd.sessionid and ccd.sessionseqnum = cqd.sessionseqnum and cqd.profileid = ccd.profileid and cqd.nodeid = ccd.nodeid where DATEADD("HOUR", -5,ccd.startdatetime) >= @startdate and DATEADD("HOUR", -5,ccd.startdatetime) <= @enddate + '23:59:59.000'select @MondayCnt as Monday, @TuesdayCnt as Tuesday, @WednesdayCnt as Wednesday, @ThursdayCnt as Thursday, @FridayCnt as Friday, @SaturdayCnt as Saturday, @SundayCnt as SundaySee me athttp://www.local-developers.com |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-20 : 15:38:52
|
Varaibles are null until you assign them a value. Since you didn't assign them a value adding 1 to a null value is null. |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:40:29
|
ok so I've initialized them to zero and now I get zero, why don't my counters increment? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 15:43:47
|
quote: Originally posted by DeanT ok so I've initialized them to zero and now I get zero, why don't my counters increment?
because you're not assigning new values back. you've just included them in select but not assigned new values back to original variables------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:44:39
|
show me |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:46:59
|
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109309this is wrong |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:48:39
|
if your too lazy to post code then don't post at all |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 15:51:18
|
[code]declare @startdate datetime , @enddate datetimeselect @startdate ='10/01/2011' , @enddate = '12/08/2011'declare @csqname varchar(20)declare @MondayCnt int=0,@TuesdayCnt int=0,@WednesdayCnt int=0,@ThursdayCnt int=0,@FridayCnt int=0,@SaturdayCnt int=0,@SundayCnt int=0select @MondayCnt =sum(MondayCnt),@TuesdayCnt =sum(TuesdayCnt),@WednesdayCnt =sum(WednesdayCnt),@ThursdayCnt =sum(ThursdayCnt),@FridayCnt =sum(FridayCnt),@SaturdayCnt =sum(SaturdayCnt),@SundayCnt =sum(SundayCnt)from(select distinct(ccd.startdatetime),cqd.disposition,casewhen datepart(DW, ccd.startdatetime) = 1 then 1 end as SundayCnt,when datepart(DW, ccd.startdatetime) = 2 then 1 end as MondayCnt,when datepart(DW, ccd.startdatetime) = 3 then 1 end as TuesdayCnt,when datepart(DW, ccd.startdatetime) = 4 then 1 end as WednesdayCnt, when datepart(DW, ccd.startdatetime) = 5 then 1 end as ThursdayCnt,when datepart(DW, ccd.startdatetime) = 6 then 1 end as FridayCnt,when datepart(DW, ccd.startdatetime) = 7 then 1 end as SaturdayCntfrom dbo.contactcalldetail ccd inner join contactqueuedetail cqd on ccd.sessionid = cqd.sessionid and ccd.sessionseqnum = cqd.sessionseqnum and cqd.profileid = ccd.profileid and cqd.nodeid = ccd.nodeid where DATEADD("HOUR", -5,ccd.startdatetime) >= @startdate and DATEADD("HOUR", -5,ccd.startdatetime) <= @enddate + '23:59:59.000')tselect @MondayCnt as Monday, @TuesdayCnt as Tuesday, @WednesdayCnt as Wednesday, @ThursdayCnt as Thursday, @FridayCnt as Friday, @SaturdayCnt as Saturday, @SundayCnt as Sunday[/code]i didnt understand why you've select distinct and other columns inside. that didnt make sny sense to me------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Vassago
Starting Member
33 Posts |
Posted - 2012-03-20 : 15:51:53
|
Plus, we can't tell what is in your original tables. I'm guessing something in your join is also wrong if you are getting zeros. I used a generic calendar table on my system and it came up with values of 1 with each date listed, since you are doing a distinct on that. I don't think your joins are right. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 15:54:47
|
quote: Originally posted by DeanT if your too lazy to post code then don't post at all
you sound as if you're paying the people here for providing you the helpplease understand that people here are doing help voluntarily and have other main tasks also. Have the courtesy to appreciate the help got and also have attitude to apply yourselves based on guidelines given rather than waiting for spoonfed answers!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 15:55:49
|
quote: Originally posted by DeanT http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109309this is wrong
who toldsee how i'm doing iti'm assigning it back to original variable which is what you missed!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:56:26
|
then voluntarily do it right and stop being such an anuss |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 15:58:44
|
your answers are wrong sorry |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 16:00:20
|
i didnt understand why you've select distinct and other columns inside. that didnt make sny sense to mebecause smartass there are multiples startdatetime within the same day and I just want to count the day of weeks |
 |
|
Vassago
Starting Member
33 Posts |
Posted - 2012-03-20 : 16:00:42
|
DeanT is probably a student looking for help with school work or something to slide through school. I would hate to think that a professional would act in this manner. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 16:01:31
|
quote: Originally posted by DeanT your answers are wrong sorry
Nobody will be able to help you unless you give them full picture. By this attitude you're not helping anyone even yourself!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-20 : 16:02:13
|
quote: Originally posted by Vassago DeanT is probably a student looking for help with school work or something to slide through school. I would hate to think that a professional would act in this manner.
exactly ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
DeanT
Starting Member
13 Posts |
Posted - 2012-03-20 : 16:09:51
|
you act the way you want your the jerks |
 |
|
Vassago
Starting Member
33 Posts |
Posted - 2012-03-20 : 16:12:41
|
You're not setting your variables to the count you are trying to reach. You are also not doing anything to select a count in an aggregate function to set the variables to. Without these two steps, your variables will never stop being 0 because that is what you originally set them to without setting them again. Look up the set and count functions. That should give you a starting point.The only one being rude in this thread is you, as far as I'm concerned. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|