Avatar of DaveThomasPilot
DaveThomasPilot
Flag 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:

Example records from table lineups
Example roster records:

Example records from "rosters" table
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.
Databases

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
SOLUTION
Jerry Miller

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
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?
awking00

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
DaveThomasPilot

ASKER
Here's an example set of records from table lineups:

Records from lineups table
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

Records desired (almost)
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
awking00

>>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.
DaveThomasPilot

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DaveThomasPilot

ASKER
Here's the subset of records from club_rosters that are referenced from the lineups table.
DaveThomasPilot

ASKER
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
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.