Hi
I have a table similar to the following:
Date ID Name Job Number JobType
12/12/2007 123456 Fred Smith 111111 Full Day
12/12/2007 654321 Bob Blue 222222 Half Day AM
12/12/2007 654321 Bob Blue 333333 Half Day PM
I need the following output:
Date ID Name Job Number JobType
12/12/2007 123456 Fred Smith 111111 Full Day
12/12/2007 654321 Bob Blue 222222 Half Day AM
12/12/2007 654321 Bob Blue 333333 Half Day PM
Now before you say the output is the same . It isn't! There are only 2 records in the output. The italic lines are one record, with a carriage return linefeed between each piece of data. So for job number the field is equal to 111111 + CHAR(10) + CHAR(13) + 222222
Could someone please point me in the right direction?
Cheers
You could to use SELECT FOR XML PAHT with empty tag:
Code Snippet
create table t2
(
Date datetime,
ID int,
Name varchar(20),
JobNumber varchar(20),
JobType varchar(20)
)
go
insert into t2 values('12/12/2007', 123456,'Fred Smith','111111','Full Day')
insert into t2 values('12/12/2007', 654321,'Bob Blue',' 222222','Half Day AM')
insert into t2 values('12/12/2007', 654321,'Bob Blue',' 333333','Half Day PM')
select
replace( (SELECT name + '##' FROM t2 as d where d.ID=m.ID FOR XML PATH('')), '##', char(10)+char(13) ) as CName
,ID from t2 m group by ID
|||Hi Kosinsky,
Your querry is not working in SQL200 is it for SQL 2005 or it will run properly in sql2000 also if not then wht will be the querry for sql2000,
I got the following error when i am trying to run your select querry in sql2000
Code Snippet
Server: Msg 170, Level 15, State 1, Line 1Line 1:
Incorrect syntax near 'XML'.
|||
My query use SELECT FOR XML PATH. Its SQL Server 2005 feature.
For SQL Server 2000 you could use FOR XML RAW and two additional replaces:
Code Snippet
select
replace
(
replace
(
replace( (SELECT name as t FROM t2 as d where d.ID=m.ID FOR XML RAW('t')), '"/><t t="', char(10)+char(13))
,'<t t="',''
)
,'"/>',''
)
,ID
from t2 m group by ID
|||Hi,Still its giving me the same error.?
Code Snippet
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'XML'.
|||
Sorry, but me solution doesn't work on SQL Server 2000. Because FOR XML is not valid in subselections
|||Thanks for the replies Konstantin Kosinsky, but I'm also running SQL Server 2000.
Does anyone have any other ideas on how to achieve this please?
Cheers
|||From what I can tell, all the easy solutions for this are in SQL2005. SQL2000 solutions are much messier. Try searching this forum for words like aggregate and concatenate. There are a few that might help, like this one:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=125302&SiteID=1
Note Umachandar's solution I think should work on 2000. MRys', while much neater, relies on having 2005.
Good luck.|||Cheers Cringing Dragon, great find. I used the post on the link you provided by Umachandar Jayachandran - MS.
The SQL of which is:
Code Snippet
select t3.id
, substring(
max(case t3.seq when 1 then ',' + t3.comment else '' end)
+ max(case t3.seq when 2 then ',' + t3.comment else '' end)
+ max(case t3.seq when 3 then ',' + t3.comment else '' end)
+ max(case t3.seq when 4 then ',' + t3.comment else '' end)
+ max(case t3.seq when 5 then ',' + t3.comment else '' end)
, 2, 8000) as comments
-- put as many MAX expressions as you expect items for each id
from (
select t1.id, t1.comment, count(*) as seq
from your_table as t1
join your_table as t2
on t2.id = t1.id and t2.comment <= t1.comment
group by t1.id, t1.comment
) as t3
group by t3.id;
No comments:
Post a Comment