Relating Multiple Tables to One Table [SQL - Oracle 12c] - sql

I'll try my best to pose this as a question, and not a "write my script" request- but finding it hard to phrase without using the working example.
To start, here's a relational diagram of what I'm working with;
**TABLE 1 - TABLE 2 are scalable, i.e there is a TABLE 4 - TABLE 5, TABLE -... etc, it's Tables, A/B/C that are constant.
Essentially, what I'm trying to do with this structure is match a certain value that only exists in TABLE 1 or 2 to TABLE C, where I'll pass that value to TABLE 1 or 2 and return the TABLE C value.
SELECT
TABLEC.CUSTOMID
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.TABLE2ENTITYKEY = TABLE2.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE2.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE1.USERENTITYKEY = /*ENTER_VALUE*/
OR
SELECT
TABLEC.CUSTOMID
FROM TABLE3
INNER JOIN TABLE4 ON TABLE3.TABLE4ENTITYKEY = TABLE4.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE4.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE3.USERENTITYKEY = /*ENTER_VALUE*/
Both of these work, and will work for my Tables 5/6, 7/8 etc - but that leaves me with multiple queries.
What I'd like to do is 'combine' these queries into one, where I'll pass my value to Tables 1/3/5 etc and return any values from C that link back to any of the source tables - it's not important that I know which source table the value in C links too, it's only important that it links to one of them.
Does anyone have any suggestions or examples I can use to see how this kind of script is formatted? I have played around with some things - but my SQL isn't that strong, so it's futile so far.
Thanks in advance, please comment if more information is required - and sorry in advance if the question is poorly asked!
-L

Not a full solution - but currently tying the two above queries together using UNION.

Related

Compare 2 Tables and write RowID from Table A to Table B

I have Table A with RowID, Date, Vendor and Cost, Confirmed
I have table B with RowID, Date, Vendor and Cost, Confirmed
Table A list our purchases.
Table B list the statement data from the credit card.
I would like to compare Date, Vendor and Cost in Table A with the same columns in Table B. If there is a match with those three columns, then I would like to take the RowID value from Table A and write it to the matching row in Table B under the Confirmation column.
I am very new to SQL and I am not even sure this is a reasonable expectation.
What do you think?
Is this enough detail to provide your opinion?
Thank you for any help you can give.
Currently I am using an Outer Right Join to get all the rows that do NOT have a match. What I really need is the opposite.
It might help to know what database engine you are using...
My answers are going to relate to MS SQL Server, but much SQL Syntax is the same...
To answer your first question, I would write something like:
Update TableB Set Confirmation = TableA.RowID From TableA
Where TableA.Vendor = TableB.Vendor
And TableA.Cost = TableB.Cost
And TableA.Date = TableB.Date
I would use aliases, but I left them out to hopefully make it easier to understand.
To answer your second question, you can specify an INNER JOIN which is the opposite of an OUTER JOIN and as you mentioned, what you are looking for, as it will return ALL rows that Match and exclude the rest.
You can do this with this query
UPDATE
B
SET
Confirmation = A.RowID
FROM
TableA A
INNER JOIN TableB B
ON B.Vendor = A.Vendor
AND B.Cost = A.Cost
AND B.Date = A.Date
Basically we do an inner join to keep the intersection (the records that match) of the two tables. and update the records that coincided from table B with the id of table A

SQL Statement for Accessing Data from Multiple Tables

I have 7 Tables as per attached following Image.
I will either enter Engine Number or Chassis Number and it should show the respective tables information (these tables have only mentioned fields) so all fields can be shown as result.
I can use hard coded Engine Number or Chassis Number. Every time of execution of this Query, I will hard code the required Engine/Chassis Number and will get the result.
Can anybody please help me to write this query for me?
Click Here to See the Tables
This might be a starting point for your solution.
SELECT prod.EngineNo AS engNo, prod.ChassisNo, doral.doralNo [, table.column [AS name]]
FROM DOProductSpecsDetais AS prod
INNER JOIN DORAL AS doral
ON prod.DOProductSpecsDetailID = doral.DOProductSpecsID
INNER JOIN DOProductDetail AS prodDetail
ON prod.DOProductDetailID = prodDetail.DOProductDetailID
WHERE prod.ChassisNo = '<input>' OR prod.EngineNo='<input>'
Between the SELECT and the FROM Statement, you can select any column out of your JOIN.
You can cascade as many JOINs as you like...
Which DBMS are you going to use?
One suggestion: Try to simplify the names of your columns, if possible.
One more: If you just started to do Database things, it is always helpful to start a test environment and use a client tool.
You can write query something like this:
select * from
DoProductSpecsDetail tbl1 inner join Doral tbl2
on tbl1.DoProductSpecsDetailId = tbl2.DoProductSpecsId
inner join DoproductDetail tbl3
on tbl1.DoProductDetailId = tbl3.DoProductDetailId
inner join ProductColor tbl4
on tbl1.ProductColorId = tbl4.ProductColorId
inner join DoDetail tbl5
on tbl3.DeliveryOrderDetailId = tbl5.DeliveryOrderId
inner join ProductMain tbl6
on tbl3.ProductId = tbl6.ProductId
inner join BPMain tbl7
on tbl5.BusinessPartnerId = tbl7.BusinessPartnerId

Having issues using a Right Join in MS Access 2010 with two tables that have the same fields

I have two tables, Table A and Table B. Each table have 4 fields, the name of the fields are the same for both. Both tables are extracted from other tables, and each record acts as a primary key.
I want to write a query in MS Access 2010 that gets the data unique to Table B and not shared with Table A. I am using the following image as a reference, and it looks like I need to do a Right Join.
Hello. There is something not right with my SQL, I've tested it and I am getting the incorrect result. Below is the closest I've gotten:
SELECT DISTINCT TableB.*
FROM TableB RIGHT JOIN TableA ON (TableB.Field1 = TableA.Field1) AND (TableB.Field2 = TableA.Field2) AND (TableB.Field3 = TableA.Field3) AND (TableB.Field4 = TableA.Field4)
WHERE (((TableA.Field1) Is Null));
I think it would be clearer for you to use not exists:
select tableb.*
from tableb
where not exists (select 1
from tablea
where (TableB.Field1 = TableA.Field1) AND (TableB.Field2 = TableA.Field2) AND (TableB.Field3 = TableA.Field3) AND (TableB.Field4 = TableA.Field4)
);
Your use of RIGHT JOIN is incorrect. As phrased, you want a LEFT JOIN. That is, you want to keep all rows in the first table (the "left" table in the JOIN) regardless of whether or not a match exists in the second table. However, the NOT EXISTS does the same thing and the logic is a bit clearer.
You want to have right join if tablea is in your select statement, but as you have
SELECT DISTINCT TableB.*
you may want to have a left join instead. My suggestion would be changing your code from right to left join.
TableB acts like table A from venn diagrams above.

Comparing two tables and finding mismatches

ive got two sql tables and i want to comepare them against eachother to find the ones that dont match.
i have something that works but for some reason missed out two records?
table flag_content contains:
- userid
- content_id
table topfive_order contains
- nid
- uid
i wish to find all records which topfive_order.nid doesnt exists in flag_content.content_id
my current query is:
select * from flag_content left join topfive_order topfive_order ON flag_content.content_id = topfive_order.nid WHERE topfive_order.nid is null
any tips or advice much welcome. im not too sure what im doing with left join.. so i assume that the couple of records which slip the net has something to do with that.
Turn the join around
SELECT *
FROM topfive_order topfive_order left join flag_content
ON flag_content.content_id = topfive_order.nid
WHERE flag_content.content_id IS NULL
To find rows from the topfive_order table that do not exist in the flag_content table, you need to put the topfive_order at the LEFT of the LEFT JOIN.
For more on various join types, see Wikipedia

Basic SQL join question. Can you help me improve my skillset?

Ok.. So I'm trying to improve my SQL skills and have a question. Here is a screen shot of the schema.
Schema http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif
(http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif)
Alright so I'm selecting a bunch of Report Bundles and other rows from a table you can see. I've got these two tables joining together correctly and displaying what should be returned. Now I need to add another field onto my result rows that states what type of report this is. How can I join up to the ReportGroupType table through the ReportBundleGroup table without getting a shwack of results?
Here is the query I am using so far.
SELECT *
FROM ReportBundleCustomerVisibility INNER JOIN ReportBundle
ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
Thanks again SO
SELECT *
FROM ReportBundleCustomerVisibility AS v
JOIN ReportBundle AS b ON b.ID = v.ReportBundleID
JOIN ReportBundleGroup AS g ON b.ID = g.ReportBundleID
JOIN ReportGroupTYpe AS t ON t.ID = g.ReportGroupTypeID
WHERE v.ReferenceCustomerID = 2303
It sounds like you just need another inner join to get the information you need. You can think about the second join as joining the result of the join with the ReportGroupType table. I added parenthesis to try to join the two sets the second INNER JOIN is operating on.
SELECT * FROM ((ReportBundleCustomerVisibility
INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID)
INNER JOIN ReportGroupType ON ReportBundleGroup.ReportGroupTypeID = ReportGroupType.ID)
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
I also highly suggest against using "SELECT *" in production code or any query you plan on reusing as the table schema can change and possibly effect reports and UI. Explicitly specify the columns instead.