• Self Joins (Access 2000 >)

    Author
    Topic
    #408479

    Hi all,

    Does anyone have some sample scenarios of when and where to use self joins in a query. I’m trying to explain the concept to someone and I’m blank with examples of when it is best to use it! I know about the example in the Northwind sample database in office. Please give me another example or two?!?
    Thanx

    Viewing 2 reply threads
    Author
    Replies
    • #862874

      Here is an example from the Sample Queries database: you have a table with milages and gallons of fuel used. (see screenshot). One way to retrieve the mileage from the previous record (useful for calculating miles/gallon) is to use a self-join:

      SELECT tblMileage.id, tblMileage.Date, tblMileage.Odometer, tblMileage_1.Odometer AS [Previous Odometer], tblMileage.Gallons
      FROM tblMileage LEFT JOIN tblMileage AS tblMileage_1 ON tblMileage.id-1 = tblMileage_1.id;

      (This query cannot be displayed in design view since it uses a non-standard join.)

    • #863803

      here is a nice real world example

      http://groups.google.com.au/groups?dq=&amp…s&start=100

      I changed the url and tested it and it worked for me but failing that the main parts are as follows.

      Problem
      I have a list of 50,000 records of which two records need
      to be removed. The olny thing I know about the two is that
      their sum = 1160.09. How can I get Access to evaluate the
      sum of any two of these records and determine which two
      sumed together provides this result

      A suggested Solution.

      I suggest you can create a new query, select SQL view and paste the
      following text:

      SELECT [MyTable].[RecordID] AS Record1, [MyTable_1].[RecordID] AS Record2,
      [MyTable].[NumberToSum] AS Value1, [MyTable_1].[NumberToSum] AS Value2,
      [MyTable].[NumberToSum]+[MyTable_1].[NumberToSum] AS SumOfValues
      FROM MyTable, MyTable AS MyTable_1
      WHERE ((([MyTable].[NumberToSum]+[MyTable_1].[NumberToSum])=1160.09));

      Then edit the text just to do the next changes:
      Change “MyTable” to the actual name of your list of records, (and also
      MyTable_1 to ActualName_1 )
      Change “RecordID” to the name of the field you used to identify your records
      (primary key)
      Change “NumberToSum” to the name of the field containing the number to sum

      I’m affraid because the size of your table, and probably the query is gonna
      perform quite slow. I think it would be better if you try it first by
      creating a little test table. Then you can decide if results satisfy your
      needs.

      The query will provide you a complete list of all pair of records that sum
      1160.09

    • #863804

      here is a nice real world example

      http://groups.google.com.au/groups?dq=&amp…s&start=100

      I changed the url and tested it and it worked for me but failing that the main parts are as follows.

      Problem
      I have a list of 50,000 records of which two records need
      to be removed. The olny thing I know about the two is that
      their sum = 1160.09. How can I get Access to evaluate the
      sum of any two of these records and determine which two
      sumed together provides this result

      A suggested Solution.

      I suggest you can create a new query, select SQL view and paste the
      following text:

      SELECT [MyTable].[RecordID] AS Record1, [MyTable_1].[RecordID] AS Record2,
      [MyTable].[NumberToSum] AS Value1, [MyTable_1].[NumberToSum] AS Value2,
      [MyTable].[NumberToSum]+[MyTable_1].[NumberToSum] AS SumOfValues
      FROM MyTable, MyTable AS MyTable_1
      WHERE ((([MyTable].[NumberToSum]+[MyTable_1].[NumberToSum])=1160.09));

      Then edit the text just to do the next changes:
      Change “MyTable” to the actual name of your list of records, (and also
      MyTable_1 to ActualName_1 )
      Change “RecordID” to the name of the field you used to identify your records
      (primary key)
      Change “NumberToSum” to the name of the field containing the number to sum

      I’m affraid because the size of your table, and probably the query is gonna
      perform quite slow. I think it would be better if you try it first by
      creating a little test table. Then you can decide if results satisfy your
      needs.

      The query will provide you a complete list of all pair of records that sum
      1160.09

    Viewing 2 reply threads
    Reply To: Self Joins (Access 2000 >)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: