Joining SQL tables on the column that has a value - sql

Let's say I have the following tables
Collection DirectorId StudioId
1 NULL 1
2 2 NULL
3 NULL 2
Director MovieId
1 1
2 4
Studio MovieId
1 1
2 3
2 4
1 2
The collection table will always have either director or studio id but never both.
How could I write a query to get all the movies in one a collection so that I end up with
CollectionID MovieID
1 1
1 2
2 4
3 3
3 4
I hope this makes sense

This should work (not sure about your column names, but you get the idea):
select c.CollectionID, isnull(d.MovieId, s.MovieId) MovieID
from Collection c
left join Director d on d.Director=c.DirectorId
left join Studio s on s.Studio=c.StudioId

Related

How to join three tables or more than three tables if many ID is pointing from one table to another table?

I am confused here about joining suppose I have three tables here Student table, Course table, Teacher table
'Student' table
Std_Id Std_Name Course_Id Teacher_Id
1 Amit 2 1
2 Yogesh 1 1
3 Pravin 3 2
4 Nilay 1 3
5 Abhijit 2 2
6 Vishal 2 4
7 Ramesh 3 3
8 Vijay 3 4
9 Ben 1 3
10 Vinod 2 4
'Course' table
Course_Id Course_Name Std_Id Teacher_Id
1 JAVA 1 3
2 C# 1 3
3 C++ 3 1
4 SAP 2 2
5 PYTHON 2 1
6 JAVASCRIPT 3 2
'Teacher' table
Teacher_Id Teacher_Name Std_Id Course_Id
1 Roy 1 1
2 John 2 1
3 Ben 1 3
4 Renu 2 3
5 Ramesh 1 3
As you can see I have three tables here so now I want to see the students who have courses and teachers so let`s look up here in the student table, we have Std_Id,Course_Id,Teacher_Id and in the Course table we have Course_Id,Std_Id,Teacher_Id and in the Teacher table we have Teacher_Id,Std_Id ,Course_Id so here I want to join three tables as I said I want to show students who have the courses and teachers then which column should be used here and here i am not giving any reference key so how to join three tables i am giving query here
select Std_Name,Course_Name,Teacher_Name
from Student
inner join Course
on ๐’๐ญ๐ฎ๐๐ž๐ง๐ญ.๐’๐ญ๐_๐ˆ๐=๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐
or ๐’๐ญ๐ฎ๐๐ž๐ง๐ญ.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐=๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐’๐ญ๐_๐ˆ๐
or ๐’๐ญ๐ฎ๐๐ž๐ง๐ญ.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐=๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐
or ๐’๐ญ๐ฎ๐๐ž๐ง๐ญ.๐’๐ญ๐_๐ˆ๐=๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐’๐ญ๐_๐ˆ๐
inner join Teacher
on ๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐=๐“๐ž๐š๐œ๐ก๐ž๐ซ.๐“๐ž๐š๐œ๐ก๐ž๐ซ_๐ˆ๐
or ๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐“๐ž๐š๐œ๐ก๐ž๐ซ_๐ˆ๐=๐“๐ž๐š๐œ๐ก๐ž๐ซ.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐
or ๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐“๐ž๐š๐œ๐ก๐ž๐ซ_๐ˆ๐=๐“๐ž๐š๐œ๐ก๐ž๐ซ.๐‚๐จ๐ฎ๐ซ๐ฌ๐ž_๐ˆ๐
or ๐‚๐จ๐ฎ๐ซ๐ฌ๐ž.๐“๐ž๐š๐œ๐ก๐ž๐ซ_๐ˆ๐=๐“๐ž๐š๐œ๐ก๐ž๐ซ.๐“๐ž๐š๐œ๐ก๐ž๐ซ_๐ˆ๐
so which common column should be used here to join three tables please let me know guys please give me answer.

Selecting rows and filler (null data)

I have a table that looks like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 5 7
1 B 1 3
1 B 3 2
1 B 6 1
I need to select all of that data, plus null data for the missing innings. It also need to stop at the max Inning for both teams (i.e. teamB's highest inning is 6, so I would collect 6 rows for both teamA and teamB yielding 12 total rows.)
For a visual, I need the output of the query to look like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 3 NULL
1 A 4 NULL
1 A 5 7
1 A 6 NULL
1 B 1 3
1 B 2 NULL
1 B 3 2
1 B 4 NULL
1 B 5 NULL
1 B 6 1
Is there anyway to do this with just a query? Modifying the original table to add the null values is not an option.
Self join to generate the permutations of reports and teams
Left self join to generate hits which might be nullable.
This is probably a lot more efficient if it's done outside of SQL
SELECT ins.ReportID, teams.TeamID, ins.inning, score.Runs
FROM games as ins
JOIN games AS teams
ON ins.ReportID = teams.ReportID
LEFT JOIN games AS score
ON ins.ReportID = score.ReportID
AND teams.TeamID = score.TeamID
AND ins.inning = score.inning
GROUP BY ins.ReportID, teams.TeamID, ins.inning;

Matching two variables to create a new ID

I'm trying to create an SQL statement to match either an id number or a postcode and then assign a new id number
What I want to end up with is โ€˜newidโ€™ that correctly recognizes that the first four records are the same person (even though the postcode for record 2 is different).
record id postcode newid
--------------------------
1 1 1 1
2 1 2 1
3 1 1 1
4 2 1 1
5 3 3 2
Any suggestions would be appreciated greatly.
Going based on your example:
SELECT RECORD,
(SELECT MIN (ID)
FROM users u2
WHERE users.id IN (u2.id, u2.postcode)
OR users.postcode in (u2.id, u2.postcode)
) AS newid
FROM users
This results with the following data:
RECORD NEWID
------------------
1 1
2 1
3 1
4 1
5 3
Here is the SQLFiddle

Selecting Data from Same Table that Doesn't Match

I have found several solutions for my type of problem, but I having trouble applying it in my situation.
Essentially I have a Vehicle Table:
License VIN Region
1 1 1
1 2 2
2 3 1
2 3 2
3 4 1
3 4 2
3 5 3
I want to take the license and vin from region 1 and see if the vin matches in all other regions based on the license. If it doesn't I want all the rows that don't match, but if it does match I don't want the row. So a complexity does come in when say I have 3 licenses and region 1 matches one row, but not the other, I want both the unmatched and region 1; however, when I have 3 licenses that all match I don't want any rows including region 1.
So my results in this case would be:
License VIN Region
1 1 1
1 2 2
3 4 1
3 5 3
I am using SQL Server 2005.
I think this is what you're looking for
SELECT DISTINCT a.*
FROM Vehicle AS a
INNER JOIN Vehicle AS b
ON a.License = b.License
WHERE a.VIN != b.VIN
AND a.Region != b.Region
AND (a.Region = 1 OR b.Region = 1)

Help with SQL and table optimization

I have two things to do: optimize table, write a query.
customer
id customer year_in_business is_discountable customers_friend_id
1 a 2 true 2
2 b 3 false 1
3 c 2 true 1
4 d 3 true null
Here customer_friend_id is pointing back to pk(id) of the same table.
product
id product
1 xxx
2 yyy
3 aaaa
customer_product
customer_id product_id
1 2
2 3
3 1
Questions
How to make these tables flexible or extensible in the future?
Get Name of each customer that has a friend and the name of that customerโ€™s friend.
Here is one suggestion: remove customer.customers_friend_id and create a new CustomerFriend table like so:
CustomerFriend
customerFriendID customerID friendID
1 1 2
2 2 1
3 3 1
This will allow customers with more than one friend, and avoids nulls for customers without friends.