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.
Related
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.
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.
Here is my query (jpql-eclipseLInk) -
"SELECT NEW com.nrbwlko.officeprojectmaven1.dao.ProjectProgressDao(c.projectMemberFk.projectMember,"
+ "COUNT(c),"
+ "CASE (c.materialDetailFk.materialType.materialType) WHEN 'plate' "
+ "THEN SUM(c.componentLength*c.componentWidth*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty*c.projectMemberFk.projectUnitIdFk.unitQty)/1000000 "
+ "ELSE SUM(c.componentLength*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty*c.projectMemberFk.projectUnitIdFk.unitQty)/1000 END, "
+ "CASE (c.materialDetailFk.materialType.materialType) WHEN 'plate' "
+ "THEN SUM(c.componentLength*c.componentWidth*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty)/1000000 "
+ "ELSE SUM(c.componentLength*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty)/1000 END, "
+ "CASE (c.materialDetailFk.materialType.materialType) WHEN 'plate' "
+ "THEN SUM(c.componentLength*c.componentWidth*c.materialDetailFk.unitWeight*c.projectMemberFk.fabricatedQty)/1000000 "
+ "ELSE SUM(c.componentLength*c.materialDetailFk.unitWeight*c.projectMemberFk.fabricatedQty)/1000 END, "
+ "CASE (c.materialDetailFk.materialType.materialType) WHEN 'plate' "
+ "THEN SUM(c.componentLength*c.componentWidth*c.materialDetailFk.unitWeight)/1000000 "
+ "ELSE SUM(c.componentLength*c.materialDetailFk.unitWeight)/1000 END) "
+ "FROM CuttingList c "
+ "WHERE c.projectMemberFk.projectUnitIdFk.projectId.projectId = :id "
+ "GROUP BY c.projectMemberFk.projectMember"
First Sum is the Total sum (consisting of groups by members obviously) --- which is not equal to the sum of individual row . When i group by individual row id (c.id) Total is 28193 (which is correct value i checked it manual calculation) But when i group it by member value is 28103.
I tried various combination and unless each row is individual total value is always less than actual value. It's part of my android app backend, all work is done except this data mismatch which makes my last 6 month work useless. I desperately need help.
One more information i would like to share that if i group it by Material (query provided below) then Total value is again correct, for example
Here is another query from same table Giving correct Total Material Weight but this time it grouped by material and not by member
"SELECT NEW com.nrbwlko.officeprojectmaven1.dao.MaterialSummeryByProjectDao(c.materialDetailFk.materialName,COUNT(c),"
+ "CASE (c.materialDetailFk.materialType.materialType) WHEN 'plate' "
+ "THEN SUM(c.componentLength*c.componentWidth*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty*c.projectMemberFk.projectUnitIdFk.unitQty)/1000000 "
+ "ELSE SUM(c.componentLength*c.materialDetailFk.unitWeight*c.projectMemberFk.memberQty*c.projectMemberFk.projectUnitIdFk.unitQty)/1000 END) "
+ "FROM CuttingList c "
+ "WHERE c.projectMemberFk.projectUnitIdFk.projectId.projectId = :id "
+ "GROUP BY c.materialDetailFk.materialName"
giving me material grouped in 4 type and total is 28193 (correct value)
(Count(c) in both case is correct which is 6 nos)
here is the result when i grouped by material -- (Total is correct)
I can provide more information if required.
I'm using HSQLDB and preparedStatements just fine, but if I include either "SELECT TOP" or "ORDER BY" in my SQL statement, when I call updateBoolean (or UpdateInt, etc), I hit an exception:
java.sql.SQLException: attempt to assign to non-updatable column
This sample code works fine:
preparedStatement = connection.prepareUpdatable(
"SELECT " + MyTable.COL_ID + ", " +
MyTable.COL_READ +
" FROM " + MyTable.NAME +
" WHERE " + MyTable.COL_LOCAL +
" =? AND " + MyTable.COL_REMOTE +
" =?",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
preparedStatement.setString(1, localAddress);
preparedStatement.setString(2, remoteAddress);
ResultSet rs = connection.query(preparedStatement);
if (rs.next())
{
rs.updateBoolean(MyTable.COL_READ, isRead);
rs.updateRow();
}
I get the exception if I change "SELECT" to "SELECT TOP". Or if I append this to the SQL statement:
" ORDER BY " + MyTable.COL_RECEIVED_TIMESTAMP + " DESC"
Thanks for any help.
NickB
An updatable SELECT statement cannot have TOP n, LIMIT or ORDER BY. This restriction is imposed by the SQL standard. Your SELECT becomes not-updatable when you add one of those keywords.
It is possible to use a subquery in a WITH clause with the above keywords and the SELECT is updatable.
CREATE TABLE t (a int, b int, PRIMARY KEY(a));
WITH SUBQ(COL) AS (SELECT TOP 1 a FROM t)
SELECT * FROM t WHERE a IN (SELECT * FROM SUBQ)
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.