I try to calculate percentage of amout of a specific product per total amout of products. I wrote a SQL query that works fine.
But when i try to write in Query DSL I encountred a problem , i really have a problem to write this kind of query.
SELECT ROUND((c.v2/f.v1)*100,2) , MONTH AS RESULTt
FROM
(SELECT SUM(Quantite)as v1,MONTH FROM Total_sold_view WHERE id_company='379' AND
year=2009 GROUP BY MONTH ORDER BY MONTH ASC) f,
(SELECT Quantite as v2,MONTH FROM Total_sold_view WHERE id_company='379' AND year=2009
AND product_type=13 ORDER BY MONTH ASC) c
WHERE c.MONTH=f.MONTH
Related
I have a rather simple query that works in standard SQL, but doesn't in HQL :
SELECT id
FROM ( SELECT COUNT(*) as rows,
MESSAGES_ID as id
FROM motcles_message mm
WHERE motcle IN :keyWords
GROUP BY MESSAGES_ID) a
WHERE a.rows = :size
Is there any way for me to avoid using a subquery in the FROM statement since HQL doesn't support it ?
I know it can use subqueries in SELECT and WHERE clauses, but I can't find a solution.
SELECT MESSAGES_ID as id
FROM motcles_message mm
WHERE motcle IN :keyWords
GROUP BY MESSAGES_ID
HAVING COUNT(*) = :size
I have a SQL query and I want to know how many rows will that SQL query return. Now the problem is that I want to know the number of results beforehand which means before running the SQL query.
I would have done this easily by ResultSet.getRow() to get the total number of rows from resultset. But as per the requirement, I can get the resultset only after knowing the number of rows to be returned by that query.
I tried the below Java Regex to solve the issue:
String orgQuery = "select * from emp where id<1210 and salary>55000;"
Pattern p= Pattern.compile("(?:)from\\s+(.*)*" , Pattern.CASE_INSENSITIVE);
Matcher m= p.matcher(orgQuery);
if (m.find()) {
countQuery = "SELECT COUNT(*) as total "+ m.group(1);
System.out.println(countQuery);
}
This work perfectly file and I get the "countQuery" as:
SELECT COUNT(*) as total from emp where id<1210 and salary>55000
By this I can easily know the number of rows to be returned beforehand but the problem occurs when my query become more complex like these two:--
even more complex in case of nested queries i.e. #query2.
#query1: select * from emp where id<1210 and salary>55000 order by dept, salary desc;
#query2: select name from emp where id IN (select id from emp where id < 1210 group by salary , id order by id ASC limit 10) order by id DESC limit 10
I think the main issue is with "Order By" clause. I can remove the "Order By" clause too by below regex:
Pattern.compile("(?:)from\\s+(.*)*" , Pattern.CASE_INSENSITIVE);
But it becomes more complex in case of Nested queries.
Can any Java Regex expert help????? I am using postgres as DB.
Wrap your existing query like so:
select count(*) from (<existing query>)
With your given example:
String orgQuery = "select * from emp where id<1210 and salary>55000";
String countQuery = "select count (*) from (" + orgQuery + ')';
I know this works with Oracle. I have not used postgres, so I am not certain if there would be anything preventing this approach from working there.
I will caution on this idea of getting a count first, however, that it might be possible for the data to change between your execution of the count and the actual query.
I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.
Here is Mysql query:
SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM
F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
Frankly, I don't have any idea about how to write above query in HQl.
Can anyone please help me.
Could you try this, I couldn't test, if you it has a error please inform me;
String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()";
query = session.createSQLQuery(hqlQuery);
How would represent the following Oracle SQL in Hibernate HQL.
select table_num
, room_id
, min(event_type) keep(dense_rank first order by changed_on desc)
from room_history
group by table_num, room_id;
The idea behind the query is to order the table "room_history" by "changed_on" datetime column and then group it by "table_num" and "room_id" pairs whilest keeping the first "event_type" for each group. The mentioned query works for Oracle but I have trouble converting it into HQL.
Purpose is to get the latest "event_type" for "table_num" and "room_id" pair.
It seems this is not achievable
I ended up converting the following SQL query instead. It is not perfect but it does the job for my purposes.
select table_num, event_type et from room_history where id in (select max(id) from privacy_history group by msisdn);
I was able to do this assumptions since when for this table always a.id > b.id then also a.changed_on> b.changed_on.
Excuse me for anking again about this issue but I need to have a JPA query for this:
select username, count(*)
from Records
group by username
order by count(*) desc
limit 1
I thought about smth like:
select r.username,count(*) from Records r order by r.username desc
and then to call
getResultList().get(0)
but I am allowed to write only:
select r from Records r order by r.username desc
and in this case I do not know how to get what I need.
Does anyone have any idea?
The SQL query has a group by, and orders by count. The JPA query doesn't have any group by and orders by user name. So I don't see how they could return the same thing.
The equivalent JPQL query is
select r.username, count(r.id)
from Record r
group by r.username
order by count(r.id) desc
If you call setMaxResults(1) on the Query object, the limit clause will be added to the generated SQL query, making it completely equivalent.