Unknow column in ON clause - sql

I have the following query:
select p.idPerson from person p "
+ " join dog g on g.idDog = p.idDog, "
+ " tree t "
+ " join parents p1 on p1.idParents = t.idParents "
+ " join parents p2 on p2.idParents = p.idParents "
I've changed parameters, i know these query doesn't make sense, i'ts just to show you the structure, so my error is:
Unknown column 'p.idParents' in 'on clause'
This does even less sense, i think the error is on the "tree t", the only one which is not join, but i don't know how to solve it becuase it hasn't got any relation with the other tables to do a join.
Thanks!

Related

Oracle Sql Joining from 3 tables

This is my solution for querying the columns needed:
"SELECT " +
"OL.ORDER_NO, OL.ITEM, OL.LOCATION, OL.LOC_TYPE, OL.QTY_ORDERED, OL.UNIT_COST, OL.UNIT_RETAIL, " +
"OL.QTY_PRESCALED, OL.QTY_RECEIVED, OL.LAST_RECEIVED, OL.LAST_ROUNDED_QTY, OL.LAST_GRP_ROUNDED_QTY, " +
"OL.QTY_CANCELLED, OL.CANCEL_CODE, OL.CANCEL_DATE, OL.CANCEL_ID, OL.ORIGINAL_REPL_QTY, OL.UNIT_COST_INIT, " +
"OL.COST_SOURCE, OL.NON_SCALE_IND, OL.TSF_PO_LINK_NO, OL.ESTIMATED_INSTOCK_DATE, OS.REF_ITEM, " +
"OS.ORIGIN_COUNTRY_ID, OS.EARLIEST_SHIP_DATE, OS.LATEST_SHIP_DATE, OS.SUPP_PACK_SIZE, " +
"OS.PICKUP_LOC, OS.PICKUP_NO, WH.WH_NAME " +
"FROM RMS.ORDLOC OL " +
"left outer join RMS.WH WH on WH.WH = OL.LOCATION " +
"join RMS.ORDSKU OS on OS.ORDER_NO = OL.ORDER_NO and OS.ITEM = OL.ITEM " +
"WHERE OL.ORDER_NO IN(:ORDER_NO)";
This solution gives me what I need, I'm just curious about if it is the best solution in terms of query speed.
I'm not sure how to figure out the time complexity and if this is the best solution for the query. I have very limited knowledge on relational algebra. My understanding is to join smaller tables into the larger table which I am doing, and to avoid or's which I am doing. But is there a better query that I am not seeing?
Thanks in advance.

MS Access VB SQL Syntax for 3 Table Join

I have been trying to create a form which contains information from 3 tables. On this form I would like to have a filter combo box so that the user may select a member type and it will filter the list to members with that member type.
Members in our database can have more than one type. For this reason I created a memberTable:
MemberID
FName
SName
etc.
A membertype table:
MemberTypeID
MemberTypeName
and a joining table membermembertype:
MemberID
MemberTypeID
I have tried to set my form up with a number of different queries:
SELECT qryMemberType.MemberID, qryMemberType.FName, qryMemberType.SName,
qryMemberType.MemberTypeName, qryMemberType.MemberTypeID FROM qryMemberType;
SELECT [member].[MemberID] AS [member_MemberID], [member].[FName], [member].[SName],
[membertype].[MemberTypeID] AS [membertype_MemberTypeID], [membertype].[MemberTypeName],
[membermembertype].[MemberID] AS [membermembertype_MemberID], [membermembertype].[MemberTypeID]
SELECT [member].[MemberID] AS [member_MemberID], [member].[FName], [member].[SName],
[membermembertype].[MemberID] AS [membermembertype_MemberID],
[membermembertype].[MemberTypeID] AS [membermembertype_MemberTypeID],
[membertype].[MemberTypeID] AS [membertype_MemberTypeID]
The VBA for the combo box that I have been attempting to filter with is:
Dim myMember As String
myMember = "SELECT ... FROM ... WHERE ([MemberTypeID] = " & Me.cboMemberType & ");"
Me.frmFilterTestSub.Form.RecordSource = myMember
Me.frmFilterTestSub.Form.Requery
I have tried numerous different syntaxes for the SELECT statement in the VBA for the combo box including:
"Select * from membermembertype where ([MemberTypeID] = " & Me.cboMemberType & ")"
"SELECT member.MemberID AS member_memberID, member.FName, member.SName,
membertype.MemberTypeID AS membertype_MemberTypeID, membertype.MemberTypeName,
membermembertype.MemberID AS membermembertype_MemberID,
membermembertype.MemberTypeID AS membermembertype_MemberTypeID
FROM member
LEFT JOIN (membertype
RIGHT JOIN membermembertype ON membertype.[MemberTypeID] = membermembertype.[MemberTypeID])
ON member.[MemberID] = membermembertype.[MemberID]
WHERE ([MemberTypeID] = " & Me.cboMemberType & ")"
"SELECT member.*,
(membermembertype.MemberID AS mmt_MemberID),
(membermembertype.MemberTypeID AS mmt_MemberTypeID),
membertype.MemberTypeName
FROM (member
LEFT JOIN membermembertype ON mmt_MemberID = (member.MemberID AS m_MemberID))
RIGHT JOIN membertype ON mmt_MemberTypeID=(membertype.MemberTypeID AS mt_MemberTypeID)
WHERE ([MemberTypeID] = " & Me.cboMemberType & ");"
I'm pretty sure the first example returned the closest result with the list returning the correct number of records for each member type selected, however the fields were filled with the #Name? error value which is why I was trying to do a number of different ways to join the tables. The other SELECT STATEMENTS are returning SQL Syntax errors.
Any advice would be greatly appreciated!
Since you only want to list the members, I guess that you are not really interested with membertype table info, then you can simply do this :
"SELECT member.MemberID AS member_memberID, member.FName, member.SName
FROM member
INNER JOIN membermembertype
ON (membermembertype.[MemberTypeID] = " & Me.cboMemberType & " AND member.[MemberID]=membermembertype.[MemberID]) "
Right outer join is highly frowned on in SQL, when you can use a left outer join in the correct order instead.
In your case, inner joins are better anyway.
You should use a parameter rather than string concatenation if you care about security, but since you're using Access I guess anyone with access to the file can already do what they like.
Please try this one and let me know if you get an error.
SELECT [Member].MemberId
, [Member].FName
, [Member].SName
, [MemberType].MemberTypeId
, [MemberType].MemberTypeName
FROM ((MemberType
INNER JOIN MemberMemberType ON MemberMemberType.MemberTypeId = MemberType.MemberTypeId)
INNER JOIN Member ON Member.MemberId = MemberMemberType.MemberId)
WHERE MemberType.MemberTypeId = 123 --Put your parameter here once you've tested that it works for a specific value

How to transform SQL query into JPQL query?

I'm looking for how to transform the following query (SQL) into query JPQL;
SELECT * FROM equipements eq
LEFT JOIN check_lists checks
ON eq.id_equipements = checks.equipements_id
LEFT JOIN responses_check_lists resp
ON checks.id_check_lists = resp.check_lists_id
AND resp.missions_id = 15
AND eq.id_equipements = 1
ORDER BY checks.id_check_lists
I followed the documents on the internet but I do not get the correct transformation of my query in JPQL.
I know that the attributes of the query will be replaced by the attributes of the class.
I posted here to help me in transforming the SQL query.
Edit1:
#Query(
"SELECT r, checks, eq"
+ " FROM Equipements eq"
+ " LEFT JOIN CheckLists checks "
+ " ON eq.idEquipements = checks.equipements.idEquipements"
+ " LEFT JOIN ResponsesCheckLists r"
+ " ON checks.idCheckLists = r.CheckLts.idCheckLists"
+ " AND r.Respmission.idMission= :idmiss "
+ " AND eq.idEquipements= :idEqp"
+ " ORDER BY checks.idCheckLists ASC"
)
Error of Edit1:
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'r' {originalText=r}
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path:
'checks.idCheckLists'
Thank you in advance,
Analysing your SQL and without see the entities, your JPQL can be something like this:
SELECT eq FROM Equipements eq
LEFT JOIN eq.checks check
LEFT JOIN check.responses resp
LEFT JOIN resp.missions mission WITH mission.id = :idmiss
WHERE eq.id = :idEqp
ORDER BY check.id
But, to be something like this, you need to adjust the mapping of your entities (entity name, column names, etc).

How to combine asterix syntax with table abbreviations

Is it possible to combine * syntax with table abbreviations?
I want to do something like:
"SELECT subfunds.* FROM subfunds S" +
" INNER JOIN funds F ON S.id_fund = F.id" +
" WHERE F.fund_short IN('" + stSQLFundList + "')"
The above code gets a syntax error
"invalid reference to FROM-clause entry for table "subfunds".
I already found that if I do
"SELECT * FROM subfunds S" +
" INNER JOIN funds F ON S.id_fund = F.id" +
" WHERE F.fund_short IN('" + stSQLFundList + "')"
then I get all fields from both tables rather than from the subfunds table only.
So how do I get all fields from the first table (and none of the other tables' fields) in my answer set while also being able to use the single-letter table abbreviations?
Change your code to this and you will get all fields from subfunds.
"SELECT S.* FROM subfunds S" +
" INNER JOIN funds F ON S.id_fund = F.id" +
" WHERE F.fund_short IN('" + stSQLFundList + "')"
If you are using an alias, then you want to reference that table by it's alias.

Syntax Error in Join Operation in MS-Access when splitting and comparing records

Above error message occurs with this statement:
SELECT f.fullname INTO SummaryJudgment_FinalForgottenWithMiddle
FROM (
(SELECT Left([aname],InStr(1,[aname],",")-1)) As lastname FROM
SummaryJudgment_FinalForgotten) & " " & (SELECT
RIGHT([aname],InStr(1,[aname],",")+1)) As firstname FROM
SummaryJudgment_FinalForgotten) & " " & (SELECT
summary_judgment.middle_initial AS middlename FROM summary_judgment)
) AS fullname
FROM SummaryJudgment_FinalForgotten AS f INNER JOIN summary_judgment
AS s ON f.lastname = s.last_name && f.firstname = s.first_name;
Basically this is what two tables look like (note they will have more fields than 1 where last or first name of different fields can be similar):
SummaryJudgment_FinalForgotten (table)
aname (field)
Leventhal,Raymond (data)
summary_judgment (table)
first_name(field)
Raymond (data)
last_name (field)
Leventhal (data)
middle_initial (field)
P (data)
Ultimately, I'm trying to create a new table that is like
SummaryJudgment_FinalForgotten but with the middle initial from
summary_judgment appended:
Leventhal,Raymond P
You do not need to write 3 select statements to concatenate the values into one field.
select left(...) & right(...) & initial AS fullname INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten
Are you trying to use two ampersands to represent a logical AND?
FROM SummaryJudgment_FinalForgotten AS f INNER JOIN summary_judgment
AS s ON f.lastname = s.last_name && f.firstname = s.first_name;
I don't think that's legal for Access' Jet/ACE database engines. Try it with the AND keyword in place of &&.
OTOH, I wonder if you can do something simpler.
SELECT last_name & "," & first_name & " " & middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM summary_judgment;
This works:
SELECT left([aname],InStr(1,[aname],",")-1) & " "
& right([aname],Len(aname)-InStr(1,[aname],",")) & " "
& summary_judgment.middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten, summary_judgment;
Though you might want this instead:
SELECT left([aname],InStr(1,[aname],",")-1) & ", "
& right([aname],Len(aname)-InStr(1,[aname],",")) & " "
& summary_judgment.middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten, summary_judgment;
The second version gives you the comma after the last name. Note that Right counts from the right, which is why you have to subtract the InStr value from the length.
EDIT:
The code I gave above works with your sample data--one row in each table. With more rows, it gives a cross product of (LastName, FirstName) x MiddleInitial. It occurred to me that that might be the case, so I went back to my test & added a second row--it is true. So then I tried to write the join expression....
Access doesn't like this:
... ON left([aname],InStr(1,[aname],",")-1) = last_name ...
It throws the error "Join expression not supported." Changing it to this:
... ON (trim((left(SummaryJudgment_FinalForgotten.aname,InStr(1,[aname],",")-1))=trim(summary_judgment.last_name))) ...
results in a query that runs & creates the table, but doesn't create any rows (the same was true before I added the "trim" calls in an attempt to fix it).
So I tried specifying the table for all occurences of aname. No joy--until I realized that I was making the wrong comparison (derived-last to last and derived-first to last--oops).
Using the following FROM clause with either above SELECT ... INTO does work correctly:
FROM
SummaryJudgment_FinalForgotten INNER JOIN
summary_judgment ON
((left(SummaryJudgment_FinalForgotten.aname,InStr(1,SummaryJudgment_FinalForgotten.[aname],",")-1))=summary_judgment.last_name) AND
((right(SummaryJudgment_FinalForgotten.aname,Len(SummaryJudgment_FinalForgotten.aname)-InStr(1,SummaryJudgment_FinalForgotten.[aname],","))=summary_judgment.first_name));
It might even work correctly without the full qualification of each field now that I'm joining first to first & last to last (since there is no duplication across the tables), but having proven that it does work, I'm done.