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
Related
I need to join three tables to get all the info I need. Table a has 70 million rows, after joining a with b, I got 40 million data. But after I join table c, which has only 1.7 million rows, it becomes 300 million rows.
In table c, there are more than one same pt_id and fi_id, one pt_id can connect to many different fi_id, but one fi_id only connects to one same pt_id.
I'm wondering if there is any way to get rid of the duplicate rows, cause I join table c only to get the pt_id.
Thanks for any help!
select c.pt_id,b.fi_id,a.zq_id
from a
inner join (select zq_id, fi_id from b) b
on a.zq_id = b.zq_id
inner join (select fi_id,pt_id from c) c
on b.fi_id = c.fi_id
You can use GROUP BY
select c.pt_id,b.fi_id,a.zq_id
from a
inner join (select zq_id, fi_id from b) b
on a.zq_id = b.zq_id
inner join (select fi_id,pt_id from c) c
on b.fi_id = c.fi_id
group by c.pt_id,b.fi_id,a.zq_id
to remove all duplicate row as question below:
How do I (or can I) SELECT DISTINCT on multiple columns?
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
When having multiple tables with different columns, I would like to add all of them as one record. However, when one of them doesn't have records,
the other records retrieved from the other tables are not shown. How can I show the results of the remaining tables? For example, I have three
tables with one catalogue. Suppose that Table A doesn't have records and table B and C have. How can I show the results for these tables (Table B and C)? Even when Table A doesn't have records.
For example:
Table A
RECN
FNAME
TABLE B
RECN
DATE
TABLE C
RECN
ATTR1
Table CAT
RECN
LABEL
SELECT TA.*,TB.*,TC.*
FROM
(SELECT A.RECN, A.FNAME, CAT.LABEL
FROM A, CAT
WHERE A.RECN= CAT.RECN) TA,
(SELECT B.RECN, B.DATE, CAT.LABEL
FROM B, CAT
WHERE B.RECN=CAT.RECN) TB,
(SELECT C.RECN, C.ATTR1, CAT.LABEL
FROM C, CAT
WHERE C.RECN=CAT.RECN) TC
Now, I am obtaining an empty row, but I have to show the values of the tables which include values.
Thank you in advance for your help
Use LEFT JOIN. In addition, the subqueries -- while not a problem -- are not needed. In fact, your JOIN syntax is wrong. Here is what the query should look like:
SELECT A.RECN, A.FNAME, CAT.LABEL,
B.RECN, B.DATE, CAT.LABEL,
C.RECN, C.ATTR1, CAT.LABEL
FROM TA LEFT JOIN
TB
ON B.RECN = CAT.RECN LEFT JOIN
TC
C.RECN = CAT.RECN;
Please go thourgh Attached Image where i descirbed my scenario:
I want SQL Join query.
Have a look at something like
SELECT *
FROM Orders o
WHERE EXISTS (
SELECT 1
FROM OrderBooks ob INNER JOIN
Books b ON ob.BookID = b.BookID
WHERE o.OrderID = ob.OrderID
AND b.IsBook = #IsBook
)
The query will return all orders based on the given criteria.
So, what it does is, when #IsBook = 1 it will return all Orders where there exists 1 or more entries linked to this order that are Books. And if #IsBook = 0 it will return all Orders where there exists 1 or more entries linked to this order that are not Books.
Inner join is a method that is used to combine two or more tables together on base of common field from both tables. the both keys must be of same type and of length in regardless of name.
here is an example,
Table1
id Name Sex
1 Akash Male
2 Kedar Male
similarly another table
Table2
id Address Number
1 Nadipur 18281794
2 Pokhara 54689712
Now we can perform inner join operation using the following Sql statements
select A.id, A.Name, B.Address, B.Number from Table1 A
INNER JOIN Table2 B
ON A.id = B.id
Now the above query gives one to one relation details.
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.