473,386 Members | 1,819 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,386 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 1498
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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.