two times inner join. but there is always syntax error - inner-join

SELECT E1.Name AS Empl, E2.Name AS HeadOfDepartment
FROM EMPLOYEE AS E1 INNER JOIN DEPARTMENT AS Dep
ON E1.DepartmentNr=Dep.DepartmentNr
INNER JOIN EMPLOYEE AS E2
ON Dep.Head=E2.EmployeeNr
ORDER BY Dep.Name;
i don't know where is the error.

Try following query.
SELECT E1.Name AS Empl, E2.Name AS HeadOfDepartment
FROM EMPLOYEE AS E1 INNER JOIN DEPARTMENT AS Dep
ON E1.DepartmentNr=Dep.DepartmentNr
INNER JOIN E1 ON Dep.Head = E1.EmployeeNr
ORDER BY Dep.Name;

Access database has a weird kind of syntax. Your SQL statement should work on most server type databases. Anyhow, try the code below and see if it works on your access.
SELECT E1.Name AS Empl,
E2.Name AS HeadOfDepartment
FROM EMPLOYEE AS E1
INNER JOIN (DEPARTMENT AS Dep
INNER JOIN EMPLOYEE AS E2
ON Dep.Head=E2.EmployeeNr)
ON E1.DepartmentNr=Dep.DepartmentNr
ORDER BY Dep.Name;

Related

SQL to find which table has diag_code filled, and then look it up in lookup_table

I have a query and a diag_code is either in one table (UM_SERVICE) or the other (LOS), but I can't join both tables to get diag_code that isn't null, that I can think of. Does this look ok for finding if diag_code is in one of the tables and lookup table? It's possible to have both LOS and UM_SERVICE have a diag code on different rows, and they could be different, and both or one could be in the lookup table. I'm not seeing anything in internet search.
Here's a simplified stored procedure:
SELECT distinct
c.id
,uc.id
,c.person_id
FROM dbo.CASES c
INNER JOIN dbo.UM_CASE uc with (NOLOCK) ON uc.case_id = c.id
LEFT JOIN dbo.UM_SERVICE sv (NOLOCK) ON sv.case_id = omc.case_id
LEFT JOIN dbo.UM_SERVICE_CERT usc on usc.service_id = sv.id
LEFT JOIN dbo.LOS S WITH (NOLOCK) ON S.case_id = UC.case_id
LEFT JOIN dbo.LOS_EXTENSION SC WITH (NOLOCK) ON SC.los_id = S.id
INNER JOIN dbo.PERSON op with (NOLOCK) on op.id = c.Person_id
WHERE
(sv.diag_code is not null and c.case_id = sv.case_id
or
s.diag_code is not null and c.case_id = s.case_id)
and
(sv.diag_code is not null and sv.diag_code in (select diag_code from TABLE_LOOKUP)
or
s.diag_code is not null and s.diag_code in (select diag_code from TABLE_LOOKUP)
Table setups like this:
CASES
id person_id
UM_CASE
case_id
LOS
case_id id
LOS_EXTENSION
los_id
Person
id cid
UM_SERVICE
case_id diag_code
UM_SERVICE_CERT
service_id id
TABLE_LOOKUP
diag_code
Since you have two different searches being run, it is going to be much easier to write/read by writing the searches individually and then bringing your two results sets together using the UNION operator. The UNION will eliminate duplicates across the two result sets in a similar manner to what your usage of SELECT DISTINCT is doing for a single result set.
Like so:
/*first part of union performs seach using filter on dbo.UM_SERVICE*/
SELECT
c.id
,uc.id
,c.person_id
FROM
dbo.CASES AS c
INNER JOIN dbo.UM_CASE AS uc ON uc.case_id=c.id
LEFT JOIN dbo.UM_SERVICE AS sv ON sv.case_id = omc.case_id
LEFT JOIN dbo.UM_SERVICE_CERT AS usc on usc.service_id=sv.id
LEFT JOIN dbo.LOS AS S ON S.case_id = UC.case_id
LEFT JOIN dbo.LOS_EXTENSION AS SC ON SC.los_id= S.id
INNER JOIN dbo.PERSON AS op on op.id=c.Person_id
WHERE
sv.diag_code in (select diag_code from TABLE_LOOKUP) /*will eliminate null values in sv.diag_code*/
UNION /*deduplicate result sets*/
/*second part of union performs search using filter on dbo.LOS*/
SELECT
c.id
,uc.id
,c.person_id
FROM
dbo.CASES AS c
INNER JOIN dbo.UM_CASE AS uc ON uc.case_id=c.id
LEFT JOIN dbo.UM_SERVICE AS sv ON sv.case_id = omc.case_id
LEFT JOIN dbo.UM_SERVICE_CERT AS usc on usc.service_id=sv.id
LEFT JOIN dbo.LOS AS S ON S.case_id = UC.case_id
LEFT JOIN dbo.LOS_EXTENSION AS SC ON SC.los_id= S.id
INNER JOIN dbo.PERSON AS op on op.id=c.Person_id
WHERE
s.diag_code in (select diag_code from TABLE_LOOKUP); /*will eliminate null values in s.diag_code*/

why INNER JOIN errors out at the plus sign?

This is very similar but NOT exactly the same error in an answer I saw for subject "MySQL inner join". The problem is an error at the WHERE statement, as follows: "ERROR 2.5could not prepare statement (1 no such column: Orders.OrdersID)". Are my INNER JOIN statements wrong/Too cheesy?. I have checked each table to ensure fieldnames are correct, nothing works. The Table named ORDERS has the following fields :OrderID CustomerID EmployeeID OrderDate ShipperID -- Table named OrderDetails has the following fields: OrderDetailID OrderID ProductID Quantity |||||| Does anyone has an idea or solution for this error?. (It is driving me nuts!!!) . Here is the code:
CREATE VIEW OrderView AS
SELECT Employees.EmployeeID AS 'Employee_EMPLOYEE_ID',
Orders.OrderID AS 'Orders_ORDER_ID',
Orders.EmployeeID AS 'Orders_EMPLOYEE_ID',
OrderDetails.OrderID AS 'OrderDetails_ORDER_ID',
OrderDetails.Quantity AS 'OrderDetails_QTY'
FROM
Employees
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN OrderDetails On OrderDetails.OrderID = Orders.OrderID
WHERE OrderDetails.OrderID = Orders.OrdersID
AND OrderDetails.Quatity > 30
ORDER BY Employees.EmployeeID;
I corrected the failing set of statements by using different tables, putting all the statements in an indented way, ran in under w3schools and it worked perfectly, as follows:
CREATE VIEW OrderView AS
SELECT Orders.OrderID,
Orders.CustomerID,
Customers.CustomerID AS C_CUSTOMERID,
Orders.CustomerID AS O_CUSTOMERID,
Orders.ShipperID as O_SHIPPERID,
Shippers.ShipperID as S_SHIPPERID,
Customers.Country,
Customers.CustomerName,
Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID)
WHERE Customers.Country='Mexico' or Customers.Country='Germany' or
Customers.Country='Spain'
GROUP BY Customers.CustomerID
HAVING Customers.CustomerID > 39
ORDER BY Customers.Country ASC;

What happens if you have a table in subquery with same alias as same table in outer query?

Working on a somewhat large query trying to improve performance. One thing I found is that there is a spot with a subquery, but the inner query has the same table as the outer query, with the same alias. Is this defined behavior?
select * from documents d
left join ( select distinct d.id, 'Yes' as 'IsCertainType' from documents d
left join documentAttributes da on d.id = da.id where da.Description like '%CertainType%'
) #certainType
on d.Id = #certainType.Id
It is fine as the inner and outer queries have different scopes
I see no reason for the subquery at all. Just do the Left JOIN and be done with it.
select distinct d.id, 'Yes' as 'IsCertainType'
from documents d
left join documentAttributes da
on d.id = da.id
where da.Description like '%CertainType%'
OR As stated in my comment if other rows with the same id in table documents has different values than the returned row by distinct, you can do GROUP BY
select d.id, d.someColum, etc. 'Yes' as 'IsCertainType'
from documents d
left join documentAttributes da
on d.id = da.id
where da.Description like '%CertainType%'
GROUP BY d.id, d.someColumn, etc..

Select all the data from another table even if corresponding value from another table is NULL

I have this query:
SELECT city.CITY_NAME,
SUM(case when c.CUSTOMER_ID=o.CUSTOMER_ID and o.ORDER_ID=od.ORDER_ID
then od.TOTAL_AMT_PER_ITEM
else 0 end) AS TOTAL_AMT_PER_ITEM
FROM [ORDER] o
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
INNER JOIN CUSTOMER c
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CITY city
ON ca.CITY_ID = city.CITY_ID
GROUP BY city.CITY_NAME
I am a beginner in SQL SERVER. This query displays only the CITY_NAME that has a corresponding TOTAL_AMT_PER_ITEM value. What I need is to display all the CITY_NAMEs in the database even if their corresponding value is NULL. What is the work around for this? Can someone help me out? Thanks!
I change the order of the joins maybe that help.
You start with CITY because is the source for your GROUP BY and try to see if have any CUSTOMER_ADDRESS.
I guess if is a new store you can have 0 customers.
Then INNER JOIN because direction cant exist alone, they belong to a customer
Then LEFT JOIN because again a CUSTOMER may or may not have [ORDERS].
Then INNER JOIN because every [ORDERS] have [ORDER DETAILS]
Finally you SUM(od.TOTAL_AMT_PER_ITEM) from the last JOIN table, this can get some NULL's so you need include COALESCE
SELECT city.CITY_NAME,
COALESCE(SUM(od.TOTAL_AMT_PER_ITEM) , 0) as TOTAL_AMT_PER_ITEM
FROM [CITY]
LEFT JOIN [CUSTOMER_ADDRESS] ca
ON ca.CITY_ID = [CITY].CITY_ID
INNER JOIN CUSTOMER c
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN [ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
GROUP BY [CITY].CITY_NAME
btw you should change the name of the table [Order] to [Orders] because Order is a reserved word and can cause problems.
In general I rather use the plural name for tables because is an entity saving multiple of one type
CITIES instead of CITY
CUSTOMERS intead of CUSTOMER
ORDER_DETAILS is already plural, so try to keep consistence.
SELECT
city.CITY_NAME,
SUM(od.TOTAL_AMT_PER_ITEM) AS TOTAL_AMT_PER_ITEM
FROM
CUSTOMER c
INNER JOIN
CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN
CITY city
ON ca.CITY_ID = city.CITY_ID
LEFT JOIN
[ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN
ORDER_DETAILS
ON o.ORDER_ID = od.ORDER_ID
GROUP BY city.CITY_NAME

SQL Server Select query is very slow. How can I get better result?

I've a very slow SQL query on SQL Server 2005. It takes almost 40 seconds just to do a select. After the select, I get result query and do an insert using this result. Every "textField" and "Id" is index unique and every "Id" is PK. Please what can I do to get better results? Thanks!
SELECT DISTINCT
C.Id, E.Id, B.Id, A.Id
FROM
tableA AS A
INNER JOIN
tableB AS B
INNER JOIN
tableC AS C
INNER JOIN
tableD AS D ON C.textField = D.textField
INNER JOIN
tableE AS E ON D.textField = E.textField
ON B.textField = D.textField
ON A.textField = D.textField
First off, your query is not optimized very well at all.
For your inner joins on tables A, B and C you should have some conditions on those joins or you will get a Cartesian product on those tables.
SELECT DISTINCT C.Id, E.Id, B.Id, A.Id
FROM tableA AS A INNER JOIN
tableB AS B **ON B.<fieldname> = <A.fieldname>** INNER JOIN
tableC AS C **ON C.<Fieldname> = <B.FieldName>** INNER JOIN
tableD AS D ON C.textField = D.textField INNER JOIN
tableE AS E ON D.textField = E.textField ON B.textField = D.textField ON A.textField

Resources