Generate all combinations that do not already exist between two tables and union them with the ones that already exist SQL - 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

Related

SQL many-to-one join using two foreign keys

I have two tables (Table A & Table B) that I have in a database (SpatiaLite database). I would like to join all the entries in Table A with Table B using two foreign keys (TableA.Location & TableB.LSD, TableA.LICENCE_NO & TableB.Licence_No); however, there will be multiple INCIDEN_NO entries in Table A that match up with the joined rows in Table B.
Since there will be many INCIDEN_NO entries associated with the Licence_No in Table B, I would like to evenly distribute the INCIDEN_NO entries among all the LIC_LI_NO entries in Table B that align with the foreign keys. The rows from Table A can be randomly assigned to each LIC_LI_NO in Table B randomly and in no particular order.
I cannot seem to find a SQL expression for this operation, which has really stumped me for weeks.
You could match the rows up randomly with something like this:
with B as (
select row_number() over () as rn, lic_li_no
from B
), A as (
select abs(random()) % cntb + 1 as randnum, a.*
from A cross apply (select count(*) as cntb from B) b
)
select *
from A inner join B on A.randnum = B.rn;
You could also generate the cross product and keep random rows. I tried this query out on SQLite but it didn't seem to work as I expected:
select * from A cross join B where abs(random()) % 20 = 1
That said, I don't understand the purpose behind all this and it's certainly not a common thing to do in a database.

Create a table from two tables in postgresql

I have two tables(let's say A and B) in my PostgreSQL database and I want to create a third table from the columns of A and B.
Here are the columns of A and B
A: B:
Load_ID T_ID
M_ID From
From To
To T_type
M_type T_length
T_type T_weight
T_length #Trucks
T_weight Price
#Trucks T_rating
Loading_day Loading_day
The third table(C) must have all the columns from both tables where From,To,T_type,T_length, T_weight, #Trucks and Loading_day matches each other. Also Loading day is a date column, I am not sure how to compare them.
I tried doing this(see pusedo-code):
select columns name from both tables
from A,B
where compare columns
Is there a better way of doing this ? like merge them on selected columns ?
You can do join and compare like below-
select A.*, B.* from tableA A inner join tableB B
on A.From=B.To and to=T_type and T_type =T_length
and others columns...
What you are looking for is Join (one of the flavors, depending on what you want). If you want to get a table C of only the entries in A and B where those columns match each other, you can do:
SELECT * FROM A a INNER JOIN B b ON a.From = b.from AND a.to = b.to AND ... ;

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

Retrieve a row from multiple tables on Oracle [one or more tables are empty]

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;

SQL Server Need Some Help Joins , please

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