+ Reply to Thread
Results 1 to 7 of 7

Selecting pairs of records from database with SQL statements

  1. #1
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Selecting pairs of records from database with SQL statements

    Using Excel I can extract pairs of records from an excel range in order to perform some calculations as outlined in this thread:

    http://www.excelforum.com/excel-prog...ntire-row.html


    I have moved my data into an access database and I would now like to be able to do the same thing, only this time by extracting the data using an SQL SELECT statement ( or some other method).

    This is an example of my excel workbook.

    TEST COPY DT.xlsx


    Step by step:
    1. Match records on name

    example:
    Record 1 is 41668--AAA500---Adam J---78
    Record 58 is 41689--AAA425---Adam J---78

    2. Copy the Date, DT, Name, and Score from the first record side by side to the date, name, DT and score from the second record.

    Record 1 is 41668--AAA500---Adam J---78 Record 58 is 41689--AAA425---Adam J---80

    3. I will end up with a whole lot of matches

    as per the example workbook

    4. I then need to calculate difference between the scores and calculated averages & standard deviations but I will leave that aside for now.

    To begin with , any ideas on how I can extract the data from the database as per the sample?

    Any assistance appreciated.
    Thanks.

  2. #2
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Selecting pairs of records from database with SQL statements

    Can anyone suggest how I can select PAIRS of records?

    I presume it would be something like this?
    Please Login or Register  to view this content.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Selecting pairs of records from database with SQL statements

    Wouldn't it simply be?

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Selecting pairs of records from database with SQL statements

    If you're trying to replicate what you've done in the workbook, you're going about this the wrong way, what's the step that comes after?

  5. #5
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Selecting pairs of records from database with SQL statements

    Kyle,

    After trying 100 different ways that wouldn't work I went back to my first instinct of using a self join and managed to get it to work.

    Please Login or Register  to view this content.
    I can run the rest of what I need in Excel without too much trouble but if I can get a SQL fix that would be great.

    Basically my SELECT will give me a set of data for a specific b.DT (whatever DT I choose).
    My dataset will have many a.DT all with the same b.DT.
    For each a.DT I want to work out the average difference in scores (average of b.Sc-a.Sc for each a.DT)

    In my one record example:
    average b.AAA425 a.AAA500 = (80-78)/1 = 2

    The purpose is to attempt to predict what AAA500 test score a student will get based on their AAA425 test score. Ideally some sort of regression would be the optimal method but average across a sufficient dataset should achieve the same result.

    I am guessing my SQL statement might look something like:
    Please Login or Register  to view this content.
    Blue text = average of b.Sc-a.Sc
    Red Text is another column that is b.Sc-a.Sc

    Can I incorporate the red or blue text into my statement? If so HOW?


    EDIT:

    I am aware that if I can run the calculation of the average in my SQL statement then I only need to select AVG and order by a.DT. So the question is how to get the average for each a.DT in my dataset.
    Something like this maybe?
    Please Login or Register  to view this content.
    Last edited by anrichards22; 08-14-2015 at 09:22 AM.

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Selecting pairs of records from database with SQL statements

    Will each student only take each test once? Or could Adam J take mutltple AAA425 and AAA500s?

    Edit actually just reread the sample data, the same test can be taken multiple times. So How do you decide what needs to be averaged here:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-14-2015 at 10:21 AM.

  7. #7
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Selecting pairs of records from database with SQL statements

    Students can take each test multiple times. One of the questions that I need to answer is if a subsequent attempt improves a student's results (but that is for another time) so for now I want to average using all the data.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Selecting Pairs of Numbers
    By Jordans121 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-21-2011, 05:06 PM
  2. Formatting Output - Selecting Records
    By RobertWA in forum Excel General
    Replies: 0
    Last Post: 11-03-2010, 09:58 PM
  3. Selecting records associated with LARGEst values
    By BobDaBuilder in forum Excel General
    Replies: 2
    Last Post: 09-25-2007, 01:01 PM
  4. Selecting and deleting records
    By bbig80524 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2006, 02:09 PM
  5. Unique records of matching pairs
    By pmarques in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2005, 02:05 PM
  6. Unique records of matching pairs
    By pmarques in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-25-2005, 12:23 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1