I´m starting with Hibernate.
I have 2 tables in MySQL DB.
1)Unit
2)type: has as foreign key Unit_IdUnit
I have also in type a column name "Period" VARCHAR
I want to create this query on HQL:
SELECT *
FROM unit
WHERE IdUnit not in(SELECT Unit_IdUnit
FROM type
WHERE Period='2016-09');
If I run this query in Mysql it´s working. But I can´t make it works on HQL.
How could I create this query to return a list of Unit?
This is my first question, so please let me know if you need further information
You can use below HQL:
session.createQuery(
"from unit where IdUnit not in (SELECT Unit_IdUnit
FROM type
WHERE Period='2016-09')"
).list();
This is how I fix it.
Query query= session.createQuery("SELECT e1 FROM Unit e1 "
+ "WHERE e1.idUnit NOT IN ("
+ "SELECT e2.unit "
+ "FROM type e2 "
+ "WHERE e2.period=:period)");
query.setParameter("period", "2016-09");
list= query.list();
Related
I am trying to insert into a table the results which I just selected from this query, But I can't figure it out.
List<Object[]> results = em.createQuery("SELECT s.competition_id, s.data, s.discipline, s.category, s.player_id,"
+ " s.playerFirstName, s.playerLastName, SUM(s.points) as points FROM Score s"
+ " GROUP BY s.competition_id, s.player_id "
+ " ORDER BY s.points DESC", Object[].class).getResultList();
This select is working, but I need to insert this into the result table.
You can use Insert Select statement into regular SQL. You just need to provide the same amount of compatible columns into the query. I think it should be possible with group by records as well.
You may use Insert Selects with my Daobab (http://www.daobab.io)
An example:
https://github.com/daobab-projects/daobab-100plus-examples/blob/main/src/main/java/io/daobab/demo/example/c_talented/InsertSelect.java
I am working on converting a legacy MS access system to a spring-boot application and I came across a big query. I am mostly done with converting the functions from access to mysql but not able to understand how to convert the following DLookUp sub-query as a mySql subquery
DLookUp("[price]","tbl_so","[so_id] = " & tbl_trade.so_id & " AND [product_id] = " & tbl_po.product_id
What I understood is following won't work as I don't have the Inner Joins set between the 3 tables, tbl_so, tbl_po, tbl_trade
SELECT tbl_so.price FROM tbl_so WHERE tbl_so.so_id = tbl_trade.so_id AND tbl_so.product_id = tbl_po.product_id
My question is how do I know how the tables will join with each other in this case and also when this DLookUp query is seldom used. And also the solution for this query.
Well, as a general rule, dlookup() can be replaced with a left join. However, you can also use a sub-query and they tend to be "less" change to the query.
So, if we had this:
SELECT id, partNum, dlookup("PartDescrt","tblParts","PartID = " & partNum)
as Description from tblOrders.
You would replace the above dlookup() with a sub-query like this:
SELECT id, partNum,
(select PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum)
AS Description
from tblOrders
The above is SQL or access sql syntax, but quite sure the same should work for MySQL.
If there is more then one partNumber that matches the above, then add a TOP 1, and a order by with some unique row (PK ID column is best). So the query becomes:
(select TOP 1 PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum
ORDER BY PartDescrt, ID DESC)
AS Description
I'm trying to query list of objects filtered by parameters that might not get entered by the user at all.
#Query(value = "SELECT * " +
"FROM project " +
"WHERE CASE WHEN (:location is not null) THEN location_Id LIKE :location" +
" and CASE WHEN (:category is not null) THEN category_Id LIKE :category", nativeQuery = true)
List<Project> getProjects(#Param("category") List<Category> category,
#Param("location") List<Location> location);
But i keep on getting similar errors that my syntax is wrong even tho i just copied it from a tutorial. Any idea where i might be mistaking?
If you want a single query:
WHERE (:location is null OR location_Id LIKE :location) AND
(:category is null OR category_Id LIKE :category)
From a performance perspective, OR can kill performance (by preventing the use of available indexes). Often applications build the WHERE clause for the non-NULL parameter values.
For starters, I'm a super novice in SQL, and so I don't have the knowledge to ask a straightforward question, but I'll do my best. Here is the query I'm trying to make:
query = conn1.prepareStatement(
"select Industry, Ticker, TransDate, min(TransDate), max(TransDate), " +
" count(distinct TransDate) as TradingDays, " +
" count(distinct Ticker) as TickerCnt, " +
" openPrice, closePrice " +
" from Company left outer join PriceVolume using(Ticker) " +
" group by Industry " +
" having TradingDays >= 150 " +
" order by Industry, Ticker, TransDate");
ResultSet rs = query.executeQuery();
Here's the Error:
SQLException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reedy330.Company.Ticker' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
If I include Ticker and TransDate in the GROUP BY clause, the exception no longer happens, but the ResultSet is empty. What I'm trying to do is have a ResultSet with everything in the SELECT clause as elements. I built this query from a similar example query in the assignment description, which is why I don't understand it well.
Here is the layout of the database, table names bolded with a colon, keys are just in bold:
PriceVolume:
Ticker
TransDate
OpenPrice
HighPrice
LowPrice
ClosePrice
Volume
AdjustedGross
Company:
Ticker
Name
Industry
Location
Any ideas?
Is "sql_mode=only_full_group_by" necessary for you?
If not, change the sql_mode:
SET sql_mode = '';
OR
SET sql_mode = 'STRICT_ALL_TABLES';
When you're using group by, everything in your select clause must EITHER be an aggregate function (like min, max, count etc), OR be in the group by clause.
You need to include Ticker and TransDate in the group by clause.
I'm trying to return records for an alert system based on two conditions.
The first condition is there is a booking in the system for tomorrow's date [Date()+1] with a Type value of B. If that JobNumber also has a Type value (in another record) of A AND the Result field's value is "Not Approved" we need to return an alert.
Example table:
JobNumber Type Company Date Result
58129 B 3 22/03/2013
58129 A 3 20/03/2013 Not Approved
58129 C 3
So far I have been able to create a SQL query in VBA to return the results of the first condition and have looped through the results to return the relevant JobNumbers. How do I insert these JobNumbers as criteria for the second SQL query or is it possible to combine all criteria into one SQL statement?
My SQL so far:
strSQL1 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='B') AND ((I.Date)=Date()+1));"
strSQL2 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='A') AND ((I.Result)<>'approved'));"
Any help would be much appreciated.
You can get a field from the same or another table. This will give an error if more than one row is returned, but whether or not more than one row will be returned depends on your data. If it is likely, you will need to add another criterion, such as date.
SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result,
(SELECT Result
FROM tblInspection q
WHERE q.JobNumber=i.JobNumber
AND Result="Not Approved"
AND Type="A")
As ResultA
FROM tblInspection I
WHERE I.Type='B' AND I.Date=Date()+1