select top 1 nhibernate oracle - oracle

i use nhibernate that connected with oracle.
what is wrong with my syntax :
string requeteString = #"select distinct reponse1 from
(select reponse2 from CAMPAGNE reponse2 where reponse2.TEl = :tel and reponse2.EXECUTE.ID = :IdExecute order by reponse2.ReponseLEVEL desc) reponse1
where rownum = 1";
IQuery requete = this.CreateQuery(requeteString);
i got error:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException was caught
HResult=-2146232832
Message=Une exception de type 'Antlr.Runtime.NoViableAltException' a été levée. near line 2, column 37
Source=NHibernate

CreateQuery is for HQL, not SQL. You need to use CreateSQLQuery instead.

Related

How to convert raw sql to laravel fluent query builder

hello every one please i need help how would I go about this select using Laravel's query builder? I am aware I can use a raw sql query but I would like to avoid that. Any help would be appreciated, thanks in advance!
SELECT SUM(COST) FROM (
SELECT COUNT(STOCK_ID)*PRICE COST
FROM STOCK ST INNER JOIN PRODUCT PDT ON PDT.PROD_ID=ST.PROD_ID
WHERE PROD_NAME='PENCIL' LIMIT 6 GROUP BY STOCK_ID) TB
The reason why i avoid the raw sql is because when i try it like this
private function totalprice( $product_id)
{
$selltotal =
DB::raw("SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate
FROM tbl_stock AS st
INNER JOIN tbl_product AS pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id'
LIMIT 6)
GROUP BY stock_id");
return $selltotal;
}
This is the error i get
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate ' at line 1 (SQL: insert into `tbl_sell` (`category_id`, `brand_id`, `product_id`, `buyer_id`, `sell_quantity`, `sell_total_price`, `sell_date`, `added_by`, `entry_time`) values (3, 5, 6, 3, 2, SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate
FROM tbl_stock AS st
INNER JOIN tbl_product AS pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id'
LIMIT 6)
GROUP BY stock_id, 2017-08-11, 0, 2017-08-11 07:34:11))
please i will be very greatfull if i get a solution to my problem. Thanks in advance
hello i succeded in doing it myself. It may help some one someday
private function totalprice( $product_id)
{
$selltotal = DB::table('tbl_stock')
->select(array(DB::raw('count(stock_id)*stock_rate')))
->where('tbl_stock.product_id', $product_id)
->join('tbl_product','tbl_stock.product_id','=', 'tbl_product.product_id')
->groupby('stock_id')
->sum('stock_rate');
return $selltotal;
}

ORA-00905: missing keyword error oracle

Hi when i am trying to execute following oracle query I am getting
[Err] ORA-00905: missing keyword
CREATE VIEW MJNSXJJRW_view AS
SELECT B.oID AS "_oid", B.oTm AS "_otm"
FROM
(SELECT DISTINCT oID, oTm FROM MJNSXJJRW) B
LEFT JOIN MJNSXJJRW AS S0 ON
B.oID = S0.oID AND
S0.idx = 0 AND
S0.kID = "str_val" ;
The most likely issue is MJNSXJJRW AS S0. You use the AS keyword when defining column aliases, not when defining table aliases.
CREATE VIEW MJNSXJJRW_view AS
SELECT B.oID AS "_oid", B.oTm AS "_otm"
FROM
(SELECT DISTINCT oID, oTm FROM MJNSXJJRW) B
LEFT JOIN MJNSXJJRW S0 ON
B.oID = S0.oID AND
S0.idx = 0 AND
S0.kID = "str_val" ;
I'm assuming that "str_val" is a column, not a string literal. If it's the latter you should use single quotes.

how to pass select subquery in where clause using laravel

This is my SQl query
select sum(stock.total_in_stock) as total_in_stock
,stock.name
,stock.inventory_id
from (
select i.store_id
,i.model_id
,i. total_in_stock
,i.id as inventory_id
, m.*
from `inventory` as `i`
left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id`
left join `model` as `m` on `m`.`id` = `ms`.`model_id`
where `i`.`model_id` = m.id
and `m`.`status` = 1
and `ms`.`status` = 1
and `i`.`created_at` = (
select si.created_at
from inventory AS si
where si.model_id = i.model_id
and si.store_id = i.store_id
and si.status=1
order by si.created_at desc limit 1
)
) as stock
group by stock.model_id
In laravel, it is written as this:
$results1 = DB::table('inventory as i')
->select(DB::raw( 'sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* '))
->leftJoin('model_store as ms','ms.store_id','=','i.store_id')
->leftJoin('model as m','m.id','=','ms.model_id')
->where('i.model_id','=', 'm.id')
->where('m.status','=', '1')
->where('ms.status','=', '1')
->where("i.created_at","=",function($query) {
$query->select(DB::raw("si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1)) as stock GROUP BY stock.model_id"));
});
It gives me the following error:-
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 (SQL: select sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* from `inventory` as `i` left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id` left join `model` as `m` on `m`.`id` = `ms`.`model_id` where `i`.`model_id` = m.id and `m`.`status` = 1 and `ms`.`status` = 1 and `i`.`created_at` = (select si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock GROUP BY stock.model_id))
It takes 2 closing brackets at the end and gives the above error. Please help me writing the above SQL query in laravel.
Answering your question that's in the tile: the subquery where needs fix:
->where("i.created_at", function($query) {
$query->from('inventory as si')
->selectRaw('max(si.created_at)')
->whereRaw('si.model_id = i.model_id AND ...');
});
However, in order to create a subquery in the from clause, you need to pass raw query, so your whole code requires a bit of toSql() and setBindings(...), which is cumbersome.
So to get the answer to your problem, better describe the problem itself instead.

oracle with clause error in adodb

I am not able to use the with clause in oracle adodb connection. I have tried both wrapping the qry is a select * from (myquery) and adding a , before my with clause. All create errors.
Here is the query
`WITH TEMPa AS (select _nbr, sum(ending) as Cost from t1 group by _nbr),
TEMPB AS (select _nbr, cost,entity
from t2
where end_tms is null and sale_date is null and entity = 110)
SELECT
TEMPA._nbr
,TEMPA.cost
,TEMPb._nbr
,TEMPB._cost
,tempb.entity
,(tempb._cost - tempa.cost) as Difference
FROM TEMPA, TEMPB
WHERE TEMPA._nbr = TEMPB._nbr(+)
and tempa.cost <> tempb.cost`
Any help would be awesome!

How to write Order by expression in JPQL

PostgreSQL and MySQL offers to write expression into ORDER BY clause in SQL query. It allows to sort items by some column but the special values are on the top. The SQL looks like this one. ( works in Postgres )
select * from article order by id = 4, id desc;
Now I want to write it in the JPQL but it doesn't work. My attempt is:
#NamedQuery(name = "Article.special", query = "SELECT a FROM Article a ORDER BY ( a.id = :id ) DESC, a.id DESC")
This is JPA 1.0 with Hibernate driver. Application server throws this exception on deploy.
ERROR [SessionFactoryImpl] Error in named query: Article.special
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: = near line 1, column 73 [SELECT a FROM cz.cvut.fel.sk.model.department.Article a ORDER BY ( a.id = :id ) DESC, a.id DESC]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
Thanks a lot.
For a named query, (ORDER BY ( a.id = :id ) or ORDER BY (:id )) won't work as DSC/ASC can't be parametrized at run-time.
1) Dynamic way if ordering element varies at runtime.
String query = "SELECT a FROM Article a ORDER BY "+orderElement+" DESC, a.id DESC";
entityManager.createQuery(query).getResultList();
2) Static way in entity bean if ordering element is fixed.
Field level:
#OrderBy("id ASC")
List<Article> articles;
Method level:
#OrderBy("id DESC")
public List<Article> getArticles() {...};

Resources