Link to home
Start Free TrialLog in
Avatar of DaveThomasPilot
DaveThomasPilotFlag for United States of America

asked on

SQLite Query needed

I'm struggling with figuring out a query that I think should be simple.

I have two tables, call them "rosters"  and "lineups".  

Lineups looks like this:

name, foreign_id1, foreign_id2, foreign_id3 ... foreign_id6

Where foreign_id1, foreign_id2, foreign_id3 ... foreign_id6 are all unique records in "rosters".

Example lineup records:

User generated image
Example roster records:

User generated image
I want the query to return a record for each row in lineups and use the foreign_ids (field crn) to get an associated item (dog_name) from the rosters table.
SOLUTION
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of awking00
Your example doesn't seem to show any crns in the rosters table that match any foreignids in the lineups table. Perhaps you can post some actual sample data and your expected results.
Avatar of DaveThomasPilot

ASKER

Jmiller, this is very close to what I need.  It works perfectly for getting start, dog_2, dog_3 and anchor.  

But, some records in lineups will have NULLS for empty strings for bench_1 and bench_2.  When I changed your query to this:

Select l.team_name, a.dog_name as start, b.dog_name as dog_2,  c.dog_name as dog_3, d.dog_name as anchor, e.dog_name as bench_1, f.dog_name as bench_2
From lineups l
Inner Join club_rosters a on a.crn = l.start
inner join club_rosters b on b.crn = l.dog_2
inner join club_rosters c on c.crn = l.dog_3
inner join club_rosters d on d.crn = l.anchor
inner join club_rosters e on e.crn = l.bench_1
inner join club_rosters f on f.crn = l.bench_2

Open in new window


I only get the records where bench_1 and bench_2 actually exist in club_rosters.

I can control what goes into the bench_1 and bench_2 fields when there are only valid entries for start, dog_2, dog_3, and anchor.  Or, maybe I should just put a record in the club_rosters table that has a NULL crn?
My instincts tell me that it may not be necessary to have 6 joins, but without any data with which to taste it is difficult to verify. Is the data too sensitive to actually post? If so, could you provide some made up data and the expected results?
Here's an example set of records from table lineups:

User generated image
Here's a sample of result almost like what I need (the bench_1 and bench_2 fields have crn from lineups table instead of dog_name from club_rosters.  Adding inner joins for these last two fields eliminates records where those fields are null or empty  in lineups

User generated image
There was an example of club_rosters records in the original question.  The only thing relevant about that table is that a record will exist with a matching "crn" field, for every non-null (or empty) value of start, dog_2, dog_3, anchor, bench_1 and bench_2.  

start, dog_2, dog_3, and anchor will always be non-null and will consist of numeric data as char(6).  bench_1 and bench_2 are also char(6), but may be NULL or empty.
records-desired.png
>>There was an example of club_rosters records in the original question<<
Yes, but there were no crns that matched any of the start, dog_2, dog_3, anchor, bench_1, or bench_2 records in the lineups table. So where did names like Clyde, Dusty, Blade, Blackjack, etc. come from? I can only assume they were in the rosters table with crns matching one of the attributes in the lineups table. It is that matching data I would like to see.
I can only assume they were in the rosters table with crns matching one of the attributes in the lineups table. It is that matching data I would like to see.

Yes, that is correct.   I just showed a few records from the club_rosters table (there are thousands).  

All that's relevant is that there will be a record with a matching crn in the club_rosters table for each non-null, non-empty field of the line-ups.   If you need example data, just use the dog names from the desired results and crns from the lineups query.
Slight modification to jmiller's query (lefgt outer joins) gives what I need:

Select l.team_name, a.dog_name as start, b.dog_name as dog_2,  c.dog_name as dog_3, d.dog_name as anchor, e.dog_name, f.dog_name
From lineups l
left outer Join club_rosters a on a.crn = l.start
left outer join club_rosters b on b.crn = l.dog_2
left outer join club_rosters c on c.crn = l.dog_3
left outer join club_rosters d on d.crn = l.anchor
left outer join club_rosters e on e.crn = l.bench_1
left outer join club_rosters f on f.crn = l.bench_2

Open in new window


I'll award the points to jmiller unless awking00 wants to keep trying for a simpler query.  If awking00 comes up with one, I'll split the points 70%/30% since he seems to be actively working on it.

Thanks,

Dave Thomas
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial