Basically I need to take 3 columns from a table and combine them into rows of strings using a the value of the first column.
Starting basic output using:
select ItemID, Month, Year
from tableA
where ID = @id
Output:
ItemID Month Year
1. 4 1 2012
2. 4 2 2012
3. 4 3 2012
4. 4 6 2012
5. 4 8 2012
6. 12 1 2012
7. 12 2 2012
8. 12 4 2012
9. 12 5 2012
10. 12 6 2012
11. 12 7 2012
12. 53 8 2012
13. 53 9 2012
The output I am trying to get should look something similar to this:
1. 41201222012320126201282012
2. 12120122201242012520126201272012
3. 538201292012
Basically, combining all the ItemID's of the same number and adding the month and year to the end of that line until no more, then starting a new line when there is a new ItemID.
I have tried various temp table and pivot's but cannot manage to get this to output correctly. Any help would be appreciated.
Thanks
So, using this:
declare @table table (idmy varchar(8))
insert into @table
select cast(ItemID as varchar(2)) +
cast(Month as varchar(2)) +
cast(Year as varchar(4)) as idmy
from TableA
where ID = @id
select idmy from @table
I can get an output of:
idmy
1. 412012
2. 422012
3. 432012
4. 462012
5. 482012
6. 1212012
7. 1222012
8. 1242012
9. 1252012
10. 1262012
11. 1272012
12. 5382012
13. 5392012
Now to combine all like initial rows while doing above...