How do I join two tables with the SQL Query in the image attached? Currently is pulling no data.
Here is an example of the report I need
Example report
I think this would work, you can change the type of JOIN according to your requirement:
SELECT
o.DMN_ID,
o.ORG_ID,
o.ORG_DESC,
o.ORG_TYP_ID,
o.LST_UDR_USR,
o.LST_UPD_TSTMP,
og.ORG_GROUP_ID,
og.ORG_GROUP_DESC,
og.LST_UPD_USR,
og.LST_UPD_TSTMP
FROM PA_ORG o INNER JOIN
PA_ORG_GROUP og ON
o.ORG_ID = og.ORG_GROUP_ID
and o.ORG_DESC = og.ORG_GROUP_DESC
Related
I have a table with Customers which includes their contact person in the helpdesk. I have another table that lists all vacancies of the helpdesk employees - if they are currently sick or on vacation etc.
I need to get the helpdesk contact and the start/end time of their vacation IF there is an entry.
I currently have this (simplified):
SELECT *
FROM dbo.Customers, dbo.Projects, dbo.Vacations
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
AND dbo.Projects.HDContactID = dbo.Vacations.HDContactID
So if there is a vacation listed in the Vacations table, it works fine, but if there is no vacation at all, this will not return anything - what i want is that if there is no vacation, it simply returns the other data, and ignores the missing data (returns NULL, doesn't return anything, not important)
In any case, I need to get the Customers and Project data, even if the query can't find an entry in the Vacations table. How would I do this? I pretty new to SQL and couldn't find a similar question on this site
EDIT: I'm using SQL Server, currently using HeidiSQL
Try below query:
SELECT * FROM dbo.Customers, dbo.Projects
left join dbo.Vacations on dbo.Projects.HDContactID = dbo.Vacations.HDContactID
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
Use left join as mentioned by #Flying Thunder,
Example of the left join:
SELECT country.country_name_eng, city.city_name, customer.customer_name
FROM customer
LEFT JOIN city ON customer.city_id = city.id
LEFT JOIN country ON city.country_id = country.id;
You can find a nice guide for the joins and SQL here:
https://www.sqlshack.com/learn-sql-join-multiple-tables/
You should be using LEFT JOIN. In fact, you should never be using commas in the FROM clause. That is just archaic syntax and closes the powerful world of JOINs from your queries.
I also recommend using table aliases that are abbreviations of table names. The best are abbreviations for the table names:
SELECT *
FROM dbo.Customers c LEFT JOIN
dbo.Projects p
ON c.CustomerID = p.CustomerID LEFT JOIN
dbo.Vacations v
ON p.HDContactID = v.HDContactID
WHERE c.Phone = $Phone;
Have you try this to skip vacation record if not present like this:
SELECT * FROM dbo.Customers, dbo.Projects, dbo.Vacations
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
AND (dbo.Vacations.HDContactID IS NULL OR dbo.Projects.HDContactID = dbo.Vacations.HDContactID)
I have four tables FDQueries (Firebird 3.0) COUNTRIES, REGIONS, DEPARTMENTS, AND CITIES and a fifth temporay FDQuery where to display CITIES table with related data from others tables as following :
SELECT C.NAMEXXX AS NAMEXXX, D.NAMEXXX as NAMEXXX, R.NAMEXXX AS NAMEXXX, T.NAMEXXX AS NAMEXXX
FROM GEOCITI C
LEFT JOIN GEODEPT D ON D.PKINDEX = C.FKDEPAR
LEFT JOIN GEOREGI R ON R.PKINDEX=D.FKREGIO
LEFT JOIN GEOCOUN T ON T.PKINDEX=R.FKCOUNT
The join is working perfectly and showing what i want :
Now this done, i want the user to edit the CITIES table directly from the Grid showing the joined table (Temporary) My question is how to do this the right way to update with a simple post only original CITIES Table
Thanks and sorry for my first question formulation
You can use following query to update table using join -
UPDATE CITIES
SET CITYPOPULATION = YOUR_UPDATED_VALUE
FROM CITIES C
JOIN DEPARTMENTS D ON C.departmentname = D.NAME
JOIN COUNTRIES CT ON D.countryname = CT.name;
You can read this article for further update.
I'm having an issue where I'm getting some incorrect values in my output. I am binding the below highlighted table column with the circled column down the bottom. The service_id on the highlighted column is what is unique, but I need to bind the booking_id to retrieve the info (if that makes sense. What I end up getting is the top table where I get repeats, or the price is wrong. I should be getting only 5 lines in the top table.
Here's my code. I suspect I might be doing the join wrong?
SELECT bad.agent as Agents,
dog.SUPPLIER as SUPPLIER,
bad.status as TheStatus,
country.analysis_master1 as Country,
ftb.booking_actual_retail as BookingActualRetail,
ftb.Booking_Actual_Cost as BookingCost,
ftb.Booking_Actual_Retail_inc as BookingRetailINC,
fts.Service_Id,
fts.Service_Actual_Retail_inc as ServiceActualCostInc,
Product.SERVICE,
Product.SL_STATUS as SLSTATUS,
cat.name as Product2,
bad.LAST_SERVICE_DATE as Servicedate,
bad.LW_DATE as LWDATE,
ftb.Entered_Date as DATEENTERED,
ftb.Booking_Pax as PEOPLE,
ftb.Booking_Children as KIDS,
bad.TRAVELDATE as TRAVELDATE,
bad.FULL_REFERENCE
from BHD bad
inner join FTB on bad.FULL_REFERENCE = ftb.booking_reference
inner join FTS on FTB.Booking_Id = fts.Booking_Id
inner join DRM Country on bad.agent = country.code
inner join BSL Product on bad.BHD_ID = Product.BHD_ID
inner join SRV cat on Product.SERVICE = cat.CODE
inner join OPT dog on Product.OPT_ID = dog.OPT_ID
where bad.STATUS = 'IV' AND bad.FULL_REFERENCE = 'LTIT129488'
UPDATE:
Ok, so it looks like this join here causes the multiple outputs:
inner join FTS on FTB.Booking_Id = fts.Booking_Id
I have included the two tables, their headers, and sample data
You have somewhere put the join for the service or supplier in the wrong way.. Please check this line again.
inner join SRV cat on Product.SERVICE = cat.CODE
UPDATED SOLUTION :
As per your updated screenshots, I found the issue...
you have joined like this.
inner join FTB on bad.FULL_REFERENCE = ftb.booking_reference
In this join, your one table has single record against booking reference while another have multiple records against booking refrence. Thats why you are getting the multiple records in the output.
Remove this join and your problem will be solved. If you really want the data from this table then you can select in other way like using outer apply etc.
I have this query which is returning some redundant data. Can somebody please help me to correct this query. I have also attached the table relationship.
SELECT
dbo.supplierOrder.sectionID,
dbo.supplierOrder.supplierID,
dbo.supplierOrder.supplierOrderNo,
dbo.supplierOrder.supplierOrderCreated,
dbo.supplierOrder.supplierOrderConfirmStatus,
dbo.supplierOrderDetails.productID,
dbo.supplierOrderDetails.orderQty,
dbo.supplierOrderReceive.invoiceno,
dbo.supplierOrderReceive.supplierInvoiceno,
dbo.supplierOrderReceive.ID,
dbo.supplierOrderReceiveDetail.qtyArrived
FROM
dbo.supplierOrder
INNER JOIN
dbo.supplierOrderDetails ON (dbo.supplierOrderDetails.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN
dbo.supplierOrderReceive ON (dbo.supplierOrderReceive.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN
dbo.supplierOrderReceiveDetail ON (dbo.supplierOrderReceiveDetail.supplierOrderReceiveID = dbo.supplierOrderReceive.ID)
WHERE
dbo.supplierOrder.ID = 1 ;
here is the output that I get
which is not exactly what I was want. the last column qtyArrived should show only its related qty but here as you can see it is repeating for each of the productID
and here is the db tables and relationship
I think the last table should be join using also ProductId not only OrderID :
LEFT JOIN
dbo.supplierOrderReceiveDetail
ON
(dbo.supplierOrderReceiveDetail.supplierOrderReceiveID=dbo.supplierOrderReceive.ID)
AND
(dbo.supplierOrderReceiveDetail.ProductID =dbo.sdbo.supplierOrderDetails.ProductID)
Hi guys I have a query like somethign below. I am using two inner join and I am select from two two but different coloums
the first inner join changes to see if any of the staffs shares a company vehicle with other staff.
The second inner join changes to see if the what staff level drivers what type of vechicle in the company.
select
van_col1, van_col2, admin_col3, admin_col4
from
user
INNER JOIN
admin
ON
user.van_col1=adim.admin_col3
INNER JOIN
user
ON
adim
adim.admin_col3=user.van_col2
If you join the same table twice then you have to use alias names to differ the joins of the table
select u1.van_col1, u2.van_col2, a.admin_col3, a.admin_col4
from user u1
INNER JOIN admin a ON u1.van_col1 = a.admin_col3
INNER JOIN user u2 ON a.admin_col3 = u2.van_col2
And if you have columns in that tables that are named equaly then you have to tell the DB from which table you like to take the column, otherwise it would be ambiguous.