473,405 Members | 2,154 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,405 software developers and data experts.

JOIN between 2 tables. No foreign key, just ranges

Hi,

I want to "join" 2 tables. In the second table, there is no foreign
key. The main table contains ranges (From... to...) that are used to
select rows in the second table.

1) SQL script for main table BHCODE
CREATE TABLE BHCODE
(CODE CHAR ( 3) NOT NULL WITH DEFAULT,
SEQ DEC ( 4) NOT NULL WITH DEFAULT,
FROMACCT DEC ( 4) NOT NULL WITH DEFAULT,
TOACCT DEC ( 4) NOT NULL WITH DEFAULT,
PRIMARY KEY (CODE,
SEQ))

2) SQL script for detail table BHMAIN
CREATE TABLE BHACCT
(ACCT DEC (4 ) NOT NULL WITH DEFAULT,
DESC CHAR (30 ) NOT NULL WITH DEFAULT,
PRIMARY KEY
(ACCT))

3) Here some data in table BHCODE
CODE SEQ FROMACCT TOACCT
A 1 1 2
A 2 3 4
A 3 6 7
B 1 10 11
B 2 12 13

4) Here some data in table BHACCT
ACCT DESC
1 Account 1
2 Account 2
3 Account 3
4 Account 4
5 Account 5
6 Account 6
7 Account 7
8 Account 8
9 Account 9
10 Account 10
11 Account 11
12 Account 12
13 Account 13

What I would like to do: I would like to retrieve rows from BHACCT
where CODE in BHCODE = given value.

IE: I would like to get the accounts 1 to 7 (excluding 5) from BHACCT
when column BHCODE.CODE = 'A'

I don't know how to build my SQL request to get this result. I'm not
even sure this can be done with SQL. FYI, there tables are already
existing and cannot be modified, thanks to our legacy systems...

Thanks for any help

Jack

Nov 17 '08 #1
3 4619
Ian
Jack wrote:
Hi,

I want to "join" 2 tables. In the second table, there is no foreign
key. The main table contains ranges (From... to...) that are used to
select rows in the second table.

1) SQL script for main table BHCODE
CREATE TABLE BHCODE
(CODE CHAR ( 3) NOT NULL WITH DEFAULT,
SEQ DEC ( 4) NOT NULL WITH DEFAULT,
FROMACCT DEC ( 4) NOT NULL WITH DEFAULT,
TOACCT DEC ( 4) NOT NULL WITH DEFAULT,
PRIMARY KEY (CODE,
SEQ))

2) SQL script for detail table BHMAIN
CREATE TABLE BHACCT
(ACCT DEC (4 ) NOT NULL WITH DEFAULT,
DESC CHAR (30 ) NOT NULL WITH DEFAULT,
PRIMARY KEY
(ACCT))

3) Here some data in table BHCODE
CODE SEQ FROMACCT TOACCT
A 1 1 2
A 2 3 4
A 3 6 7
B 1 10 11
B 2 12 13

4) Here some data in table BHACCT
ACCT DESC
1 Account 1
2 Account 2
3 Account 3
4 Account 4
5 Account 5
6 Account 6
7 Account 7
8 Account 8
9 Account 9
10 Account 10
11 Account 11
12 Account 12
13 Account 13

What I would like to do: I would like to retrieve rows from BHACCT
where CODE in BHCODE = given value.

IE: I would like to get the accounts 1 to 7 (excluding 5) from BHACCT
when column BHCODE.CODE = 'A'

Not really sure what you mean -- do you just want accounts that exist
in *either* the FROMACCT or TOACCT columns?

select a.acct, a.desc
from bhacct a, bhcode c
where c.fromacct = a.acct
and c.bhcode = 'A'
union all
select a.acct, a.desc
from bhacct a, bhcode c
where c.toacct = a.acct
and c.bhcode = 'A'
Or do fromacct / toacct specify a range of account numbers (i.e.
fromacct = start of range, toacct = end of range)?

select a.acct, a.desc
from bhacct a, bhcode c
where a.acct between c.fromacct and c.toacct
and c.code = 'A'

Nov 17 '08 #2
You can use new join syntax with BETWEEN predicate.

SELECT at.*
FROM bhcode cd
JOIN bhacct at
ON cd.code = 'A'
AND at.acct BETWEEN cd.fromacct AND cd.toacct
;
Nov 18 '08 #3
Thanks guys! It works fine!!!

On 18 nov, 02:39, Tonkuma <tonk...@fiberbit.netwrote:
You can use new join syntax with BETWEEN predicate.

SELECT at.*
* FROM bhcode cd
* JOIN bhacct at
* *ON *cd.code = 'A'
* *AND at.acct BETWEEN cd.fromacct AND cd.toacct
;
Nov 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rowan | last post by:
Hello, I am having a problem with a join. Either I am just not seeing the obvious, it isn't possible, or I need to use a different approach. I have an application with a vsflexgrid that needs...
4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
1
by: Justin | last post by:
Hello, I have a scenario where I have unique identifiers in about 25 tables, each table has varying fields - eg. Table #1 Table #2 Table #3 Table #4 --------- ...
2
by: gearond | last post by:
please CC me as I am on digest ------------------------------- I have three tables, simplified for, well, simplicity :-) CREATE TABLE Usrs ( usr_id serial primary NOT NULL, name text NOT...
0
by: Gary Townsend | last post by:
I am using Postgres 7.4.6 i have 3 tables i want to join 3 tables and return a list of route_id to which a specified user_id does NOT belong. Table "public.vts_users" Column | Type...
1
by: HGT | last post by:
Hello all, I am currently on a project which the source data come into the databases is always dirty (not surprisingly); however, due to the design of the database, it is very difficult to...
2
by: headache | last post by:
Greetings. I'm a first time poster, so feel free to clue me in to any protocol I may have inadvertently violated. I have an issue that has arisen where I need to join 3 tables where 2 of the 3...
1
by: teser3 | last post by:
I have 3 Access 2003 tables: TableMain MID PO TE INFO 1 2 3 ABC 2 2 1 EREER 3 1 3 OIUOI 4 3 3 PERE 5 2 2 DFE
1
by: ajcolburn | last post by:
Hi there, I'm writing a very simple shopping cart for a friend, but I'm confusing myself with the SQL required to return postage costs for each product in the cart, at checkout. It's probably...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.