Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQLite  Query needed

Posted on 2012-12-26
10
Medium Priority
?
363 Views
Last Modified: 2012-12-26
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.
0
Comment
Question by:DaveThomasPilot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 1400 total points
ID: 38721476
Something like this should get you the records that you want. It will give you a record for each row in the lineup table and the name of each dog in the lineup from the roster table.


Select l.team_name, l.start, a.dog_name, l.dog_2, b.dog_name, l.dog_3, c.dog_name, l.anchor, d.dog_name
From lineups l
Inner Join roster a on a.crn = l.start
inner join roster b on b.crn = l.dog_2
inner join roster c on c.crn = l.dog_3
inner join roster d on d.crn = l.anchor
0
 
LVL 32

Expert Comment

by:awking00
ID: 38721487
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.
0
 

Author Comment

by:DaveThomasPilot
ID: 38721515
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:awking00
ID: 38721624
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?
0
 

Author Comment

by:DaveThomasPilot
ID: 38721661
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
0
 
LVL 32

Expert Comment

by:awking00
ID: 38721692
>>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.
0
 

Author Comment

by:DaveThomasPilot
ID: 38721811
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.
0
 

Author Comment

by:DaveThomasPilot
ID: 38721826
Here's the subset of records from club_rosters that are referenced from the lineups table.
0
 

Author Comment

by:DaveThomasPilot
ID: 38721960
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
0
 
LVL 32

Accepted Solution

by:
awking00 earned 600 total points
ID: 38722068
Don't know whether or not it's "simpler" but should perform better than 6 hash joins and 7 full table scans -
select l.team_name
,max(case when l.start = c.crn then c.dog_name end) start
,max(case when l.dog_2 = c.crn then c.dog_name end) dog_2
,max(case when l.dog_3 = c.crn then c.dog_name end) dog_3
,max(case when l.anchor = c.crn then c.dog_name end) anchor
,max(case when l.bench_1 = c.crn then c.dog_name end) bench_1
,max(case when l.bench_2 = c.crn then c.dog_name end) bench_2
from lineups l,
club_rosters c
where c.crn = l.start
   or c.crn = l.dog_2
   or c.crn = l.dog_3
   or c.crn = l.anchor
   or c.crn = l.bench_1
   or c.crn = l.bench_2
group by l.team_name;
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question