473,520 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Sl1ver
196 New Member
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 1315
ck9663
2,878 Recognized Expert Specialist
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 New Member
i tried just normal joining aswell, but it does the same thing
Mar 31 '10 #3
ck9663
2,878 Recognized Expert Specialist
Have you tried INNER JOIN ?

Happy Coding!!!

~~ CK
Mar 31 '10 #4
Sl1ver
196 New Member
Tried all the joins its gives the same result
Apr 1 '10 #5
ck9663
2,878 Recognized Expert Specialist
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
4417
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 look at this query: ----------------------- SELECT * FROM (
9
3113
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 SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be...
4
2133
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 over the years. The idea is that from any widget in the database you can look forward and backward to see different versions of the widget through...
6
1886
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 look at the query below. There is a btree index on both m.account_id and a.account_id. Query (1) does not use the index on the messages table,...
6
11481
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 query in my VBA code, like this: If (result of the query > 0) then do something
6
11842
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. do { while(reader.read) { x += y;
4
1508
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 this example, I could just do left(namelist.names,2), but in more complex cases a value retrieved may have had a more complex logic behind it, e.g., if...
89
5950
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
2
2017
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- how can I update the profile variable before the listbox gets populated - ie assign the variable prior to the sql call (as the sql query should...
0
1233
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 slightly different result. My stored procedure accepts a start date and a Day of week, it then gets the date of the next date that falls on that day, it...
0
7326
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7238
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5769
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5159
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4809
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3298
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
531
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.