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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Fill row into result set

Author  Topic 

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-18 : 05:26:42
When I execute my stored procedure the resultset is
Market-------Month--------Year------Value
1----------1-------------2004-------10
1----------2-------------2004-------10
1----------3-------------2004-------10
1----------5-------------2004-------10
1----------7-------------2004-------10
1----------1-------------2005-------10
1----------2-------------2005-------10
1----------3-------------2005-------10
1----------4-------------2005-------10

What I want do show
1----------1-------------2004-------10
1----------2-------------2004-------10
1----------3-------------2004-------10
1----------4-------------2004-------0
1----------5-------------2004-------10
1----------6-------------2004-------0
1----------7-------------2004-------10
1----------8-------------2004-------0
1----------9-------------2004-------0
1----------10-------------2004-------0
1----------11-------------2004-------0
1----------12-------------2004-------0
1----------1-------------2005-------10
1----------2-------------2005-------10
1----------3-------------2005-------10
1----------4-------------2005-------10
1----------5-------------2005-------0
1----------6-------------2005-------0
1----------7-------------2005-------0
1----------8-------------2005-------0
1----------9-------------2005-------0
1----------10-------------2005-------0
1----------11-------------2005-------0
1----------12-------------2005-------0

Means that if month is missing then i need to filled with value 0 but i should show month
Year is the Parameter of Stored procdure
When user enter Year, My result set return value from previous year
How 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 value
from myTable mt

--left join to derived table of months to include zeros
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 mm.month = datepart(month, myDateCol)

--Get results from previous year of @year parameter
where myDateCol >= convert(varchar, @year-1) + '-1-1'
and myDateCol < convert(varchar, @year) + '1-1'


Be One with the Optimizer
TG
Go to Top of Page

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-18 : 06:41:24
Its not working
Go to Top of Page

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-1
from (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) b
on a.month= b.[month]
Go to Top of Page

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 zeros
left join myTable mt
on datepart(month, mt.myDateCol) = mm.month


>>Its not working
If it's still not working, post your actual stamtement as well as any error messages.

Be One with the Optimizer
TG
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-18 : 07:57:24
Try this . . . . .
#temp is the result sets of your original query
create table #temp
(
market int default 1,
mth int,
yr int,
value int default 10
)

insert into #temp(mth, yr)
select 1, 2004 union all
select 2, 2004 union all
select 3, 2004 union all
select 5, 2004 union all
select 1, 2005 union all
select 2, 2005 union all
select 3, 2005 union all
select 4, 2005

--select * from #temp

declare @year int
select @year = 2005 -- user input
select 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 t
on y.mth = t.mth
and y.yr = t.yr
order by [Year], [Month]

drop table #temp


-----------------
'KH'

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-18 : 08:00:26
Also refer this to get more ideas
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro04/html/sp04d8.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-18 : 08:06:50
HERE iS THE STATEMENT

SELECT ISNULL(COUNT(DISTINCT dbo.tbl_Enrollment.fk_PersonID),0) AS Value
,mm.Month -- Month(tbl_Enrollment.EnrollmentDate)
,YEAR(tbl_Enrollment.EnrollmentDate) Year
FROM 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.month
where 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.
Go to Top of Page

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) Year
FROM (
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 mm
left join tbl_Enrollment e
on datepart(month, e.EnrollmentDate) = mm.month
where 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 Optimizer
TG
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-18 : 08:18:41
TG, so you really VNC back home !

-----------------
'KH'

Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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.
Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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), and
5% 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'

Go to Top of Page
   

- Advertisement -