| Author |
Topic |
|
oceanboy
Starting Member
44 Posts |
Posted - 2007-06-25 : 03:42:19
|
| Hello~Is it possible to match the value of columns in 2 tables and the column might have nulls. For exampleTable1 - this table is already in the databaseAttribute1 Attribute2 Attribute3 ... ... ...1 1 Null1 2 Null1 1 11 2 11 2 2Table2 - this table is created after a few processesAttribute1 Attribute2 Attribute3 ... ... ...1 1 Null1 2 Null1 1 11 2 11 2 2Now, I would like to do match these 2 tables. I have tried using join eg. select * from table1 a inner join table2 bon a.attribute1 = b.attribute1 and a.attribute2 = b.attribute2 and a.attribute3 = b.attribute3However, with this join, I only can get the information for 1,1,11,2,11,2,2 but not 1,1,null1,2,nullIs there other better way to do this?Thanks! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-25 : 03:44:19
|
[code]SELECT *FROM table1 a INNER JOIN table2 bON a.attribute1 = b.attribute1 AND a.attribute2 = b.attribute2 AND ( a.attribute3 = b.attribute3 OR (a.attribute3 IS NULL AND b.attribute3 IS NULL) )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-25 : 03:53:12
|
| select * from table1 a inner join table2 bon a.attribute1 = b.attribute1 and a.attribute2 = b.attribute2 and coalesce(a.attribute3,0) = coalesce(b.attribute3,0)--------------------------------------------------S.Ahamed |
 |
|
|
oceanboy
Starting Member
44 Posts |
Posted - 2007-06-25 : 04:03:46
|
| thanks khtan!whao~ if that's the case, my sql statement will go super long~ hahaha~ Perhaps my other statement was written in a long winded way.Can I ask you extra question if you don't mind, let say i have a table as belowattribute1, attribute2, attribute3, price, count1 1 1 12 51 1 1 13 41 1 2 10 31 1 2 8 31 1 2 11 2I want to pick up the record where the count is unique, with the attributes as group. Therefore:1,1,1,12,5 is the answer. How should I write it? My answer to this solution is very long, so I would like to hear/see someone else's advise. Thanks! |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-25 : 04:13:47
|
| How u r telling 1,1,1,12,5 is unique...explain it clearly--------------------------------------------------S.Ahamed |
 |
|
|
oceanboy
Starting Member
44 Posts |
Posted - 2007-06-25 : 04:33:56
|
| ah! my mistake, sorry. When I say unique, it means the price and the count.attribute1, attribute2, attribute3, price, count1,1,1,12,51,1,1,11,41,1,2,10,31,1,2,12,31,1,2,9,21,1,3,15,61,1,3,6,4as above, you have 2 price for 1,1,1. which is $12 and $11 and $12 has 5 count. Which is why it is unique. On the other hand, 1,1,2 has 2 prices with similar count, which is 3 for $10 and $12 respectively, hence not unique. And for 1,1,3, the record I would like to retrieve is the 1,1,3,15,6 |
 |
|
|
|
|
|