Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help to built SQL

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 unique
insert 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 unique
insert 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 unique
insert 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 Sequence

Need 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.CoutDesc
from @tRouteD d
inner join @tCounter c
on 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]
Go to Top of Page

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.CoutDesc
from @tRouteD d
inner join @tCounter c
on 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 Bharu

why have - as prefix?
Go to Top of Page

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.CoutDesc
from @tRouteD d
inner join @tCounter c
on 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 Bharu

why have - as prefix?


can you modify as above & try?
Go to Top of Page

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?
Go to Top of Page

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>
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2010-01-25 : 13:44:43
tq mr visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-25 : 23:58:09
welcome
Go to Top of Page
   

- Advertisement -