473,425 Members | 1,797 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,425 software developers and data experts.

Sql query gets result 7 different when it should only have 2

Sl1ver
196 100+
this is my query
Expand|Select|Wrap|Line Numbers
  1. select distinct * from
  2. (select distinct city,itm__code,barcode1  from bx_itembarcodemaster
  3. where city = 'jhb'
  4. and (barcode1 is not null and bqty1 is not null)
  5. ) jhb
  6. left join
  7. (
  8. select distinct city,itm__code,barcode1 from bx_itembarcodemaster
  9. where city = 'cpt'
  10. and (barcode1 is not null and bqty1 is not null)
  11. ) cpt on cpt.itm__code = jhb.itm__code
  12. left join
  13. (
  14. select distinct city,itm__code,barcode1  from bx_itembarcodemaster
  15. where city = 'dbn'
  16. and (barcode1 is not null and bqty1 is not null)
  17. ) dbn on dbn.itm__code = jhb.itm__code
  18.  
and this is the result set

Expand|Select|Wrap|Line Numbers
  1. JHB    0-16040    0-16040        CPT    0-16040    0-16040        DBN    0-16040    0-16040
  2. JHB    0-16040    0-16040        CPT    0-16040    0-16040        DBN    0-16040    6002303714622
  3. JHB    0-16040    0-16040        CPT    0-16040    6002303714622    DBN    0-16040    0-16040
  4. JHB    0-16040    0-16040        CPT    0-16040    6002303714622    DBN    0-16040    6002303714622
  5. JHB    0-16040    6002303714622    CPT    0-16040    0-16040        DBN    0-16040    0-16040
  6. JHB    0-16040    6002303714622    CPT    0-16040    0-16040        DBN    0-16040    6002303714622
  7. JHB    0-16040    6002303714622    CPT    0-16040    6002303714622    DBN    0-16040    0-16040
  8. JHB    0-16040    6002303714622    CPT    0-16040    6002303714622    DBN    0-16040    6002303714622
  9.  
Its in multiples of 4 at first then 2 then 1 even thought there should technically only be 2 lines returned

WHat am i doing wrong?
Mar 30 '10 #1
5 1311
ck9663
2,878 Expert 2GB
Your LEFT JOIN returns all rows from your quest query regardless if there are matching rows on your right query.

Happy Coding!!!

~~ CK
Mar 31 '10 #2
Sl1ver
196 100+
i tried just normal joining aswell, but it does the same thing
Mar 31 '10 #3
ck9663
2,878 Expert 2GB
Have you tried INNER JOIN ?

Happy Coding!!!

~~ CK
Mar 31 '10 #4
Sl1ver
196 100+
Tried all the joins its gives the same result
Apr 1 '10 #5
ck9663
2,878 Expert 2GB
Will this work?

Expand|Select|Wrap|Line Numbers
  1.  
  2. select distinct city,itm__code,barcode1 
  3. from bx_itembarcodemaster
  4. where city in ('jhb','ctp','dbn') and
  5. (barcode1 is not null and bqty1 is not null)
  6.  
Happy Coding!!!

~~ CK
Apr 1 '10 #6

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

Similar topics

2
by: Agoston Bejo | last post by:
Version: Oracle 8.1.7. (I know it's prehistoric, but I am forced to work with it. Nevertheless, I'm curious if this is only a bug of this version or I'm wrong somewhere about SQL itself.) Take a...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
6
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
6
by: lakshmi | last post by:
Hi all I'm trying to traverse through the results from a query that returns more than 1 row. The data reader reads only the first row. The following code doesn't work. Let me know what's wrong....
4
by: Marc DVer | last post by:
As a simple example, say there is table 'namelist' with column 'names' as char(20). I would like to do something akin to: select namelist.names as mynames, left(mynames,2) as initials; In...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
2
by: DanWeaver | last post by:
I am assigning a profile variable based on the conents of a text box when a button is pressed- this is then used as a search term to populate a list box- all controls are in same ajax update panel-...
0
by: billchalmers | last post by:
I am using sql 2005 express and I have a strange proble, when I execute a stored procedure from the query window I get the results I expect, but when I execute it from an asp page then I get a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.