| Author |
Topic |
|
w1102157
Yak Posting Veteran
80 Posts |
Posted - 2012-01-19 : 09:37:37
|
| i have two tables temp 2 and BPthe temp 2 tables has 27356 rows , i want to pull one more colums from the dbo.bp table so i have still 27356 records , all the coulms from the temp 2 table and one extra colum from thebr table how ever when i run the query below it come back with 30582 records??? am i doing a wrong join? below is querySELECT dbo.Temp2.*, dbo.BP.KeyFROM dbo.BP RIGHT OUTER JOIN dbo.Temp2 ON dbo.BP.AKey = dbo.Temp2.AKey |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-01-19 : 09:43:51
|
It would be nice to have table structure, sample data and wanted result like this example: No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
w1102157
Yak Posting Veteran
80 Posts |
Posted - 2012-01-19 : 09:58:11
|
| it should be 1 to 1 join |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-01-19 : 10:08:51
|
select * from(SELECT dbo.Temp2.*, dbo.BP.KeyFROM dbo.BP RIGHT OUTER JOINdbo.Temp2 ON dbo.BP.AKey = dbo.Temp2.AKey)dtgroup by Akeyhaving count(*) > 1That will show you the duplicates... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
w1102157
Yak Posting Veteran
80 Posts |
Posted - 2012-01-19 : 10:33:41
|
| Thanks guys i will get back to you |
 |
|
|
w1102157
Yak Posting Veteran
80 Posts |
Posted - 2012-01-19 : 11:23:39
|
| ok i will ad more details on becuase i am now trying another way , there are three tables which areTable 1 : Temp TID am age date code Pid Table 2 :DHFid OrgDate pid SUM Hid Table 3:DHHidHcodeHdesc now I have 27000 records in temp table I want to get the Hcode and Hdesc information across so the final output should look like below Final Output: 27000 rowsTIDamagedatecodePidHcodeHdescHow can I link this acrossThank you guys |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2012-01-19 : 11:53:09
|
Hello w1102157 ,The question that is being asked is the relations between these three table and the joining values: Are they 1 to 1 or 1 to many?SELECT t1.TID, t1.am, t1.age, t1.date, t1.code, t1.Pid, t3.HidHcode, t3.HdescFROM table1 t1LEFT JOIN table2 t2 ON t2.pid = t1.pid --is this 1 to 1 or 1 to many with t1.pid?LEFT JOIN table3 t3 ON t3.hid = t2.hid -- is this a 1 to 1 or 1 to many with t2.hid It is likely that there are more than 1 value in one or more tables which is producing a result set larger than you expect.HTH. |
 |
|
|
w1102157
Yak Posting Veteran
80 Posts |
Posted - 2012-01-19 : 11:57:13
|
| 1 to 1 |
 |
|
|
|