| Author |
Topic |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-23 : 11:26:29
|
My table and row as follow,declare @tCounter table(TrnxID [int] IDENTITY(1,1) NOT NULL,CoutCd varchar(10) not null,CoutDesc varchar(50) not null)--TrnxID is a primary key--CoutCd is a uniqueinsert into @tCounter(CoutCd,CoutDesc) values('HPT','Hentian Putra');insert into @tCounter(CoutCd,CoutDesc) values('KT','Kuala Terengganu');insert into @tCounter(CoutCd,CoutDesc) values('Kntn','Kuantan');insert into @tCounter(CoutCd,CoutDesc) values('JB','Johor Bharu');declare @tRouteH table(TrnxID [int] IDENTITY(1,1) NOT NULL,RouteCd varchar(10) not null,Mileage numeric(5,2) not null)--TrnxID is a primary key--RouteCd is a uniqueinsert into @tRouteH(RouteCd,Mileage) values('_R00000001',497.23);insert into @tRouteH(RouteCd,Mileage) values('_R00000002',250.45);declare @tRouteD table(TrnxID int identity(1,1) not null,RouteCd varchar(10) not null,CoutCd varchar(10) not null,Sequence tinyint not null)--TrnxID is a primary key--RouteCd is a Foreign Key refer to @tRouteH--CoutCd is a Foreign Key refer to @tCounter--RouteCd and CoutCd is a unique--CoutCd is a uniqueinsert into @tRouteD(RouteCd,CoutCd,Sequence) values('_R00000001','KT',1);insert into @tRouteD(RouteCd,CoutCd,Sequence) values('_R00000001','Kntn',2);insert into @tRouteD(RouteCd,CoutCd,Sequence) values('_R00000001','JB',3);insert into @tRouteD(RouteCd,CoutCd,Sequence) values('_R00000002','HPT',1);insert into @tRouteD(RouteCd,CoutCd,Sequence) values('_R00000002','JB',2);How to built SQL to display as follow,RouteCd | AllCoutCd | AllCoutDesc-------------------------------------------------------_R00000001 | KT - Kntn - JB | Kuala Terengganu - Kuantan - Johor Bharu_R00000002 | HPT - JB | Hentian Putra - Johor Bharu AllCoutCd is order by SequenceNeed help |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-24 : 09:32:35
|
| [code];with cte(RouteCd,CoutCd, CoutDesc) as(select d.RouteCd,d.CoutCd, c.CoutDescfrom @tRouteD dinner join @tCounter con c.CoutCd=d.CoutCd)select h.RouteCd,stuff((select ' - ' + CoutCd from cte where RouteCd=h.RouteCd for xml path('')),1,1,''),stuff((select ' - ' + CoutDesc from cte where RouteCd=h.RouteCd for xml path('')),1,1,'')from @tRouteH h[/code] |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-24 : 10:02:18
|
quote: Originally posted by visakh16
;with cte(RouteCd,CoutCd, CoutDesc) as(select d.RouteCd,d.CoutCd, c.CoutDescfrom @tRouteD dinner join @tCounter con c.CoutCd=d.CoutCd)select h.RouteCd,stuff((select ' - ' + CoutCd from cte where RouteCd=h.RouteCd for xml path('')),1,1,''),stuff((select ' - ' + CoutDesc from cte where RouteCd=h.RouteCd for xml path('')),1,1,'')from @tRouteH h
After run above query, my result as follow,RouteCd | AllCoutCd | AllCoutDesc-------------------------------------------------------------_R00000001 | - KT - Kntn - JB | - Kuala Terengganu - Kuantan - Johor Bharu_R00000002 | - HPT - JB |- Hentian Putra - Johor Bharuwhy have - as prefix? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-24 : 10:05:54
|
quote: Originally posted by Delinda
quote: Originally posted by visakh16
;with cte(RouteCd,CoutCd, CoutDesc) as(select d.RouteCd,d.CoutCd, c.CoutDescfrom @tRouteD dinner join @tCounter con c.CoutCd=d.CoutCd)select h.RouteCd,stuff((select ' - ' + CoutCd from cte where RouteCd=h.RouteCd for xml path('')),1,3,''),stuff((select ' - ' + CoutDesc from cte where RouteCd=h.RouteCd for xml path('')),1,3,'')from @tRouteH h
After run above query, my result as follow,_R00000001 - KT - Kntn - JB - Kuala Terengganu - Kuantan - Johor Bharu_R00000002 - HPT - JB - Hentian Putra - Johor Bharuwhy have - as prefix?
can you modify as above & try? |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-24 : 21:48:37
|
| yes. i got the result.where RouteCd=h.RouteCd for xml path('')),1,3,''), --- why have a 1,3? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 06:16:31
|
quote: Originally posted by Delinda yes. i got the result.where RouteCd=h.RouteCd for xml path('')),1,3,''), --- why have a 1,3?
because you need to delete first 3 chars to avoid the additional prefix <space>-<space> |
 |
|
|
wkm1925
Posting Yak Master
207 Posts |
Posted - 2010-01-25 : 13:44:43
|
| tq mr visakh |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 23:58:09
|
welcome |
 |
|
|
|
|
|