March 6, 2014 at 9:10 am
Hi,
I have two columns which needs to repeat based on ID and number of distinct rows in that ID.
ID Date Created
1 1/1/2012 Sudheer
1 1/2/2013 Sudheer
1 3/3/2013 Sudheer
2 1/2/2014 Veera
2 2/5/2015 Veera
Results
ID Date Created Date Created Date Created
1 1/1/2012 Sudh 1/2/2013 Sudh 3/3/2013 Sudh
2 1/2/2014 Veera 2/5/2015 Veera
March 6, 2014 at 10:10 am
The best way to do this is what is known as a cross tab. These come in two varieties, static and dynamic. I will demonstrate the static version.
Notice how I posted ddl and sample data in a readily consumable format. This is something you should do in the future.
create table #Something
(
ID int,
MyDate date,
Created varchar(10)
);
insert #Something
select 1, '1/1/2012', 'Sudheer' union all
select 1, '1/2/2013', 'Sudheer' union all
select 1, '3/3/2013', 'Sudheer' union all
select 2, '1/2/2014', 'Veera' union all
select 2, '2/5/2015', 'Veera';
with MyCTE as
(
select *,
ROW_NUMBER() over (partition by ID order by ID, MyDate) as RowNum
from #Something
)
select ID,
max(case when RowNum = 1 then MyDate end) as MyDate1,
max(case when RowNum = 1 then Created end) as Created1,
max(case when RowNum = 2 then MyDate end) as MyDate2,
max(case when RowNum = 2 then Created end) as Created2,
max(case when RowNum = 3 then MyDate end) as MyDate3,
max(case when RowNum = 3 then Created end) as Created3
from MyCTE
group by ID;
drop table #Something;
You can read more about cross tabs by following the links in my signature.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 6, 2014 at 10:21 am
Thank you so much. I will keep in mind in future to present the data as you mentioned.
March 6, 2014 at 10:25 am
Glad that worked for you. 😀
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply