![]() |
| ||
| Problem using JOIN with six tables Hello all, I'm having some problems with joins in SQL. I hope someone here can help me out a little bit. I'm using MS SQL 2000 Server. I have the following tables & data: Table: tUsers What I want to achieve is the following (in one query):
So basically, the output I'm trying to get should look something like this: name Doc_A Doc_B Doc_C Doc_D I'm using the following query, but for some reason, it seems to be multiplying some values, so I'm not getting the right numbers: SELECT tContacts.name, COUNT(tDocumentsA.id) AS 'Doc_A', COUNT(tDocumentsB.id) AS 'Doc_B', COUNT(tDocumentsC.id) AS 'Doc_C', COUNT(tDocumentsD.id) AS 'Doc_D' The query above gives me the following (unwanted) result: name Doc_A Doc_B Doc_C Doc_D I'm not very used to working with Joins, so I haven't been able to figure out what's wrong with my query. I will greatly appreciate your help. Thank you. |
| ||
| Re: Problem using JOIN with six tables It's late and I'm a little blurry. But from what I can see in your query I think this: tDocumentsA.user_id = tUsers.contact_id should be: tDocumentsA.user_id = tUsers.id (the same for all document table joins) And all your JOINS should be INNER JOIN not LEFT OUTER JOIN. |
| ||
| Re: Problem using JOIN with six tables Thanx for your reply hollystyles, I've made a typo... the second column name on tables tDocumentsA ... tDocumentsD should read contact_id and not user_id, so tDocumentsA.user_id = tUsers.contact_id should be correct. Sorry about that. I've used INNER JOIN as you said, but now I'm only getting one row. This is my query: SELECT tContacts.name, COUNT(tDocumentsA.id) AS 'Doc_A', COUNT(tDocumentsB.id) AS 'Doc_B', COUNT(tDocumentsC.id) AS 'Doc_C', COUNT(tDocumentsD.id) AS 'Doc_D' And this is what I'm getting (only one contact): name Doc_A Doc_B Doc_C Doc_D When using LEFT OUTER JOIN I get the right people, but incorrect numbers (In the example above, using INNER JOIN, I'm also getting the incorrect numbers). contact5 should have (Doc_A ... Doc_D): 4, 1, 2, 2. Instead, I'm getting: 16, 16, 16, 16. It seems to be multiplying the column values (4 x 1 x 2 x 2 = 16)... :( |
| ||
| Re: Problem using JOIN with six tables Hi, Left outer join should be replaced by inner join, as already stated. Can you post the create- table statements? I have got the feeling that something could be wrong with the relationships of your tables. As for example, table tUser contains foreign key contact_id what comes from tContacts. If tUser.id is the primary key then one certain user can only have ONE contact. Maybe that s correct? If a user should have more then one contact, the tUser.id must be foreign key of tContacts. Furthermore, primary key of tContacts, obviously tContacts.id, should then be non-identifying foreign key of the tdocumentsA...D instead of user_id. As for your four document-tables, it would be a good idea to think of better normalization: What if you get further categories, say tdocumentsE ... ? So all documents should be stored in ONE table. This approach leads to simpler SQL statements, too. krs, tesu |
| ||
| Re: Problem using JOIN with six tables hi recursiveNugget, bad news! I have checked your join operations on those 6 tables, and the (wrong) results are: 1. contact.id id 2 will be counted 20 times id 5 will be counted 16 times id 8 will be counted 4 times 2. Indeed, you need LEFT OUTER JOIN. With inner join (which would be the appropriate one for such tasks, but...) you would lose all information about contact.id 2 and 8. 3. However, the result is wrong because you join four tables (tdocumentsA..D) independently together. The information you have spread over these 4 table MUST be arranged in ONE table only. Then applying of INNER JOIN on tusers INNER JOIN tContacts INNER JOIN tdocuments would work properly. Long story short, you need to redesign your tables. krs, tesu Btw, below is the result of the last join, where you can check out why inner join would produce poorer results (all rows containing (NULL) would then disappear) # DocA DocB DocC DocD |
| ||
| Re: Problem using JOIN with six tables Try this code select c.name, isNull(doc_A.Doc_A, 0) as Doc_A, isNull(doc_B.Doc_B, 0) as Doc_B, isNull(doc_C.Doc_C, 0) as Doc_C, isNull(doc_D.Doc_D, 0) as Doc_D |
| ||
| Re: Problem using JOIN with six tables @tesuji... the documents must be in separate tables since they will have different fields... the ones I've shown are just to illustrate the problem. Thank you for your help. :) @huangzhi... that's what I was looking for! It works! Thank you very much! :cool: |
| All times are GMT -4. The time now is 8:23 pm. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC