ORA-00936 : missing expressions for IN clause - oracle

select * from students where s_id IN (:sIds);
public List<Integer> getStudents(#Param("userId") Integer sIds);
where
sIds => List of student ids

You cannot use a single bind variable to hold multiple values and use it as an in-list. Depending on the type of the s_id column you can do something like this (here I suppose :sIds is a comma-separated list of integers):
select *
from students
where s_id in (select regexp_substr(:sIds, '[0-9]+',1,level)
from val
connect by level<regexp_count(:sIds,',')+2);

Another option might be this:
SQL> select * from emp
2 where empno in
3 (select * from table(sys.odcinumberlist(7369, 7499, 7521)));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12-1980 800 20
7499 ALLEN SALESMAN 7698 20-02-1981 1600 300 30
7521 WARD SALESMAN 7698 22-02-1981 1250 500 30
SQL>
where 7369, 7499, 7521 represents values you'd enter into sIds

Related

Row Number In oracle

I want to fetch data based on the last row number.
I want the records highlighted in yellow color. Please guide.
Should be a simple MAX function.
select max(row_number) rn,
account_no
from your_table
group by account_no
order by account_no;
If "row_number" represents the result of analytic function (which isn't clear from what you posted so far), then include ORDER BY clause into the function (I don't know which column you're sorting data on) in descending order so that your "max" actually becomes "min" whose RN = 1 and then it is easy to select it as a final result.
with temp as
(select columnb,
columnc,
row_number() over (partition by accountno order by SOMETHING desc) rn
^^^^^^^^^^^^^^^^^^^^^^^
add this
from some_table
)
select columnb,
columnc
from temp
where rn = 1
As I don't have your tables, here's Scott's EMP:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17.12.80 920 20
7499 ALLEN SALESMAN 7698 20.02.81 1600 300 30
7521 WARD SALESMAN 7698 22.02.81 1250 500 30
7566 JONES MANAGER 7839 02.04.81 2975 20
7654 MARTIN SALESMAN 7698 28.09.81 1250 1400 30
7698 BLAKE MANAGER 7839 01.05.81 2850 30
7782 CLARK MANAGER 7839 09.06.81 2450 10
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7839 KING PRESIDENT 17.11.81 10000 10
7844 TURNER SALESMAN 7698 08.09.81 1500 0 30
7876 ADAMS CLERK 7788 12.01.83 1100 20
7900 JAMES CLERK 7698 03.12.81 950 30
7902 FORD ANALYST 7566 03.12.81 3000 20
7934 MILLER CLERK 7782 23.01.82 1300 10
14 rows selected.
Your code would then be like this; note line #6 which calculates row number as I suggested; you'll use it in the final WHERE clause (line #11), while presenting that "max" row number value you desperately wanted.
SQL> with temp as
2 (select deptno,
3 sal,
4 row_number() over (partition by deptno order by sal) rn,
5 --
6 row_number() over (partition by deptno order by sal desc) rnd
7 from emp
8 )
9 select deptno, sal, rn
10 from temp
11 where rnd = 1
12 /
DEPTNO SAL RN
---------- ---------- ----------
10 10000 3
20 3000 4
30 2850 6
SQL>

If date range is provided return the logs in table within specific range else return all the logs in table where date is in time stamp —-oracle query

If date range is provided return the data in table within specific range else return all the data in table where date is in timestamp —-oracle query
This is how I understood the question.
Based on Scott's EMP table which contains the hiredate column, I'll select values in a certain date range.
SQL> alter session set nls_date_format = 'dd.mm.yyyy'
2 /
Session altered.
SQL> select ename, job, hiredate from emp order by hiredate;
ENAME JOB HIREDATE
---------- --------- ----------
SMITH CLERK 17.12.1980
ALLEN SALESMAN 20.02.1981
WARD SALESMAN 22.02.1981
JONES MANAGER 02.04.1981
BLAKE MANAGER 01.05.1981 --> from here ...
CLARK MANAGER 09.06.1981
TURNER SALESMAN 08.09.1981
MARTIN SALESMAN 28.09.1981
KING PRESIDENT 17.11.1981 --> ... to here
JAMES CLERK 03.12.1981
FORD ANALYST 03.12.1981
MILLER CLERK 23.01.1982
SCOTT ANALYST 09.12.1982
ADAMS CLERK 12.01.1983
14 rows selected.
SQL>
Query uses two parameters: date from (pass 01.05.1981) and date to (pass 17.11.1981). This is a "standard" way to deal with possibility of parameters being NULL. Example will run well in e.g. TOAD or SQL Developer.
select ename, job, hiredate
from emp
where (hiredate >= :par_date_from or :par_date_from is null)
and (hiredate <= :par_date_to or :par_date_to is null)
order by hiredate;
ENAME JOB HIREDATE
---------- --------- ----------
BLAKE MANAGER 01.05.1981
CLARK MANAGER 09.06.1981
TURNER SALESMAN 08.09.1981
MARTIN SALESMAN 28.09.1981
KING PRESIDENT 17.11.1981
If both parameters are NULL, all table rows are returned.

Oracle Conditional where clause

is there any way to write query with following functionality, add where clause as a conditional way,
select e.emp_id, emp.admin_user from employees e
if emp.admin != 'Y'
then
query run with where clause
else
query run without where clause ?
Using a CASE expression in the WHERE clause should do the trick. When you say you don't need the where clause if condition is not met, then all you want is a condition like WHERE 1 = 1, i.e. when condition is not met then return all rows. So, you need to make the not met condition as always TRUE.
For example,
I have an employee table,
SQL> SELECT empno, ename, deptno
2 FROM emp;
EMPNO ENAME DEPTNO
---------- ---------- ----------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected.
SQL>
I want to select the employee details, if department is 20 then use the where clause else return all the employee details, but filter the department which meets the where condition.
SQL> SELECT empno, ename, deptno
2 FROM emp
3 WHERE ename =
4 CASE
5 WHEN deptno = 20
6 THEN 'SCOTT'
7 ELSE ename
8 END
9 /
EMPNO ENAME DEPTNO
---------- ---------- ----------
7499 ALLEN 30
7521 WARD 30
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7900 JAMES 30
7934 MILLER 10
10 rows selected.
SQL>
So, for department 20, the filter is applied by where clause, and I get only the row for ename SCOTT, for others it returns all the rows.
To keep it simple I would go for union clause in this case, so you can have your where clause as complex as you need. I tried to guess your table structure from above comment, let's see this example:
SQL> create table employees (emp_id number, admin_user number, project_id number);
Table created.
SQL> create table project_accessible_to_user (emp_id number, project_id number);
Table created.
Now make simple union all of two queries one with where condition anoother without it
SQL> select * from employees e where e.admin_user!='Y' and project_id in
(select project_id from project_accessible_to_user where emp_id=e.emp_id)
union all
select * from employees e where (e.admin_user is null or
e.admin_user='Y');
UNION ALL is better from performance point of view as UNION because it means that it is not checking for intersect values so if there are any it will return duplicates. However in this case it is filtered already by condition on admin_user, so these duplicates will not occure.

Show the column required of a table using group by function in oracle

I want to display the column c.officeID along with the column "Amount". This is my query:
select c.officeID,max(Sum(p.amount)) as “Amount” from payment p, client c where c.clientid in (select clientid from client) and p.clientID=c.clientID group by c.officeID;
I tried using:
select c.officeID,max(Sum(p.amount)) as “Amount” from payment p, client c where c.clientid in (select clientid from client) and p.clientID=c.clientID group by c.officeID,p.amount;
But I am getting a error saying 'ORA00937-Not a single group-group funtion'. Could anyone please tell me where I am going wrong?
You can't add a max and group by, use this following query to get the result, if you want max then use the second query,
SCOTT#research 17-APR-15> select c.empno,Sum(p.sal) as "Amount"
2 from empp p, emp c
3 where c.empno in (select empno from emp)
4 and p.empno = c.empno
5 group by c.empno
6 ;
EMPNO Amount
---------- ----------
7782 2450
7839 5000
7844 1500
7698 2850
7521 1250
7902 3000
7566 2975
7654 1250
7788 3000
7934 1300
7499 1600
7876 1100
234 800
7900 950
14 rows selected.
select max("Amount") from (
select c.empno,Sum(p.sal) as "Amount"
from empp p, emp c
where c.empno in (select empno from emp)
and p.empno = c.empno
group by c.empno)
MAX("AMOUNT")
-------------
5000

Difference b/w 'HAVING NOT EXISTS' and 'MINUS'

HAVING acts like a where clause and EXISTS checks for the rows that exist for the given row or not. So, when we use HAVING NOT EXISTS it should have the same functionality as MINUS which eliminates the common rows from first table.
But in an example when I substituted HAVING NOT EXISTS for MINUS the result sets are not same. the query is
(
select empno,ename,job,mgr,hiredate,sal,comm,deptno
,
count(*) as cnt
from v
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
( select empno,ename,job,mgr,hiredate,sal,comm,deptno
,
count(*) as cnt
from emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno)
)
union all
(
select empno,ename,job,mgr,hiredate,sal,comm,deptno
,
count(*) as cnt
from emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
( select empno,ename,job,mgr,hiredate,sal,comm,deptno
,
count(*) as cnt
from v
group by empno,ename,job,mgr,hiredate,sal,comm,deptno)
)
V is a view, which is formed like
create or replace view v
as
select * from emp where deptno != 10
union all
select * from emp where ename = 'WARD';
The query is an example to determine whether the tables have same data or not
this is my emp table:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
This is the view V
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
When minus is used the query results in
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CNT
------ ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 2
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 1
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 1
7839 KING PRESIDENT 17-NOV-81 5000 10 1
7934 MILLER CLERK 7782 23-JAN-82 1300 10 1
When HAVING NOT EXISTS is used no rows are resulted from the query
Is there any wrong in my understanding of the terms. Please explain why the result sets are different with a short example.Thanks
This is always going to produce no rows:
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
having not exists (
select empno,ename,job,mgr,hiredate,sal,comm,deptno, count(*) as cnt
from emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
);
There is no correlation between the main select and the data returned by the query in the having clause. Unless emp is empty there will always be something in that clause, so the having not exists has to be false, so all rows in the main query are discarded.
It's nothing to do with your data really; a simplified version does the same:
select * from dual
where not exists (select * from dual);
It's logically impossible for there to be rows in dual, and no rows in dual, at the same time. (OK, if there are no rows in dual you're in big trouble, so maybe this wasn't a great example).
In your case v and emp are related; if emp is empty then v must be too.
The main select in this part finds 11 rows from v. The select inside the having clause finds 14 rows from emp, but it wouldn't matter if it found 1 or 100 rows, as there is no relation between the two sets of rows. The having clause just acts as a filter; having not exists (<query that finds anything>) filters out everything. If you break it down, exists (select ...) is true because there are rows in emp; not exists (select ...) is just the negation of that, so it's false; so you're effectively saying 'give me the rows where true = false'.
The second part of the union finds 14 rows from emp, but the same 'true = false' filter applies, so all rows are still discarded, even though the inner select only finds 11 rows from v. You are not making any connection between the data in v and emp.
But any subquery that always produces rows would have the same effect; because there is no correlation you could be querying a different table which has nothing to do with emp at all, and as long is that subquery returned one or more rows your having clause would filter out everything you expect to see. This behaves the same, for example:
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
having not exists (
select * from dual
);
For the second part of the union you can get some of the data back by adding the correlation:
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
having not exists (
select empno,ename,job,mgr,hiredate,sal,comm,deptno, count(*) as cnt
from emp
where emp.empno = v.empno
and emp.ename = v.ename
and emp.job = v.job
and emp.mgr = v.mgr
and emp.hiredate = v.hiredate
and emp.sal = v.sal
and emp.deptno = v.deptno
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CNT
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 1
7934 MILLER CLERK 7782 23-JAN-82 1300 10 1
7839 KING PRESIDENT 17-NOV-81 5000 10 1
Just using empno might work if it's a primary key or unique, unless you'd really be looking for discrepancies between other fields. But correlating the counts between v and emp is tricky, and possibly not worth the effort. Looking at the count at all seems a bit odd; they only differ for WARD because of the contrived way you've built the view. Using minus here is simpler and possibly quicker.

Resources