Hql Querys for sql query - sql

Can you give exact hql Query for this Sql query?
SELECT * FROM `ims_product`WHERE
quantity <= reorder_quantity AND
reorder_quantity IS NOT NULL AND
NOT(reorder_quantity=0);

Try using.
FROM `ims_product` WHERE `quantity` <= `reorder_quantity` AND `reorder_quantity` != 0;

Related

Convert SQL query into activerecord

I want to convert this SQL query into activerecord codeigniter, is there anyone can help?
SELECT t.idkas,t.tglkas, (
SELECT (SUM(kredit))-(SUM(debet))
FROM kaskecil t2
WHERE t2.idkas <= t.idkas
) AS total
FROM kaskecil t
$this->db->select("t.idkas, t.tglkas, ((SELECT SUM(kredit) - SUM(debet) FROM kaskecil t2 WHERE t2.idkas <= t.idkas) AS total)", FALSE);
$this->db->from("kaskecil t");
$query = $this->db->get();

How can i change this query from correlated to uncorrelated

actual code
select adjust.id, adjust.orginal_ticket_id, adjust.trans_history_id , adjust.ticket_id
from trans_history_shared adjust
where cdr_type=201
and adjust.event_timestamp <= v_end_time_summary
and adjust.create_date >= v_start_time_summary
and adjust.create_date < v_end_time
and exists (
select origin.ticket_id
from trans_history_shared origin
where origin.ticket_id=adjust.orginal_ticket_id
and origin.event_timestamp <= v_end_time_summary
and origin.create_date >= v_start_time_summary
and origin.create_date < v_end_time
and (origin.revenue_date is null or origin.revenue_date = v_start_time_summary)
)
and ticket_id = (
select latest.ticket_id
from trans_history_shared latest
where latest.orginal_ticket_id = adjust.orginal_ticket_id
and cdr_type=201
and latest.event_timestamp <= v_end_time_summary
and latest.create_date >= v_start_time_summary
and latest.create_date < v_end_time
and (latest.revenue_date is null or latest.revenue_date=v_start_time_summary)
order by latest.create_date desc, latest.event_timestamp desc
fetch first 1 rows only);
Hi all, I'm trying to implement this query inside spark sql, but it's giving me an error saying correlated scalar subqueries must be aggregated. Could you guys perhaps help me how to fix this? Thank you for reading and have a nice day!

Select Rows with id having even number

I am passing a simple query where I am searching for specific rows where OrderID is an even number
SELECT *
FROM Orders
WHERE mod(OrderID,2) = 0;
Error :
Syntax error (missing operator) in query expression 'mod(OrderID,2) = 0'.
You are not using Oracle, so you should be using the modulus operator:
SELECT * FROM Orders where OrderID % 2 = 0;
The MOD() function exists in Oracle, which is the source of your confusion.
Have a look at this SO question which discusses your problem.
SELECT * FROM Orders where OrderID % 2 = 0;///this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0;///this is for odd numbers
Sql Server we can use %
select * from orders where ID % 2 = 0;
This can be used in both Mysql and oracle.
It is more affection to use mod function that %.
select * from orders where mod(ID,2) = 0
MOD() function exists in both Oracle and MySQL, but not in SQL Server.
In SQL Server, try this:
SELECT * FROM Orders where OrderID % 2 = 0;
--This is for oracle
SELECT DISTINCT City FROM Station WHERE MOD(Id,2) = 0 ORDER BY City;
Try this:
SELECT DISTINCT city FROM STATION WHERE ID%2=0 ORDER BY CITY;
For SQL Server:
SELECT * FROM Orders where OrderID % 2 = 0; //this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0; //this is for odd numbers
For Oracle and MySQL, you have to use the MOD function:
select * from orders where mod(ID,2) = 0
SELECT * FROM ( SELECT *, Row_Number()
OVER(ORDER BY country_gid) AS sdfg FROM eka_mst_tcountry ) t
WHERE t.country_gid % 2 = 0
In SQL, all these options can be used for MOD:
SELECT * FROM CITY WHERE MOD(ID,2) = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID % 2 = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID MOD 2 = 0 ORDER BY CITY;
Hope it helps!

SQL. How should i write NOT LIKE statement

SELECT *
FROM [Table]
WHERE (Izdava NOT LIKE NULL)
// how to check if Izdava is not NULL
This should work:
SELECT * FROM [Table] WHERE Izdava IS NOT NULL
SELECT * FROM [Table] WHERE (Izdava IS NOT NULL)
LIKE NULL is meaningless. It doesn't make sense. LIKE is used for comparing a partial string using wildcards, or a complete string without wildcards.
Depending on the RDBMS you want NOT IS NULL,
SELECT * FROM [Table] WHERE (NOT Izdava IS NULL)
I think this is what you want.
SELECT *
FROM [Table]
WHERE Izdara Is Not NULL
Information about NULL values from MSDN:
Following is information about nulls:
To test for null values in a query, use IS NULL or IS NOT NULL in the
WHERE clause.
If you are checking for not null, dont use the like clause. Just write
select * from tablename where columnmame is not null
If it's MSSQL or Oracle then do this:
SELECT *
FROM [Table]
WHERE Izdava IS NOT NULL

Filtering a sql count(*) with SQLAlchemy

How can I do the following SQL query using SQLAlchemy?
SELECT COUNT (*)
FROM det_factura
WHERE company = 31
AND date::text LIKE '2011-05% ';
If det_factura is also a mapped object, then this query should do it:
qry = (session.query(func.count(det_factura.id))
.filter(det_factura.company==31)
.filter(det_factura.date.like('2010-05%'))
)
If it is a table instance, the one below should work:
qry = select([func.count(det_factura.id)],
and_(det_factura.company==31,
det_factura.date.like('2010-05%')
)
)