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

Looking for some help with JOIN logic

92
Good Day,

I have a number of tables that need to be joined to gether and up to now everything has been fine. I now need to exclude old files that are identified by a sequence number; newer records have a higher number.

The joins currently look like this:

Expand|Select|Wrap|Line Numbers
  1. FROM a
  2.     INNER JOIN b
  3.         ON(a.resource_id = b.resource_id)
  4.     INNER JOIN c
  5.         ON(a.resource_id = c.apar_id)
  6.     INNER JOIN  d
  7.         ON(a.client = d.client)
  8.     INNER JOIN e
  9.         ON( a.resource_id = e.resource_id
  10.     AND a.client = e.client )
  11.  
The sequence number is held in table b and I want to return only the record from table a that corresponds to the highest sequence number. As all records have a sequence number (starting at 1), I cannot work out how to exclude only those that have a superceeding record.

For Example:

With a.resource_id = 123456

Record 1 (To be excluded due to Record 2):
b.resource_id = 123456
b.sequence_no = 1

Record 2 (To be returned):
b.resource_id = 123456
b.sequence_no = 2

Whereas for records with no superceeding sequence_no:

With a.resource_id = 654321

Record 1 (To be returned):
b.resource_id = 654321
b.sequence_no = 1


I hope that makes sense.

All help is much appreciated.
Nov 14 '11 #1
1 1497
NDayave
92
Never mind, sorted it with help from here.

Final syntax was (Take note of the WHERE clause addition at the bottom):

Expand|Select|Wrap|Line Numbers
  1. FROM a 
  2.     INNER JOIN (b AS b1
  3.                 LEFT OUTER JOIN b AS b2
  4.                     ON(b1.resource_id = b2.resource_id
  5.                         AND b1.sequence_no < a2.sequence_no))
  6.         ON(a.resource_id = b.resource_id) 
  7.     INNER JOIN c 
  8.         ON(a.resource_id = c.apar_id) 
  9.     INNER JOIN  d 
  10.         ON(a.client = d.client) 
  11.     INNER JOIN e 
  12.         ON( a.resource_id = e.resource_id 
  13.     AND a.client = e.client )
  14.  
  15. WHERE b2.resource_id IS NULL
  16.  
Nov 14 '11 #2

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

Similar topics

2
by: Richard J Lacroix | last post by:
I have two tables that I am trying to use to create a report by using a join. When the query is executed, the SUM on the second table is 4 times the expected amount. As a stand alone query on the...
5
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
2
by: beena | last post by:
Apologize for posting this question.... Yes there were postings on update with join.... My question involves 4 table join... (hopefully qualifies as a new question) Need to convert the...
5
by: Sphenix | last post by:
------------------------ UPDATE A SET A.ID = '?' + A.ID FROM TABLEA A LEFT OUTER JOIN TABLEB B ON A.INDEX = B.INDEX WHERE B.DUP_ID IS NULL ------------------------ seems like update with...
6
by: Simon Harvey | last post by:
Hi everyone, We have a need to make a Windows Forms (2.0) client application that will be installed on our clients site. The data that the application uses needs to be centrally available to a...
40
by: rdemyan via AccessMonster.com | last post by:
I have two databases, db1 and db2, with the same table, TableA. I want to select the records from TableA in db1 that have a LAST_UPDATE SomeDate. Then I want to get the identical records in TableA...
4
by: news.onet.pl | last post by:
Hi!! Is it possible to delete (or update) with join ?? For example DELETE a FROM TableA a INNER JOIN TableB b ON b.id = a.bId
0
by: Unter | last post by:
Hi all My Treeview is working fine with the following layout 1 Parent 2 Parent 1 Child 2 Child 3 Parent
6
by: nodrogbrown | last post by:
hi i am using python on WinXP..i have a string 'folder ' that i want to join to a set of imagefile names to create complete qualified names so that i can create objects out of them ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.