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 on SQL

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-14 : 20:45:07
I've as tables and rows as follow,

declare @t1 table
(rid varchar(20), cout varchar(10),seq tinyint);
insert into @t1 values('_r00000007','SG',3);
insert into @t1 values('_r00000007','KL',1);
insert into @t1 values('_r00000007','JB',2);
insert into @t1 values('_r00000008','SG',1);
insert into @t1 values('_r00000008','BI',2);
insert into @t1 values('_r00000008','KL',3);
insert into @t1 values('_r00000010','JB',1);
insert into @t1 values('_r00000010','KL',2);
/*@t1(rid,cout) is unique*/

declare @t2 table
(tickNo varchar(30),rid varchar(20), scout varchar(10), ecout varchar(10));
insert into @t2 values('SG1003000145','_r00000008','SG','KL');
insert into @t2 values('SG1003000146','_r00000008','SG','KL');
insert into @t2 values('JB1003000147','_r00000010','JB','KL');
/*@t2(tickNo) is unique*/

/*
@t2(rid) is a foreign key to relate to @t1
*/


How SQL look's like ,and the resultset as follow

tickNo | rid | scout | ecout | scoutSeq | ecoutSeq
---------------------------------------------------------------------
SG1003000145 _r00000008 SG KL 1 3
SG1003000146 _r00000008 SG KL 1 3
JB1003000147 _r00000008 JB KL 1 2


looking for help

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-14 : 23:44:53
[code]
select t2.tickNo, t2.rid, t2.scout, t2.ecout, t1s.seq as scoutSeq, t1e.seq as ecoutSeq
from @t2 t2
inner join @t1 t1s on t2.rid = t1s.rid
and t2.scout = t1s.cout
inner join @t1 t1e on t2.rid = t1e.rid
and t2.ecout = t1e.cout
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-20 : 22:16:32
tq sir
Go to Top of Page
   

- Advertisement -