Joining three tables using result from one SQL query on one of the tables - sql

I'm trying to search one table for entities with a certain field in one table and use fields within that entity to to join it with two other tables specific fields
first I search for entities with CustomerID == 2 and use their other fields to search to other tables
Using BoxTypeID from those specific entities I want to find the corresponding BoxType
Using WarehouseID from those specific entities I want to find the corresponding name
Once I have found all three fields (BoxID, BoxType, name) I want to display them all in one table
I have some SQL that works but only displays the BoxID and Warehouses.name. I can get it to also display BoxID and BoxType however not all three at once.
SELECT Boxes.BoxID, Warehouses.name AS Warehouse
FROM Boxes
INNER JOIN Warehouses
ON Boxes.WarehouseID = Warehouses.WarehouseID
WHERE Boxes.CustomerID = 2;
resulting table from query above
However once I try to apply another JOIN it doesn't work
SELECT Boxes.BoxID, Warehouses.name AS Warehouse, BoxTypes.BoxType as Size
FROM Boxes
INNER JOIN Warehouses
ON Boxes.WarehouseID = Warehouses.WarehouseID
INNER JOIN BoxTypes
On Boxes.BoxTypeID = BoxTypes.BoxTypeID
Where Boxes.CustomerID = 2;

Related

SQL select with three tables

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

SQL Joining tables - To only display rows that have matching parameters

I have 2 tables that I am attempting to join but to only display rows of data based on specific parameters.
Table one: "orders"
Table two: "cash_tracking"
Matching parameters from both tables: "orders.user_id" and "cash_tracking.user_id" and the user_id is 2640
The result I want to see is all the "orders.name" from the "user_id=2640" on a specific shop "shop_id = 7777" and based on a specific register number from the "cash_tracking" table eg. "cash_transaction_reg=444454"
However, when I run my query it shows me every single order on this shop instead of the ones related to the user 2640 on this specific register - 444454
Here is what I have so far:
SELECT orders.name
FROM orders
Inner JOIN cash_tracking
ON orders.user_id = cash_tracking.user_id
WHERE orders.shop_id = 7777
AND cash_tracking.user_id=2640
AND cash_tracking.cash_transaction_reg=444454
You select all orders for user_id, shoud be some join orders.Id = cash_tracking.OrderId

Outputting the data from several sql tables without having a common value

I have a select query which combined several tables. PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS and SERVICE_GUARANTY_NEW have common value however STOCKS table does not.
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT,
workcube_test_1.PRODUCTION_ORDERS PR_ORDERS,
workcube_test_1.SERVICE_GUARANTY_NEW SG,
workcube_test_1.STOCKS S
WHERE PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
AND PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
when I run the query, it shows the output as below.
The problem here is there are four data rows returned from PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS, SERVICE_GUARANTY_NEW and once I have added the STOCKS table, arrived_city, montaj_date, transfer_date columns are side by side with STOCKS table's rows, but the columns value should be null, not filled with data.
The way I tried is UNION of STOCKS table, however unioned table values are ignored, can not use them in html blocks.
there needs to be at least one more join condition among tables where there's for STOCKS table, I think there might exist such a column STOCK_ID within a table such as PRODUCTION_ORDER_RESULTS in order to join with STOCKS table. I think this should be the reason for multiple returning rows. If there's no common column, then the returning data will be produced as many as the number of records within STOCK table due to existing CROSS JOIN logic within the current query. So rearrange your query as
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT
JOIN workcube_test_1.PRODUCTION_ORDERS PR_ORDERS
ON PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
JOIN workcube_test_1.SERVICE_GUARANTY_NEW SG
ON PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
JOIN workcube_test_1.STOCKS S
ON PR_ORDER_RESULT.STOCK_ID = S.ID

I want to use CONCAT and exclude and duplicate any duplicate entries

I'm trying to use the CONCAT expression, but also exclude any duplicate entries.
So I'm trying to update a report based on a single process held in our product. The problem is that whoever created the tables that the current report is pulling from is not from a single table. Currently I have found three tables that the report pulls from for one column.
SELECT concat(dbo.t_log_TaskBody.TaskDescription,' ', dbo.t_ezDocument.FileName) as Title
FROM dbo.t_logs_SigDocPrintedEmailed
LEFT JOIN dbo.t_log_Data ON t_logs_SigDocPrintedEmailed.t_ezDataPKid = dbo.t_log_Data.PKid
LEFT JOIN dbo.t_log_TaskBody ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_log_TaskBody.DocumentId
LEFT JOIN dbo.t_ezSignDocumentQ ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezSignDocumentQ.PKid
LEFT JOIN dbo.t_ezArcSigDocQLog ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezArcSigDocQLog.t_ezSignDocQPKID
LEFT JOIN dbo.t_ezDocument ON dbo.t_ezSignDocumentQ.t_ezDocument = dbo.t_ezDocument.PKID or dbo.t_ezArcSigDocQLog.t_ezDocument = EasyID.dbo.t_ezDocument.PKID
So now that I have one entry that happens to connect to two of the tables I'm pulling from to get a title of a document I end up with the title appearing twice in one box. Is there anyway I can use CONCAT to combine the two tables while keeping it to unique entries, or is there a better way of doing this.
I'll get Something along the lines of:
Title
null
Title Title
null
Title
Title
Based on your sample results, I think you want COALESCE(), not CONCAT():
concat(dbo.t_log_TaskBody.TaskDescription, dbo.t_ezDocument.FileName) as Title
You have no examples where results are actually concatenated.

tableau join multiple tables ORing on single column

I have following tables: Product and ProductEvent. Some of the relevant columns of Product table are SocialID, MarketID, LocalID. These three columns have numbers that corresponds to one column called EventID in ProductEvent.
Here is my question: is there a better way to join these tables where I do not have to make 3 separate joins all pointing to match EventID?
Maybe something like this:
select *
from Product P join ProductEvent PE
on (PE.EventID = P.SocialID or PE.EventID = P.MarketID or PE.EventID = P.LocalID)