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 query

Author  Topic 

wkm1925
Posting Yak Master

207 Posts

Posted - 2010-05-06 : 12:54:29
I've as follow,
declare @t1 table
(idx int identity(1,1), tripid varchar(10), tickno varchar(20), scout varchar(10),
ecout varchar(10), departdate datetime);
insert into @t1 values('1090','1234','hpt','kntn','3/21/2010');
insert into @t1 values('1090','1235','ast','jb','3/21/2010');
insert into @t1 values('1090','1236','hpt','trg','3/21/2010');
insert into @t1 values('1090','1237','trg','ah','3/21/2010');

declare @t2 table
(idx int identity(1,1), tripid varchar(10), cout varchar(10),
ttime varchar(6), ndf char(1));
insert into @t2 values('1090','ast','103000','n');
insert into @t2 values('1090','hpt','143000','n');
insert into @t2 values('1090','kntn','173000','n');
insert into @t2 values('1090','jb','213000','n');
insert into @t2 values('1090','trg','003000','y');
insert into @t2 values('1090','ah','063000','y');


So far, my query as follow,
select t1.tripid,t1.tickno,
t1.scout,t2.ndf as 'sNDF',t1.ecout,t3.ndf as 'eNDF',
t1.departdate
from @t1 t1 inner join @t2 t2 on t1.tripid=t2.tripid and t1.scout=t2.cout
inner join @t2 t3 on t1.tripid=t3.tripid and t1.ecout=t3.cout


I'm looking for help, how to make it
1. sNDF='n' AND eNDF='n', then NDF='n'
2. sNDF='n' AND eNDF='y', then NDF='y'
3. sNDF='y' AND eNDF='y', then NDF='y'

So my final result as follow,

tripid | tickno | scout | sNDF | ecout | eNDF | departdate | NDF
-------------------------------------------------------------------------------
1090 1234 hpt n kntn n 2010-03-21 00:00:00.000 n
1090 1235 ast n jb n 2010-03-21 00:00:00.000 n
1090 1236 hpt n trg y 2010-03-21 00:00:00.000 y
1090 1237 trg y ah y 2010-03-21 00:00:00.000 y

Sachin.Nand

2937 Posts

Posted - 2010-05-06 : 13:06:15
You can use CASE statements.Something like this

Case when sNDF='n' AND eNDF='n' then 'n'
when sNDF='n' AND eNDF='y'then 'y'
and so on
End as NDF


PBUH
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-05-06 : 13:08:16
Try this:

select t1.tripid,t1.tickno,
t1.scout,t2.ndf as 'sNDF',t1.ecout,t3.ndf as 'eNDF',
t1.departdate, Case When t2.ndf ='n' and t3.ndf='n' then 'n'
When t2.ndf ='n' and t3.ndf='y' then 'y'
When t2.ndf ='y' and t3.ndf='y' then 'y'
End as NDF

from @t1 t1 inner join @t2 t2 on t1.tripid=t2.tripid and t1.scout=t2.cout
inner join @t2 t3 on t1.tripid=t3.tripid and t1.ecout=t3.cout

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2010-05-06 : 13:17:47
both answers were great.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-06 : 13:18:27
just use a case like

select t1.tripid,t1.tickno,
t1.scout,t2.ndf as 'sNDF',t1.ecout,t3.ndf as 'eNDF',
t1.departdate,
CASE WHEN t2.ndf='n' AND t3.ndf = 'n' THEN 'n'
WHEN t2.ndf='n' AND t3.ndf = 'y' THEN 'y'
WHEN t2.ndf='y' AND t3.ndf = 'y' THEN 'y'
END
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2010-05-06 : 13:23:39
tq mr visakh
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2010-05-06 : 14:02:32
Based on what I'm seeing, wouldn't this work as well??? It appears t3.ndf is the deciding factor in the case or am I over-simplifying things?

select t1.tripid,t1.tickno,
t1.scout,t2.ndf as 'sNDF',t1.ecout,t3.ndf as 'eNDF',
t1.departdate,
t3.ndf as NDF
---


Terry

-- A word to the wise ain't necessary - it's the stupid ones that need the advice. -- Bill Cosby
Go to Top of Page
   

- Advertisement -