| Author |
Topic |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2011-04-06 : 04:54:25
|
Hi,I have three tables AccountBase, Organisation and Clientsand I have AccountBaseID as a foreign key in Organisation and Clients tables..Now i want information from AccountBase table for matching records in both tables Organisation and ClientsHow can i do it with inner join? Is inner join with main tables will work?I have written below query..using union.. Any other way using join to achieve it.SELECT AB.Name, AB.AccountIDFROM [AccountBase] AS AB INNER JOIN tbl_Organisation O ON O.CRMAccountID = AB.AccountIDUNION ALL SELECT AB.Name, AB.AccountIDFROM [AccountBase] AS AB INNER JOIN tbl_ClientProjects CP ON CP.CRMAccountID = AB.AccountID |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-04-06 : 05:27:55
|
| Can you post some sample data ?PBUH |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2011-04-06 : 05:41:32
|
AccountBase tableAID Name1D A2E B3F C43 D85 F OrganisationOID OName AID1 test 1 1D2 test 2 85 Client ProjectCPID CName AID1 cp1 3F2 cp2 43 OUTPUT FOR MATCHED RECORDS IN LAST TWO TABLES..from AccountBase table.AID AName1D A85 B3F D43 F |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-06 : 06:08:57
|
| But your output is not showing any sense I think you are looking form thisTry this will Help you select * from #AccountBasewhere AId in (select * from (select Aid from #ClientProjectunion select Aid from #Organisation) as t) order by AIDRaghu' S |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2011-04-06 : 06:14:11
|
| Is there any other way using simply join instead of "in" for efficiency. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-04-06 : 06:17:18
|
| How are you getting 'B' for AName in o/p ?PBUH |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-06 : 06:31:42
|
| Ok Try thisselect A.* from #AccountBase A, #ClientProject B,#Organisation C where A.Aid=B.Aid or A.Aid=C.Aid group by A.Name,A.AIDRaghu' S |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-04-06 : 06:42:50
|
quote: Originally posted by raghuveer125 Ok Try thisselect A.* from #AccountBase A, #ClientProject B,#Organisation C where A.Aid=B.Aid or A.Aid=C.Aid group by A.Name,A.AIDRaghu' S
Basically I feel using a OR in a join is a bad idea performance wise because the optmiser is going to scanthe each table independently to get the matching rows and then combine them to the final result.PBUH |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-04-06 : 06:47:36
|
quote: Originally posted by keyursoni85 Is there any other way using simply join instead of "in" for efficiency.
Just modify raghuvirs first query to this to use a join instead of IN.select a.Name,a.AID from(select * from #Organisationunion allselect * from #ClientProject)tinner join #AccountBase a on a.AID=t.AID PBUH |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2011-04-06 : 07:22:51
|
| But above query will load all records from organisation and clientproject and then it will join with account base.. isn't it proper?Confused..I have currently used UNION QUERY as i posted above in first question. |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-06 : 07:33:54
|
| You are joining two time your tables with main table this is not good, performance wise.Because your table have too short records so you wont see any performance issue. When you deal with large records then you can see what is good for you.Great carry on which one you want....!Raghu' S |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-04-06 : 07:40:20
|
quote: Originally posted by keyursoni85 But above query will load all records from organisation and clientproject and then it will join with account base.. isn't it proper?Confused..I have currently used UNION QUERY as i posted above in first question.
Difference between your query is that you are doing a set of operation(in this case inner join on a table) and then doing another set of operation on another table(again inner join) and then combining them using a union.But with my query I am first combining the two tables and then joining them with the combined result.If I give you 2 numbers 1 and 2 which I want that you add and divide it by 3 how will you go about this?1) (1/3)+(2/3)---Method 12) (1+2)/3---Method 2Which method will you adapt Method 1 or Method 2 ? I will go for Method 2.Method 1 is what your query does while Method 2 is what my query does.PBUH |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2011-04-06 : 07:47:43
|
| Yes very true..thank you very much..sachin and raghu |
 |
|
|
|