Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Column into rows depending on Date.


Convert Column into rows depending on Date.

Author
Message
abhas
abhas
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 662
Hi All,
Please suggest me an idea on the below scenario.

I am having table which contain studentid,studentname,startdate,enddate. Sample data is as below.
StudentID StudName StartDate EndDate
1 Mike 4/8/2013 6/16/2013
1 Mike 6/18/2013 8/26/2013
2 John 1/29/2014 4/8/2014
3 Andy 4/10/2014 6/18/2014

Now on depending StartDate and EndDate I want to convert data as below.i.e Monthwise.

StudentID StudName Month
1 Mike Apr
1 Mike May
1 Mike Jun
1 Mike Jul
1 Mike Aug
2 John Jan
2 John Feb
2 John Mar
2 John Apr
3 Andy Apr
3 Andy May
3 Andy Jun



Thanks
Abhas.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
You really need to post ddl and sample data. I posted it for you this time.

You can use a tally table here to fill in the "missing" months. You can read about tally tables here. http://www.sqlservercentral.com/articles/62867/

This produces the output you want.


set dateformat mdy

if OBJECT_ID('tempdb..#Student') is not null
drop table #Student

create table #Student
(
StudentID int,
StudentName char(4),
StartDate datetime,
EndDate datetime
)

insert #Student
select 1, 'Mike', '4/8/2013', '6/16/2013' union all
select 1, 'Mike', '6/18/2013', '8/26/2013' union all
select 2, 'John', '1/29/2014', '4/8/2014' union all
select 3, 'Andy', '4/10/2014', '6/18/2014'

select StudentID, StudentName, left(DATENAME(month, DATEADD(month, N - 1, 0)), 3) as [Month]
from #Student s
join Tally t on t.N >= month(StartDate) and t.N <= month(enddate)
group by StudentID, StudentName, left(DATENAME(month, DATEADD(month, N - 1, 0)), 3), DATEADD(month, N - 1, 0)
order by StudentID, DATEADD(month, N - 1, 0)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45002 Visits: 39880
micheltomes (6/8/2013)
I think my friend , gave you right script. So not need to give more details. Please check the code and use it. I think will some all problem.


Spam reported.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search