473,422 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

I keep getting multiple results when there should only be one, combined result

I have a long sql query that, among other things, accesses multiple tables with an inner join and some left outer joins. I am expecting it to give a single set of results but when it finds more than one result from the left outer join, it duplicates the results. So, code is basically:
Expand|Select|Wrap|Line Numbers
  1. Select tenant.name, r1.roommate, r2.roommate, r3.roommate from Property
  2. Inner Join Tenant on tenant.property = property.property
  3. left outer join room r1 on r1.code = property.code
  4. left outer join room r2 on r2.code = property.code and
  5. r2.code <> r1.code
  6. left outer join room r3 on r3.code = property.code and
  7. r3.code <> r1.code and r3.code <> r2.code
What happens is if there are no rommates, everything works well but if there are any rommates, I get the right query results but it sends the data more than one time instead of just a single result with all the data populated (the results are sent to a work file to print). So, instead of sending something like name, rommate1, roommate2, roommate3 I get name, rommate1 and then name, roommate1, and then name, roommate2, etc so I get 3x results.

Thanks, Brad
Oct 7 '10 #1
4 1197
ck9663
2,878 Expert 2GB
It's because of duplicate records. I assume one if it is because a property could have many tenants, and one property have many rooms.

What output are you expecting?

Happy Coding!!!

~~ CK
Oct 7 '10 #2
NeoPa
32,556 Expert Mod 16PB
You have three tables for roommates. This is not a good plan. It's also the fundamental reason why you're multiplying your results.

I would look first at your table structure, and only after that review your SQL.
Oct 7 '10 #3
No, I have one table with tenants (unique as only one tenant come sup at a time) and on etable with all roommates. a teantn can have one or more roommates. That is why I queried the room table r1,r2,r3 multiple times to get multiple roommates. I expected to get something like Tenant: John Smith; Roommate: Jane Smith; Roommate2: Cindy Smith. I do get those results but it comes out 3x instead of just once.
Oct 7 '10 #4
NeoPa
32,556 Expert Mod 16PB
You're absolutely right Brad. I looked for that (I usually use the AS keyword in SQL but I do understand it's optional) but still didn't see it.

Actually, your SQL is a bit disordered. As you know SQL doesn't need to be formatted in any way to be recognised by the SQL engine itself, but that doesn't mean random spacing and case (SQL keywords are typically shown in upper-case) is not important. Reading through SQL in that state is unnecessarily complicated . Tidying it up, I notice there are no parentheses around any of your JOINs. This may not be the problem. I wouldn't expect it to be, but I'm not too sure as I don't know all the hierarchy rules and I never leave such things to chance (Even if I were right in my understanding, the chances are that the next one to come along would not know so would require the parentheses).

See if this makes any difference. I can't see why it wouldn't work, but then I couldn't see why yours didn't either :

Expand|Select|Wrap|Line Numbers
  1. SELECT tenant.name
  2.      , r1.roommate
  3.      , r2.roommate
  4.      , r3.roommate
  5. FROM   (((Property
  6.        INNER JOIN
  7.        Tenant
  8.   ON   Property.property = Tenant.property)
  9.        LEFT OUTER JOIN
  10.        room AS r1
  11.   ON   Property.code = r1.code)
  12.        LEFT OUTER JOIN
  13.        room AS r2
  14.   ON   Property.code = r2.code
  15.  AND   Property.code <> r1.code)
  16.        LEFT OUTER JOIN
  17.        room AS r3
  18.   ON   Property.code = r3.code
  19.  AND   Property.code NOT IN(r1.code, r2.code)
If it has the same problem, please post the data that you're using and the results received. Illustrative data is all that is required. The whole dataset is not necessary, but all records for each property that is included, are.
Oct 8 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ringo Langly | last post by:
Hi all, I need to put multiple results in one field, but not sure how. Here's some sample code: select a.name, a.accountnum, a.ordernum, (select itemid from items where items_ordernum =...
2
by: Patrick Olurotimi Ige | last post by:
When i run the code below with stored proc :- I get only the first table results :-"templates" even if i fill the dataset with another table for example category,pages etc.. Any ideas? ...
2
by: David Hearn | last post by:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Description: An unhandled exception occurred during the execution of the current...
8
by: lawrence k | last post by:
I've installed Apache 1.3.36 on my Redhat EL 3 machine. Now I'm trying to install PHP 5.1.4. I can not get the ./configure command to work. I keep getting this error: configure: error: Invalid...
4
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
1
by: Gabriel Genellina | last post by:
En Mon, 05 May 2008 00:31:45 -0300, Barclay, Ken <Ken.Barclay@schwab.comescribió: No entry in the error log, on the web server? (Apache?) Perhaps you're hitting some internal limit of the cgi...
8
Alireza355
by: Alireza355 | last post by:
I have two tables: table1: number explanation 10 something here 11 something here 12 something here 13 something...
2
by: Jacson | last post by:
Hello, I am trying to return multiple values as the CASE result embedded in a WHERE clause. This sample code, though it throws a syntax error, should give an idea what I am trying to accomplish....
7
by: seegoon | last post by:
Hi guys. I'm trying to write some php so that, depending on the page displayed, a certain <div> will load a certain class, giving it a certain background image. I have it down in theory, but...
3
by: rotaryfreak | last post by:
Hi, in my database table, i have inserted the following data after creating the appropriate tables: insert into Transaction values(0001, null, 'BransenEnt', 1, 'Mastercard', 99.99,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.