Author |
Topic |
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-08-28 : 00:26:22
|
select id,depart,arrive,code from tableAid depart arrive code total123 NULL NULL A 30123 UK AUS B 80123 AUS UK B 70select id,depart,arrive,journey from tableBid depart arrive journey123 UK AUS 1123 AUS UK 2how can i apply/replace codeA which has null depart and arrive column to be same as tableB journey1?expected result:id depart arrive code total123 UK AUS A 30123 UK AUS B 80123 AUS UK B 70 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-28 : 01:10:31
|
Whats the rule for getting values? why cant it be AUS UK instead of UK AUS? is it like next available value? if yes, is there a field to determine the order?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
jethrow
Starting Member
37 Posts |
Posted - 2013-08-28 : 01:24:41
|
Whereas I agree with visakh16 that we need more info, here is a surface level answer that satisfies the OP:SELECT id, IsNull(depart, (Select depart From tableB Where journey=1)) As depart, IsNull(arrive, (Select arrive From tableB Where journey=1)) As arrive, code, totalFROM tableA Microsoft SQL Server Noobie |
 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-08-28 : 02:06:03
|
those which is NULL only will grab those with journey = 1 |
 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-08-28 : 02:22:00
|
i can't use isNULL. the column which is blank suppose to be depart='' |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-28 : 02:40:16
|
[code]SELECT a.id, COALESCE(NULLIF(a.depart,''),b.depart) As depart, COALESCE(NULLIF(a.arrive,''),b.arrive) As arrive, a.code, a.totalFROM tableA aLEFT JOIN TableB bON b.id = a.idAND b.Journey=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2013-08-28 : 02:42:33
|
[code];WITH TableA AS( SELECT * FROM (VALUES(123,NULL,NULL,'A',30), (123,'UK','AUS','B',80), (123,'AUS','UK','B',70)) AS TableA(id, depart, arrive, code, total)), TableB AS( SELECT * FROM (VALUES(123,'UK','AUS',1), (123,'AUS','UK',2)) AS TableB(id, depart, arrive, journey))SELECT TableA.id, COALESCE(TableA.depart, TableB.depart), COALESCE(TableA.arrive, TableB.arrive), TableA.code, TableA.totalFROM TableA LEFT JOIN TableB ON TableA.id = TableB.idWHERE TableB.journey = 1[/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-28 : 02:57:34
|
quote: Originally posted by waterduck
;WITH TableA AS( SELECT * FROM (VALUES(123,NULL,NULL,'A',30), (123,'UK','AUS','B',80), (123,'AUS','UK','B',70)) AS TableA(id, depart, arrive, code, total)), TableB AS( SELECT * FROM (VALUES(123,'UK','AUS',1), (123,'AUS','UK',2)) AS TableB(id, depart, arrive, journey))SELECT TableA.id, COALESCE(TableA.depart, TableB.depart), COALESCE(TableA.arrive, TableB.arrive), TableA.code, TableA.totalFROM TableA LEFT JOIN TableB ON TableA.id = TableB.idWHERE TableB.journey = 1
The WHERE condition will cause LEFT JOIN to reduce to an INNER JOIN. So you might need to specify the condition in ON clauseseehttp://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2013-08-28 : 03:16:30
|
noted, and reading urs article |
 |
|
|
|
|