| Author |
Topic |
|
Mir
Starting Member
19 Posts |
Posted - 2009-06-04 : 07:01:56
|
| Hi Guys,I have two table, ticket1 and ticket2 both have the same columns,how to combine this two tables? cAN SOMEONE HELP? I TRY TO USE UNION BUT FAILTHANKS..TICKET1BUS, KM1,KM2,DISTANCEWEX101 0 1 10WEX101 1 2 20WEX101 2 3 30WEX101 3 4 NULLWEX101 4 5 NULLWEX101 5 6 NULLWEX101 6 7 70WEX101 7 8 NULLWEX101 8 9 NULLWEX101 9 10 100TICKET1BUS, KM1,KM2,DISTANCEWEX101 0 1 NULLWEX101 1 2 NULLWEX101 2 3 NULLWEX101 3 4 401WEX101 4 5 501WEX101 5 6 601WEX101 6 7 NULLWEX101 7 8 801WEX101 8 9 901WEX101 9 10 NULL |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 07:25:20
|
What is the meaning of "combine this two tables"?What is your expected output for given sample data? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Mir
Starting Member
19 Posts |
Posted - 2009-06-04 : 07:55:49
|
quote: Originally posted by webfred What is the meaning of "combine this two tables"?What is your expected output for given sample data? No, you're never too old to Yak'n'Roll if you're too young to die.
Hi webfred,I want to combine both table mean that if distance from table ticket1 is null then it will be replace with the not null value from table ticket2. it is the same if the distance value from table ticket2 is null, then it will use the value from table ticket 1..thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 07:58:44
|
[code]select t1.BUS, t1.KM1, t1.KM2, distance = coalesce(t1.Distance, t2.Distance)from TICKET1 t1 full outer join TICKET2 t2 on t1.BUS = t2.BUSand t1.KM1 = t2.KM1and t1.KM2 = t2.KM2[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 08:09:34
|
[code]declare @Ticket1 table(BUS varchar(255),KM1 int,KM2 int,DISTANCE int)declare @Ticket2 table(BUS varchar(255),KM1 int,KM2 int,DISTANCE int)insert @Ticket1select 'WEX101', 0, 1, 10 union allselect 'WEX101', 1, 2, 20 union allselect 'WEX101', 2, 3, 30 union allselect 'WEX101', 3, 4, NULL union allselect 'WEX101', 4, 5, NULL union allselect 'WEX101', 5, 6, NULL union allselect 'WEX101', 6, 7, 70 union allselect 'WEX101', 7, 8, NULL union allselect 'WEX101', 8, 9, NULL union allselect 'WEX101', 9, 10, 100insert @Ticket2select 'WEX101', 0, 1, NULL union allselect 'WEX101', 1, 2, NULL union allselect 'WEX101', 2, 3, NULL union allselect 'WEX101', 3, 4, 401 union allselect 'WEX101', 4, 5, 501 union allselect 'WEX101', 5, 6, 601 union allselect 'WEX101', 6, 7, NULL union allselect 'WEX101', 7, 8, 801 union allselect 'WEX101', 8, 9, 901 union allselect 'WEX101', 9, 10, NULL--select * from @Ticket1--select * from @Ticket2selectt1.BUS,t1.KM1,t1.KM2,ISNULL(t1.DISTANCE,t2.DISTANCE) as DISTANCEfrom @Ticket1 t1join @Ticket2 t2 on t1.BUS = t2.BUS and t1.KM1 = t2.KM1 and t1.KM2 = t2.KM2[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 08:10:25
|
Aarrgh again - toooooooooo late... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 08:16:39
|
And I see KH.FULL OUTER JOIN is the correct way for this case GreetingsWebfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-04 : 08:30:40
|
| orselect bus,km1,km2,max(DISTANCE) as DISTANCE from(select bus,km1,km2,DISTANCE from @Ticket1union allselect bus,km1,km2,DISTANCE from @Ticket2) as tgroup by bus,km1,km2MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 08:49:57
|
There's only one way to rock (Sammy Hagar)but there are always many ways to query Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Mir
Starting Member
19 Posts |
Posted - 2009-06-07 : 07:22:57
|
quote: Originally posted by webfred There's only one way to rock (Sammy Hagar)but there are always many ways to query Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks webfred,What if the Ticket1 table have extra data like belowTICKET1BUS, KM1,KM2,DISTANCEWEX101 0 1 10WEX101 1 2 20WEX101 2 3 30WEX101 3 4 NULLWEX101 4 5 NULLWEX101 5 6 NULLWEX101 6 7 70WEX101 7 8 NULLWEX101 8 9 NULLWEX101 9 10 100WEX102 0 1 221WEX102 1 2 151WEX102 2 3 303hOW to make sure wex102 data together in the result even though there is no match in Ticket2 table for it? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-07 : 08:18:58
|
Yeah that's why I posted KH's FULL OUTER JOIN is the correct way.See here:declare @Ticket1 table(BUS varchar(255),KM1 int,KM2 int,DISTANCE int)declare @Ticket2 table(BUS varchar(255),KM1 int,KM2 int,DISTANCE int)insert @Ticket1select 'WEX101', 0, 1, 10 union allselect 'WEX101', 1, 2, 20 union allselect 'WEX101', 2, 3, 30 union allselect 'WEX101', 3, 4, NULL union allselect 'WEX101', 4, 5, NULL union allselect 'WEX101', 5, 6, NULL union allselect 'WEX101', 6, 7, 70 union allselect 'WEX101', 7, 8, NULL union allselect 'WEX101', 8, 9, NULL union allselect 'WEX101', 9, 10, 100 union allselect 'WEX102', 0, 1, 221 union allselect 'WEX102', 1, 2, 151 union allselect 'WEX102', 2, 3, 303insert @Ticket2select 'WEX101', 0, 1, NULL union allselect 'WEX101', 1, 2, NULL union allselect 'WEX101', 2, 3, NULL union allselect 'WEX101', 3, 4, 401 union allselect 'WEX101', 4, 5, 501 union allselect 'WEX101', 5, 6, 601 union allselect 'WEX101', 6, 7, NULL union allselect 'WEX101', 7, 8, 801 union allselect 'WEX101', 8, 9, 901 union allselect 'WEX101', 9, 10, NULL--select * from @Ticket1--select * from @Ticket2selectt1.BUS,t1.KM1,t1.KM2,ISNULL(t1.DISTANCE,t2.DISTANCE) as DISTANCEfrom @Ticket1 t1full outer join @Ticket2 t2 on t1.BUS = t2.BUS and t1.KM1 = t2.KM1 and t1.KM2 = t2.KM2 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-07 : 08:26:53
|
if you have records in TICKET1 not in TICKET2 or vice versa, then use FULL OUTER JOIN. I missed out the coalesce on the BUS & KM.SELECT BUS = coalesce(t1.BUS, t2.BUS), KM1 = coalesce(t1.KM1, t2.KM1), KM2 = coalesce(t1.KM2, t2.KM2), distance = coalesce(t1.Distance, t2.Distance)FROM TICKET1 t1 FULL OUTER JOIN TICKET2 t2 ON t1.BUS = t2.BUSAND t1.KM1 = t2.KM1AND t1.KM2 = t2.KM2 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-07 : 08:36:50
|
My thought was that there are always records in Ticket1 and only Distance can be NULL.But you are right KH.When using full outer join then it is the save way to look for NULL values on each side.GreetingsWebfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-07 : 08:51:24
|
If such cases does not exists for OP then a LEFT JOIN is sufficient.But from what OP describe here sounds like required a FULL OUTER JOIN to me quote: Originally posted by MirI want to combine both table mean that if distance from table ticket1 is null then it will be replace with the not null value from table ticket2. it is the same if the distance value from table ticket2 is null, then it will use the value from table ticket 1..thanks
Alternatively, can also use the solution posted by Madhivanan. Using UNION ALL to combine both table and then GROUP BY BUS, KM1, KM2. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|