| Author |
Topic |
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-18 : 05:26:42
|
| When I execute my stored procedure the resultset isMarket-------Month--------Year------Value1----------1-------------2004-------101----------2-------------2004-------101----------3-------------2004-------101----------5-------------2004-------101----------7-------------2004-------101----------1-------------2005-------101----------2-------------2005-------101----------3-------------2005-------101----------4-------------2005-------10What I want do show 1----------1-------------2004-------101----------2-------------2004-------101----------3-------------2004-------101----------4-------------2004-------01----------5-------------2004-------101----------6-------------2004-------01----------7-------------2004-------101----------8-------------2004-------01----------9-------------2004-------01----------10-------------2004-------01----------11-------------2004-------01----------12-------------2004-------01----------1-------------2005-------101----------2-------------2005-------101----------3-------------2005-------101----------4-------------2005-------101----------5-------------2005-------01----------6-------------2005-------01----------7-------------2005-------01----------8-------------2005-------01----------9-------------2005-------01----------10-------------2005-------01----------11-------------2005-------01----------12-------------2005-------0Means that if month is missing then i need to filled with value 0 but i should show month Year is the Parameter of Stored procdureWhen user enter Year, My result set return value from previous yearHow can i do this Help me |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-18 : 05:43:35
|
perhaps something like this will work. You will obviously need to change the query to "fit" your actual table.select mt.Market ,mm.Month ,mt.datepart(year, myDateCol) as year ,isNull(myValue, 0) as valuefrom myTable mt--left join to derived table of months to include zerosleft join (select 1 as month union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12) mm on mm.month = datepart(month, myDateCol)--Get results from previous year of @year parameterwhere myDateCol >= convert(varchar, @year-1) + '-1-1'and myDateCol < convert(varchar, @year) + '1-1' Be One with the OptimizerTG |
 |
|
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-18 : 06:41:24
|
| Its not working |
 |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2006-01-18 : 06:52:14
|
| Try this..select market, a.month, isnull(value ,'0' ) ,@year-1from (select 1 as month union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12) a left outer join(select * from [MyTable] where [year] = @year-1) bon a.month= b.[month] |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-18 : 07:25:09
|
Ah yes, I reversed the table joins:from (select 1 as month union ...--left join to derived table of months to include zerosleft join myTable mt on datepart(month, mt.myDateCol) = mm.month >>Its not workingIf it's still not working, post your actual stamtement as well as any error messages.Be One with the OptimizerTG |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-18 : 07:57:24
|
Try this . . . . .#temp is the result sets of your original querycreate table #temp( market int default 1, mth int, yr int, value int default 10)insert into #temp(mth, yr)select 1, 2004 union allselect 2, 2004 union allselect 3, 2004 union allselect 5, 2004 union allselect 1, 2005 union allselect 2, 2005 union allselect 3, 2005 union allselect 4, 2005--select * from #tempdeclare @year intselect @year = 2005 -- user inputselect isnull(t.market, 1) as Market, y.mth as [Month], y.yr as [Year], isnull(t.value, 0) as [Value]from( select mth, yr from ( select 1 as mth union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all select 11 union all select 12 ) as m cross join ( select (@year - 1) as yr union all select @year ) as y) as y left join #temp ton y.mth = t.mthand y.yr = t.yrorder by [Year], [Month]drop table #temp -----------------'KH' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-18 : 08:06:50
|
| HERE iS THE STATEMENTSELECT ISNULL(COUNT(DISTINCT dbo.tbl_Enrollment.fk_PersonID),0) AS Value,mm.Month -- Month(tbl_Enrollment.EnrollmentDate),YEAR(tbl_Enrollment.EnrollmentDate) YearFROM tbl_Enrollment LEFT JOIN (select 1 as month union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12) mm on datepart(month, tbl_Enrollment.EnrollmentDate) = mm.monthwhere Year(tbl_Enrollment.EnrollmentDate) >= convert(DateTime,convert(varchar(10), @year-1) + '-1-1')OR Year(tbl_Enrollment.EnrollmentDate) < convert(DateTime,convert(varchar(10), @year) + '-1-1')GROUP BY mm.Month,YEAR(tbl_Enrollment.EnrollmentDate)Its not showing any error but i also not showing needed result.Result is same wht a get previously. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-18 : 08:13:14
|
as I mentioned in my 2nd post, I reversed the tables. I'm not at a sql server so I can't test this but give it a try:SELECT ISNULL(COUNT(DISTINCT e.fk_PersonID),0) AS Value ,mm.Month -- Month(e.EnrollmentDate) ,YEAR(e.EnrollmentDate) YearFROM ( select 1 as month union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 ) as mmleft join tbl_Enrollment e on datepart(month, e.EnrollmentDate) = mm.monthwhere Year(e.EnrollmentDate) >= convert(DateTime,convert(varchar(10), @year-1) + '-1-1')and Year(e.EnrollmentDate) < convert(DateTime,convert(varchar(10), @year) + '-1-1')GROUP BY mm.Month ,YEAR(e.EnrollmentDate) Be One with the OptimizerTG |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-18 : 08:18:41
|
TG, so you really VNC back home ! -----------------'KH' |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-18 : 08:39:09
|
LOL - Nope, that was from notepad Be One with the OptimizerTG |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-01-18 : 09:21:15
|
| TG -- the cross join is the way to go, as khtan demonstrates. YOu need to cross join the list of all months with the list of all years that you wish to consider, and from there left outer join to the data. In addition, if there is more than 1 market to display, you will need to cross join a distinct list of those as well.See http://weblogs.sqlteam.com/jeffs/archive/2005/09/12/7755.aspx for more info on cross joins and how they help solve problems like this quite nicely. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-19 : 21:01:37
|
quote: Originally posted by jsmith8858 TG -- the cross join is the way to go, as khtan demonstrates. YOu need to cross join the list of all months with the list of all years that you wish to consider, and from there left outer join to the data. In addition, if there is more than 1 market to display, you will need to cross join a distinct list of those as well.See http://weblogs.sqlteam.com/jeffs/archive/2005/09/12/7755.aspx for more info on cross joins and how they help solve problems like this quite nicely.
Ok, no more notepad posts from vacation. I've obviously left all my sql talents (such as they are) back at work.Keep up the good work, sqlteam. I'll "see" ya in a couple weeks. Be One with the OptimizerTG |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-19 : 21:14:43
|
TG, Obviously there is more than one thing you did not pack with you for your trip.quote: 95% scored higher (more nerdy), and5% scored lower (less nerdy).What does this mean? Your nerdiness is:Definitely not nerdy, you are probably cool.
With a score like that, it is understandable -----------------'KH' |
 |
|
|
|