Solved

SQLite  Query needed

Posted on 2012-12-26
10
355 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
  • 5
  • 4
10 Comments
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 350 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
 
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 150 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now