I want to translate the following Oracle SQL statement to HQL
select count(*) cnt from v$session where upper(machine) like upper(?)
I found this here in the net: https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/stat/Statistics.html
But I don't get, how I can the exact same query as above
Related
I am using DBeaver to query a PostgreSQL database.
I have this query, it simply selects the highest id per Enterprise_Nbr. The query works but is really slow. Is there any way I can rewrite the query to improve performance.
I am using the querytool DBeaver because I don't have direct access to PostgreSQL. The ultimate goal is to link the PostgreSQL with PowerBi.
select *
from public.address
where "ID" in (select max("ID")
from public.address a
group by "Enterprise_Nbr")
Queries for greatest-n-per-group problems are typically faster if done using Postgres' proprietary distinct on () operator
select distinct on ("Enterprise_Nbr") *
from public.address
order by "Enterprise_Nbr", "ID" desc;
Your query could rewrite as: per each value of Enterprise_Nbr, retrieve row which there is not exists other rows that have same Enterprise_Nbr and greater ID.
SELECT *
FROM public.address a
WHERE NOT EXISTS (
SELECT 1
FROM public.address b
WHERE b.Enterprise_Nbr = a.Enterprise_Nbr AND b.ID > a.ID
)
Is group by 1 a standard SQL?
The SQL like this:
select c_name, count(1) as number from table_one group by 1
This SQL is right in MySQL. But I want to know that can it work in SQL server or Oracle? I do not have SQL server or oracle service, please help me.
Depending on where you put SELECT, FROM, WHERE, etc, I have run into syntax errors. What is the proper order to write queries and code? An example below:
//No error
SELECT count(*)
FROM us_counties_pop_est_2019
WHERE births_2019 - deaths_2019 <=0;
vs
//Syntax error
SELECT count(*)
WHERE births_2019 - deaths_2019 <=0
FROM us_counties_pop_est_2019;
The main clauses of a SELECT statement in PostgreSQL are written in the following order:
[WITH]
SELECT
FROM
JOIN
WHERE
GROUP BY
HAVING
WINDOW
ORDER BY
OFFSET
LIMIT
If the query includes CTEs, then the WITH clause comes before the other ones.
I new developer for linq and sql server.
I know oracle and pl/sql
how I can write this query in linq
Oracle :
select * from (select * from table order by createDate) where rownum<500
var query = db.Table.OrderBy(x => x.createDate).Take(499);
Is there a way to use an ICriteria result as a 'base' for a subsequent criteria query?
For example if I would like to create a query
SELECT department_id, sum(cost) AS total
FROM payment
GROUP BY payment.department_id
storing the result as query0, and then execute the query
SELECT department.name, total
FROM department, query0
JOIN LEFT ON department.id=query0.id
WHERE total > 3
I do not want to have one single huge query executed all at once (which would be the result of creating an ICriteria with subqueries). Note that I have a selection/ restriction on a result of the first query and at the same time including one of its columns in the second query's projection.
The criteria is generated dynamically using strings to identify the classes.
The closest thing I can think of to this is a Common Table Expression (the SQL WITH statement). Unfortunately NHibernate doesn't seem to have any good abstractions for dealing with CTEs, but here is how your query might look in SQL Server:
WITH query0 AS (
SELECT department_id AS id, sum(cost) AS total
FROM payment
GROUP BY payment.department_id
)
SELECT department.name, total
FROM department, query0
WHERE department.id=query0.id
AND total > 3;
SQL Fiddle: http://sqlfiddle.com/#!3/8e6877/7