Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating and Counting Variable

Author  Topic 

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2011-03-30 : 15:25:09
I am trying to create a variable that I call subflag. It says where a subscriberid = memberid then assign a 1 or else assign a 0.

select ReportingMonth
Program
,SubscriberId
,MemberId
,case
when SubscriberId = MemberId then '1' else '0' end as Subflag

into temp

from mytable

where year(reportingmonth)= '2010'

Then I just want to sum the subflag field and count the memberid field by program:

select Program
,sum(convert(int,subflag)) as Subs
,count(memberid) as Members

from temp

where month(reportingmonth) = '12'

I get results, but they do not look correct to me. What am I doing wrong?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-03-30 : 15:37:35
forcing everything to text seems to stand out... and maybe a missing comma ??


select
ReportingMonth
,Program
,SubscriberId
,MemberId
,case when SubscriberId = MemberId then 1 else 0 end as Subflag
into temp
from mytable
where year(reportingmonth)= 2010




select
Program
,sum(convert(int,subflag)) as Subs
,count(memberid) as Members
from temp
where month(reportingmonth) = 12


Corey

I Has Returned!!
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2011-03-30 : 16:02:22
I dont intentinally force things to text.....both the subscriberid and memberid are intergers. Does this - ,case
when SubscriberId = MemberId then '1' else '0' end as Subflag - convert it to text??

quote:
Originally posted by jcb267

I am trying to create a variable that I call subflag. It says where a subscriberid = memberid then assign a 1 or else assign a 0.

select ReportingMonth
Program
,SubscriberId
,MemberId
,case
when SubscriberId = MemberId then '1' else '0' end as Subflag

into temp

from mytable

where year(reportingmonth)= '2010'

Then I just want to sum the subflag field and count the memberid field by program:

select Program
,sum(convert(int,subflag)) as Subs
,count(memberid) as Members

from temp

where month(reportingmonth) = '12'

I get results, but they do not look correct to me. What am I doing wrong?



Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-03-30 : 16:14:37

I was referring to the = '<int>' scenarios and the 'then' and 'else' values...

it doesn't convert your existing data... but [subflag] would be text
and month() & year() return an int, so no need to put the value in quotes...

on a different note... did the comma help?

Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -