Results 1 to 5 of 5

Thread: Using parent row in subquery where clause

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Using parent row in subquery where clause

    Hopefully you can see from the code what I want to achieve but am currently unsuccesful.

    I want to join on to each booking the latest record from from the audit table for that booking, however because the audit table is so big I would like to limit the nested select to only search for the records corresponding to the booking on each join iteration.

    Currently though the statement does not recognise b as a table alias.
    Code:
    SELECT * FROM
    Bookings as b
    INNER JOIN 		
    		(SELECT MAX(record_id) as LatestAuditID, bookingnumber 
    		FROM audit a 
    		WHERE a.bookingnumber=b.booking_number 
    		GROUP BY bookingnumber
    		) AS aud 
    		ON aud.bookingnumber = b.booking_number

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Using parent row in subquery where clause

    I'm not 100% sue I've understood but I think you're after something like this:-
    Code:
    Select B.Booking_Number, Max(A.RecordID)
    From Bookings B
    Inner Join Audit A
       on A.BookingNumber=B.Booking_Number 
    Group By B.Booking_Number
    No sub query required.

    You can select additional fields from the Bookings table as long as you include them in the group by. You've already grouped by Booking Number (which I assume is the unique identifier for a booking) so grouping by extra fields on the booking table will not affect the way teh rows collapse. The same is not true if you want extra fields form the Audit table.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Using parent row in subquery where clause

    This will join Bookings with the latest audit:
    Code:
    SELECT b.*, aud.*
    FROM Bookings as b
    	INNER JOIN (
    		SELECT au.*
    		FROM audit AS au
    			INNER JOIN (
    				SELECT MAX(record_id) as LatestAuditID, bookingnumber
    				FROM audit a
    				GROUP BY bookingnumber
    			) AS l ON l.LatestAuditID = au.record_id
    	) AS aud ON aud.bookingnumber = b.booking_number

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Using parent row in subquery where clause

    Another stab.
    Code:
    Select B.*
    FROM Bookings B
    INNER JOIN Audit A
       ON A.BookingNumber = B.Booking_Number 
    WHERE A.record_id IN 
    (SELECT TOP 1 C.record_id FROM Audit C WHERE C.BookingNumber = B.BookingNumber ORDER BY C.record_ID)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Using parent row in subquery where clause

    Quote Originally Posted by FishGuy View Post
    Hopefully you can see from the code what I want to achieve but am currently unsuccesful.

    I want to join on to each booking the latest record from from the audit table for that booking, however because the audit table is so big I would like to limit the nested select to only search for the records corresponding to the booking on each join iteration.

    Currently though the statement does not recognise b as a table alias.
    Code:
    SELECT * FROM
    Bookings as b
    INNER JOIN 		
    		(SELECT MAX(record_id) as LatestAuditID, bookingnumber 
    		FROM audit a 
    		WHERE a.bookingnumber=b.booking_number 
    		GROUP BY bookingnumber
    		) AS aud 
    		ON aud.bookingnumber = b.booking_number
    Since the audit table is big we have to avoid full table scans... what are the available indices on audit table and bookings table? How many rows are in audit table and in bookings table?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width