Oracle Sql Joining from 3 tables - sql

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.

Related

SQL Union not working when one table is empty

Hi I am trying to read data from different tables with a SELECT statement and want to use UNION to get one dataset in the end. It is possible that one table does not (yet) contain data. In this case I can read the data from the first two successfully but once I use UNION to combine it with the data from the empty table the resulting dataset will be empty as well, even though it contained data from the first two datasets before.
My problem is, data is shifted between these tables irregularily so there is no way for me to know in which of the three tables I will find the data and in any case usually I have to combine results from more than one table to get the entire dataset I need (one contains data only 3 months back, the other two contain data for the last and the current year - depending on the period I need the data for it is possible that I have to look in all the three of them to collect all the data I need).
Do you know how I can check out all three tables and read data to collect the entire dataset I need?
I also tested if everything works in case all the three tables contain data, et voila it worked and I retrieved all the data I needed. So the statement itself should be OK, just not applicable to the special case where one table does not contain data.
I am using Python to connect to the database and retrieve the data via an sql statement.
So my sql statement currently looks like this:
sql_stmt = (
"select " + sql_param+" from " + db_table +
" where datum>=" + season_start_sql + " and datum<" + season_end_sql +
" and statnr="+str(statnr) +
" union" +
" select " + sql_param+" from " + db_table +
" where datum=" + season_end_sql + " and stdmin<=" + season_end_time_sql +
" and statnr="+str(statnr) +
" union" +
" select " + sql_param + " from " + db_table + "_" + str(season_start.year) +
" where datum>=" + season_start_sql + " and datum<" + season_end_sql +
" and statnr=" + str(statnr) +
" union" +
" select " + sql_param + " from " + db_table + "_" + str(season_start.year) +
" where datum=" + season_end_sql + " and stdmin<=" + season_end_time_sql +
" and statnr=" + str(statnr) +
" union" +
" select " + sql_param + " from " + db_table + "_" + str(next_year) +
" where datum>=" + season_start_sql + " and datum<" + season_end_sql +
" and statnr=" + str(statnr) +
" union" +
" select " + sql_param + " from " + db_table + "_" + str(next_year) +
" where datum=" + season_end_sql + " and stdmin<=" + season_end_time_sql +
" and statnr=" + str(statnr) +
" order by datumsec")
This statement is simply untrue:
once I use UNION to combine it with the data from the empty table the resulting dataset will be empty as well, even though it contained data from the first two datasets before.
If one of the components of a UNION is empty, then you will still get the results from the other tables.
Note: If one of the tables does not exist -- which is quite different from being empty -- then the query returns an error, but not an empty result.
I will note that in general you should be using UNION ALL instead of UNION, to avoid the overhead of removing duplicate values. And your code is wide open to SQL injection attacks and to unexpected syntax errors. I would question whether your application can be designed so you don't have to use dynamic SQL for this purpose.

Unknow column in ON clause

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!

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).

Does OpenJPA support Where In Select queries?

So I have a query as follows that I am trying to run on DB2 using OpenJPA:
#NamedQuery(name="getStuff",
query = "SELECT mtl.final, mtl.entity " +
"FROM MCT mct, MTL mtl " +
"WHERE ~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " +
"AND mct.cTotPK.Label IN ( SELECT mccp.cCPPK.cPLC " +
"FROM MCCP mccp WHERE mccp.cCPPK.cLC = '#My String#')")
I can get rid of the WHERE IN SELECT statements (bottom two lines) and it will run. So I can't figure out what's wrong with the bottom two lines.
Whenever I run this though I get the DB2 error:
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=FOR;AND OR HAVING GROUP INTERSECT ORDER ) FETCH EXCEPT MINUS UNION, DRIVER=3.61.75
When I looked this error up I found that it is an "illegal use of a keyword" but I really don't know what else I could do. Is there an alternative to the WHERE IN SELECT that I could try out? Thanks for the help.

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.