SQL Server Need Some Help Joins , please - sql

I have 3 tables A, B, C. There is a relationship between tables A and C while there is a relationship between tables B and C . There is no relationship between A and B.
What I would really like to do is get a list of all the records from B when there are records in C related to B given a value from A .
Please let me know, if this is not clear enough
Thanks

you can right query something like this...
SELECT B.* FROM B
INNER JOIN C ON C.aa = B.aa
INNER JOIN A ON A.bb = C.bb
WHERE A.cc = #yourvalue
#yourvalue is your value on which bases you need to select the value from B table. if you need match mutliple values from A then you need to change bit of query some thing like this...
WHERE A.cc IN (#val1,#val2,#val3....,#valNth)
In this query we have used INNER JOIN so it will gives only those records which are common on both the tables LIKE if you only join B with C then it will give the records which are common in B and C and then you join A with C then it will give those records which are common in A and C.
So suppose in B there is records something like 1,2,3 and in C there is 2,3,4,5 and in A there is 1,3,4,5
so the output of above query (without applying WHERE cause) is 1,3 only because this is common in all three table A,B,C.
you can got more information for joins in sqlserver by refering this links..
http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/
http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html
http://www.aspdotnet-suresh.com/2011/12/different-types-of-joins-in-sql-server.html

Simple math dictates if there is a relationship between A and C, and a relationship between B and C, there is, albeit by association, a relationship between A and B (through C).
Thus you will need to join all three together, going from A, through C, to B:
SELECT B.*
FROM A
JOIN C ON A.x = C.x
JOIN B ON B.y = C.y
WHERE A.z = #z

Related

How to join tables using CASE WHEN in postgresql?

I have a PostgreSQL query like below and I want to join a new table to that when rateid>100
CASE WHEN rateid<100 Then
Select * FROM rate as A, plan AS B, room AS C
WHERE A.id=B.rateid
AND B.roomid=C.id
ELSE
Select * FROM rate as A, plan AS B, room AS C, hotel AS D
WHERE A.id=B.rateid
AND B.roomid=C.id
AND C.hotelid=D.id
END
Can I know is there any way to join hotel table when rateid>100?
In your question you say "when rateid>100", I assume you mean "rateid>=100" because in the code you use "rateid<100"
I also changed the old-style to new-style joins.
The on-clause tells SQL what to join, so adding "rateid>=100" should solve the problem (When I understand your question correctly)
select
*
from
rate as A,
inner join plan as B on
A.id = B.rateid
inner join room as C on
B.roomid = C.id
and rateid >= 100

SQL - Searching a table using another table's column as criteria

I have table B with bcust(4-digit integer) and bdate(date) columns. I also have table C with ccust(4-digit integer) and cdate(date). I want to show the records from table c where cdate occurred after bdate.
I guess, maybe you're looking for this?
SELECT c.*
FROM c
INNER JOIN b
ON b.bcust = c.ccust
AND b.bdate < c.cdate;
I assumed, that the records are linked via the bcust and ccust columns.
Although you did not mention anything on how the records in both tables are related, I guess that records are related if bcust = ccust.
Then something like this should do what you want:
SELECT c.*
FROM tableC c
INNER JOIN tableB b ON c.ccust = b.bcust
WHERE c.cdate > b.bdate

Generate all combinations that do not already exist between two tables and union them with the ones that already exist SQL

I have 3 tables:
A
B
C
C is an association class between A and B. Meaning that there is a many to many relationship between A and B. C also has fields of its own that are not the primary keys of A/B.
I want to return all the fields in C for a given A.ID (PK). Now this part might return 0 to * results. But I always want to return the same number of results as there are records in B. That is I want to fill in the missing combinations between A.ID and B (that do not exist in C) with Null Values.
SAMPLE:
I am trying to do this within Access.
In case it helps, here is an image with the specific tables and their fields that I am trying to do this with.
Where A is ASCs, B is Flights, and C is FlightHistory.
You will need 2 queries, one that selects all IDs of B together with the desired AID, and one query that selects all these combinations, outer-joined to the existing combinations in C. This can be written in a single query (with a subquery) like this:
SELECT AB.AID, AB.BID, C.Desc
FROM (SELECT A.AID, B.BID FROM A, B WHERE (((A.AID)=1))) AB
LEFT JOIN C ON (AB.BID = C.BID) AND (AB.AID = C.AID);
You could union two result sets together, one which gets the primary records from C with another that gets the missing entries from B...
SELECT * FROM C WHERE C.AID = 1
UNION
SELECT 1 as AID, ID as BID, '' as Desc FROM B WHERE ID NOT IN (SELECT BID FROM C WHERE C.AID = 1)
ORDER BY BID;
http://sqlfiddle.com/#!9/02d110/11/0

pig script: join tables with null values

I'd like to join 2 tables, and I'm a bit lost with different kinds of joins
A(a_name:chararray, a_number:int)
a 1
b 2
c
d 3
e
B(b_id:int, b_name:chararray)
1 one
2 two
3 three
I know that I need to some sort of join, but with
AB = JOIN A by a_number, B by b_id;
FOREACH AB GENERATE
a_name,
b_name as a_number;
I get
a one
b two
d three
Instead of
a one
b two
c
d three
e
which I actually want.
How should I do this?
edit:
Ok, I tried left join but it doesn't keep the row order and instead returns
a one
b two
d three
c
e
Any workaround?
You are looking for a left JOIN.
This will keep all values on the left side of the relationship even if they don't appear in the right. Pig defaults to an inner JOIN, so it only keeps values that are in both sides.
This will now generate what you expect.
AB = JOIN A by a_number LEFT, B by b_id;
C = FOREACH AB GENERATE a_name, b_name AS a_number;
Also, you should be able to compact those two relations into:
AB = FOREACH (JOIN A by a_number LEFT, B by b_id)
GENERATE a_name, b_name AS a_number;
As far as I know there is no option in JOIN to perverse the order of the left relation. However, you can RANK A beforehand then ORDER on the number RANK creates after the JOIN.

Joining One table to many using Joins

In this question it's finally clicked how to write joins between multiple tables, where they link in a line e.g.
Table A - Table B - Table C
Where Table A references Table B, and Table B references Table C and so on.
What I still don't understand is how to reference the situation where Table A references Table B as above and also reference Table D.
In implicit Joins I can get the following to work, but want to move it to explicits...
SELECT a.name, b.office, c.firm, d.status
FROM job a, depts b, firms c, statuses d
WHERE a.office = b.ref
AND b.firm = c.ref
AND a.status = d.ref
Any tips?
SELECT
a.name,
b.office,
c.firm,
d.status
FROM
job a
JOIN depts b ON a.office = b.ref
JOIN firms c ON b.firm = c.ref
JOIN statuses d ON a.status = d.ref
That's as detailed as I could get on such an obscure question. You didn't describe what exactly does "link" mean in your case. So I don't know, maybe you need left join.