ORA-00907:missing right parenthesis when taking sample from the data [duplicate] - sql

This question already has answers here:
Select a random sample of results from a query result
(9 answers)
Closed 7 years ago.
I want to take random sample by id (not by data points) from Oracle database. My code is shown below but it failed.
select C.* from original_table C,
(select * from (select id from original_table group by id) as A
ORDER BY RAND() LIMIT 500) as B where C.id = B.id;
error message is ORA-00907:missing right parenthesis, highlighting the
"as"s and the parenthesis "(select * " and "LIMIT 500) ".

Two things:
RAND() is not an Oracle function.
You could use dbms_random package.
LIMIT is not supported in Oracle.
Alternatively, you could use following:
On 12c, you could use the new Top-n row limiting feature.
ROWNUM in pre-12c version..

Related

'limit' clause in Oracle SQL "SQL command not properly ended" [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 5 years ago.
I know that questions related to 'limit' have been asked before here,
and I have already referred to them. My question is somewhat different.
Here's my query:
select id,somecol from sometable where someval=2 order by id desc limit 3
I'm getting an error saying 'SQL command not properly ended'.
How do I resolve this? If you need additional information, feel free to tell me so.
Generally, we use LIMIT in MYSQL database and Rownum in Oracle.
MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
Oracle Syntax:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
References:
https://www.w3schools.com/sql/sql_top.asp
If you are running Oracle 12c, you could use FETCH FIRST n ROWS ONLY:
SELECT id, somecol
FROM sometable
WHERE someval = 2
ORDER BY id DESC
FETCH FIRST 3 ROWS ONLY;

SQL Command error: Incorrect syntax near '4' [duplicate]

This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 5 years ago.
I get a "Incorrect syntax near '4'." while executing this command :
#"SELECT * FROM [Table] OFFSET 4 LIMIT 2;"
what is the probem here?
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause. MySQL
supports the LIMIT clause to select a limited number of records, while
Oracle uses ROWNUM.
SQL Server / MS Access Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
Example
SELECT TOP 2 *
FROM [Table]
There is no LIMIT in SQL Server. You use FETCH:
Without an ORDER BY, the OFFSET is meaningless, so you can just do
SELECT TOP 2 t.*
FROM [Table] t;
If you do have an ORDER BY:
SELECT t.*
FROM [Table] t
ORDER BY ?
OFFSET 4 ROWS FETCH FIRST 2 ROWS ONLY;
The ? is a placeholder for the name of the column you want to sort by.

SQL Select all rows with specific value [duplicate]

This question already has an answer here:
How can I select a value from one row in SQL Server?
(1 answer)
Closed 6 years ago.
Trying to select all rows with the value 33487MO in the column ORMSTRID
This is my current query -
SELECT * from MyTable
Where ORMSTRID = "33487MO"
But it gives me an error that is saying Invalid column name '33487MO'
SELECT * from MyTable
shows me all of the rows and columns from the table, and ORMSTRID is definitely the column name.
You should use single quote with string data
According your example your query should be:
select * from mytable
where ORMSTRID like '33487MO'

Postgres SQL - Column does not exist [duplicate]

This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
Closed 6 years ago.
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate FROM factorderline GROUP BY cohort_email limit 5) cohorts
ON order_email= cohort_email limit 5;
ERROR: column "order_email" does not exist
What is the problem with this query?
The problem is most likely that the definition of the column alias hasn't been parsed at the time the join is evaluated; use the actual column name instead:
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (
SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate
FROM factorderline
GROUP BY cohort_email limit 5
) cohorts ON nmemail = cohort_email
limit 5;
Also, when using limit, you really should use an order by clause.
From the docs:
When using LIMIT, it is important to use an ORDER BY clause that
constrains the result rows into a unique order. Otherwise you will get
an unpredictable subset of the query's rows.
The problem is that output column names can't be used in joins.
From the documentation:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.

Selecting all columns using distinct against one specific column [duplicate]

This question already has answers here:
SELECT DISTINCT on one column
(7 answers)
Closed 8 years ago.
I appreciate that this question has been asked before but I am struggling to find an answer that will even run within Oracle 10g (10.2.0.5.0)
I have a table called BASIC which contains approximately 70 columns. Currently, I return a specified number of rows using the following code (as an example) - the result being the first 20 members who have a MEMBNO after 5000
SELECT * FROM BASIC WHERE MEMBNO>5000 AND ROWNUM <=20 ORDER BY MEMBNO;
Within the 20 rows returned, several of the rows have the same value in the NINO column
I would like to modify my SELECT statement to return the next 20 rows with distinct/unique NINO values
Simply wrapping a DISTINCT around the * gives me an ORA-00936: missing expression error, plus it would not be as precise as I would like.
Can you try the code below:- I have used analytical query concept to fetch only distinct nino values.
select * from
(SELECT b.*,row_number() over (partition by nino order by MEMBNO ) rn
FROM BASIC b WHERE MEMBNO>5000)
where rn =1 AND ROWNUM <=20 ORDER BY MEMBNO;
Let me know in case you encounter any issues.
I think I have found a solution via another source
This shows the rows where there are duplicates...
select * from basic where rowid not in (select min(rowid) from basic group by nino)
This shows the rows with the duplicate rows removed...
select * from basic where rowid in (select min(rowid) from basic group by nino)
Then I can add my row count and membno filters for the final result...
select * from basic where rowid in (select min(rowid) from basic where membno>6615 group by NINO) and rownum <=20 order by membno;