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);
Related
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
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
)
I'm using Oracle SQL and i need some help with a query.
In the following query i'm selecting some rows with a simple condition (never mind hat kind of). From the output rows, i need to select the row with minimum value of DATE. For that, i'm using ROWNUM.
SELECT *
FROM(
SELECT NAME, DATE
FROM LIST
WHERE NAME = 'BLABLA'
ORDER by DATE)
WHERE ROWNUM = 1;
However, this query must fit to any other SQL languages, and therefore i need to write this query without ROWNUM.
Is there a simple way to write this query without using ROWNUM?
Unfortunately, row limit syntax differs between RDBMS.
The following is portable between SqlServer, Oracle and PostGres:
SELECT *
FROM (
SELECT NAME, DATE, ROW_NUMBER() OVER (ORDER by DATE) AS RowNum
FROM LIST
WHERE NAME = 'BLABLA'
) X
WHERE RowNum = 1;
However, other DB's syntax is different, e.g. MySql's LIMIT
select * from LIST
where Date=(select min(date) from LIST where Name='BLABLA' )
and Name='BLABLA'
I'm working on a custom DAL and am now dealing with automatic result pagination. The DAL generates dynamic queries based on our models, like:
Model.All() --> generates "select * from mytable"
Model.Filter ("d=#0", "arg0value") --> generates "select * from mytable where d=#0"
...
I don't want to add extra parameters to our filter functions, nor specialized functions for paginating (well, if it's avoidable, of course). Instead, I'd like to paginate the results changing the query before it gets executed. Something like...
var resultset = Model.Filter ("d=#0, "arg0value"); // "select * from mytable where d=#0"
resulset = resultset.Paginate (1, 25); // this should paginate the query before loading any objects
In Oracle it's easy, using the ROWNUM pseudocolumn and wrapping the old query.
select * from (my old sql) where rownum between 20 and 40
In Sql Server (2005) there's nothing like ROWNUM. I've seen there is the ROW_NUMBER function, but it requires knowing the inners of the query, as you need to pass an "OVER (ORDER BY xxxx)" clause.
select row_number() over (order By <?????>) as rownum, dv.* from (my old sql) as dv where rownum between 20 and 40
So, is there a way to perform this in Sql Server without adding specific parameters/functions to our models?
Thanks!
Edit
As #Dems says, I could parse the query and add the ORDER BY based on the output fields, but: first, I don't want the overhead of parsing, and second, if the query is of type "select *", I can't extract the fields.
If you're writing your own data access layer, and you want to apply ROW_NUMBER() only knowing the query's output field names (rather than it's internals)...
SELECT
*
FROM
(
SELECT
ROW_NUMBER() OVER (ORDER BY field1, field2, field3) AS row_id,
*
FROM
(
<your-query>
)
AS unordered_data
)
AS ordered_data
WHERE
row_id BETWEEN x AND y
Is there a way to only show the first N lines of output from an SQL query? Bonus points, if the query stops running once the N lines are outputted.
I am most interested in finding something which works in Oracle.
It would be helpful if you specify what database you are targetting. Different databases have different syntax and techniques to achieve this:
For example in Oracle you can ahieve this by putting condition on RowNum (select ... from ... where ... rownum < 11 -> would result in outputting first 10 records)
In MySQL you can use you can use limit clause.
Microsoft SQL Server => SELECT TOP 10 column FROM table
PostgreSQL and MySQL => SELECT column FROM table LIMIT 10
Oracle => select * from (SELECT column FROM table ) WHERE ROWNUM <= 10 (thanks to stili)
Sybase => SET rowcount 10 SELECT column FROM table
Firebird => SELECT FIRST 10 column FROM table
NOTE: Modern ORM tools such as Hibernate give high level API (Query, Restriction, Condition interfaces) that abstract the logic of top n rows based on the dialect you choose.
For Oracle the suggested and accepted solution is wrong. Try using an order clause, and the results will be unpredictable. The SQL will need to be nested to accomplish this in Oracle.
select name, price
from (
select name, price, row_number() over (order by price) r
from items
)
where r between 1 and 5;
The example above was borrowed from http://www.adp-gmbh.ch/ora/sql/examples/first_rows.html which has a good discussion on this topic.
I know it with MySQL but I don't know if it's standard SQL :
end you Query with 'limit X', X = n. of lines you want to get.
Example :
SELECT NAME FROM EMPLOYEES ORDER BY SALARY DESC LIMIT 10;
For Oracle, you can try this
select /*+ FIRST_ROWS(10) */ * from table;