order by and rownum in inner query - sql

I've 2 linked tables A an B 1 to n
For all records of A I'd like to have the record of B matching the following conditions
A.dep=B.dep
There is no record in B having the same year of A
I want the maching record of B with the year beeing the latest year before the year of A
If There is more than one record take the one with the latest updateDate
If There is more than one record take the one with the latest creationDate
If There is more than one record take the one with the latest Id
I tried the Following
Select ...
from A
...
left join B same on A.dep=same.dep and A.year=same.year
left join B last on A.dep=last.dep and A.year>last.year
where ...
and same.id is null
and (last.id is null or
last.id = (select id from B where dep=A.dep and rownum=1 order by year desc, updateDate desc, creationDate desc, id desc))
But I've an error :
00907. 00000 - "missing right parenthesis"
But all the parenthesis are ok.
Looks like the inner select doesn't support the order by
Any idea?

It is ORDER BY in a subquery.
If you run it in SQL*Plus (or some tool that shows error position), you'd get
SQL> select *
2 from dept d
3 where 1 = 1
4 and d.deptno = (select e.deptno from emp e where e.ename = 'KING' order by e.sal);
and d.deptno = (select e.deptno from emp e where e.ename = 'KING' order by e.sal)
*
ERROR at line 4:
ORA-00907: missing right parenthesis
So:
SQL> select *
2 from dept d
3 where 1 = 1
4 and d.deptno = (select e.deptno from emp e where e.ename = 'KING');
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NEW YORK
SQL>

Indeed, inner sub-query does not support order by because it will give a partial resultset which will be manipulated further by the outer query.
Use order by to sort the result based on your requirements once the resultset corresponding to other filter conditions is obtained.
So, in your case, get the query to match your filter conditions first and then apply order by based on the requirements. But, if you do want the inner sub-query results to be ordered first before applying it to outer/main query, use a join. Create a resultset based on the inner sub-query first and join it with an alias to the other/main query results with appropriate join conditions.

Assuming b.id is unique, you can do:
select a.*, b.*
from a left join
b
on b.dep = a.dep and
b.year <= a.year and
b.id = (select max(b2.id) keep (dense_rank first order by b2.year desc, b2.updatedate desc, b2.creationdate desc, b2.id desc)
from b b2
where b2.dep = b.dep
);

Related

showing columns with Oracle SQL

I work with tables that have lots of columns that don't fit on my screen. When I create SQL I am really looking at a few columns worth of information.
What is a better way of moving a column from the right side to the first left most column? That way my results will show the column I am interested in first.
Currently I am using
Select ColumnNumber201, t.*
from TableName t
I have a feeling this will add extra results to my query if I was concerned about counts, because it is a join. I would like avoid joins if possible.
Thanks for your help in advance.
We are using Oracle DB SQL
Well, this is the way to display columns you're interested in - name them. In order to put them to the "front" (i.e. to be the leftmost), do exactly what you are doing.
I don't understand your concern about counts. What "join" are you talking about? There's no join in a statement you posted. This:
Select ColumnNumber201, t.*
from TableName t
selects only from one table.
Even if you join it to another table, it is the join condition and where clause that matters, not number of columns you select (OK, aggregates change things, but that's another story).
For example:
SQL> select count(*) one_column_one_table
2 from (select e.ename
3 from emp e);
ONE_COLUMN_ONE_TABLE
--------------------
14
SQL>
SQL> select count(*) many_columns_one_table
2 from (select e.ename, e.*
3 from emp e);
MANY_COLUMNS_ONE_TABLE
----------------------
14
SQL>
SQL> select count(*) many_columns_two_tables
2 from (select d.dname, e.ename, e.*, d.*
3 from emp e join dept d on e.deptno = d.deptno);
MANY_COLUMNS_TWO_TABLES
-----------------------
14
SQL>
SQL> select count(*) many_columns_two_tables_where
2 from (select d.dname, e.ename, e.*, d.*
3 from emp e join dept d on e.deptno = d.deptno
4 where d.deptno = 20);
MANY_COLUMNS_TWO_TABLES_WHERE
-----------------------------
5
SQL>

Finding max of a column while doing inner join of two tables

I have two tables as follows:
Table A
=====================
student_id test_week
-------- ---------
s1 2018-12-01
s1 2018-12-08
Table B
======================
student_id last_updated remarks
-------- ------------ --------
s1 2018-12-06 Fail
s1 2018-12-10 Pass
Above two tables, I want to fetch following columns:
student_id, last(test_week) and remarks such that
last_updated>=test_week -1 and last_updated<=test_week-15,
i.e. last_updated should be within two weeks of last(test_week), so following will be the result for above entries:
s1 2018-12-08 Pass
I have written like following:
select a.student_id, test_week, remarks
from A inner join B
on A.student_id = B.student_id
and DATEDIFF(last_updated, test_week)>=1
and DATEDIFF(last_updated, test_week)<=15;
But how I will handle the last(test_week), that I am not getting.
If you need the only record related to the last test_week then you can do the following. If I understood this right.
select top 1 a.student_id, test_week, remarks
from A inner join B
on A.student_id = B.student_id
and DATEDIFF(last_updated, test_week)>=1
and DATEDIFF(last_updated, test_week)<=15
order by last_week desc;
You can try to use window function row_number(). The following query will give the max(test_week) for every student_id.
select * from (
select id, test_week, remarks, row_number()
over (partition by id order by test_week desc) as rn
from (
select a.id, test_week, remarks from A join B on A.id = B.id and last_updated - test_week >=1 and last_updated - test_week <=15)tb1
)tb2 where rn=1;
Note : The above query is supported in postgresql, you might want to convert it into equivalent Mysql query

Limit the data set of a single table within a multi-table sql select statement

I'm working in an Oracle environment.
In a 1:M table relationship I want to write a query that will bring me each row from the "1" table and only 1 matching row from the "many" table.
To give a made up example... ( * = Primary Key/Foreign Key )
EMPLOYEE
*emp_id
name
department
PHONE_NUMBER
*emp_id
num
There are many phone numbers for one employee.
Let's say I wanted to return all employees and only one of their phone numbers. (Please forgive the far-fetched example. I'm trying to simulate a workplace scenario)
I tried to run:
SELECT emp.*, phone.num
FROM EMPLOYEE emp
JOIN PHONE_NUMBER phone
ON emp.emp_id = phone.emp_id
WHERE phone.ROWNUM <= 1;
It turns out (and it makes sense to me now) that ROWNUM only exists within the context of the results returned from the entire query. There is not a "ROWNUM" for each table's data set.
I also tried:
SELECT emp.*, phone.num
FROM EMPLOYEE emp
JOIN PHONE_NUMBER phone
ON emp.emp_id = phone.emp_id
WHERE phone.num = (SELECT MAX(num)
FROM PHONE_NUMBER);
That one just returned me one row total. I wanted the inner SELECT to run once for each row in EMPLOYEE.
I'm not sure how else to think about this. I basically want my result set to be the number of rows in the EMPLOYEE table and for each row the first matching row in the PHONE_NUMBER table.
Obviously there are all sorts of ways to do this with procedures and scripts and such but I feel like there is a single-query solution in there somewhere...
Any ideas?
I'd use a rank (or dense_rank or row_number depending on how you want to handle ties)
SELECT *
FROM (SELECT emp.*,
phone.num,
rank() over (partition by emp.emp_id
order by phone.num) rnk
FROM EMPLOYEE emp
JOIN PHONE_NUMBER phone
ON emp.emp_id = phone.emp_id)
WHERE rnk = 1
will rank the rows in phone for each emp_id by num and return the top row. If there could be two rows for the same emp_id with the same num, rank would assign both a rnk of 1 so you'd get duplicate rows. You could add additional conditions to the order by to break the tie. Or you could use row_number rather than rank to arbitrarily break the tie.
All above answers will work beautifully with the scenario you described.
But if you have some employees which are missing in phone tables, then you need to do a left outer join like below. (I faced similar scenario where I needed isolated parents also)
EMP
---------
emp_id Name
---------
1 AA
2 BB
3 CC
PHONE
----------
emp_id no
1 7555
1 7777
2 5555
select emp.emp_id,ph.no from emp left outer join
(
select emp_id,no,
ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY emp_id) as rnum
FROM phone) ph
on emp.emp_id = ph.emp_id
where ph.rnum = 1 or ph.rnum is null
Result
EMP_ID NO
1 7555
2 5555
3 (null)
If you want only one phone number, then use row_number():
SELECT e.*, p.num
FROM EMPLOYEE emp JOIN
(SELECT p.*,
ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY emp_id) as seqnum
FROM PHONE_NUMBER p
) p
ON e.emp_id = p.emp_id and seqnum = 1;
Alternatively, you can use aggregation, to get the minimum or maximum value.
This is my solution. Simple but maybe wont scale well for lot of columns.
Sql Fiddle Demo
select e.emp_id, e.name, e.dep, min(p.phone_num)
from
EMPLOYEE e inner join
PHONE_NUMBER p on e.emp_id = p.emp_id
group by e.emp_id, e.name, e.dep
order by e.emp_id;
And this fix the query you try
Sql Fiddle 2
SELECT emp.*, phone.num
FROM EMPLOYEE emp
JOIN PHONE_NUMBER phone
ON emp.emp_id = phone.emp_id
WHERE phone.num = (SELECT MAX(num)
FROM PHONE_NUMBER p
WHERE p.emp_id = emp.emp_id );

how can I select the other departments from this query

So I have the following query
SELECT deptno, COUNT(deptno) number_of_jobs
FROM emp
WHERE job = 'SALESMAN'
GROUP BY deptno
ORDER BY number_of_jobs ASC;
What this does is to return me just department 30, while I want departments 10 and 20 aswell.
Like this
deptno number_of_jobs
10 (here it can be null or 0 doesn't really matter)
20 (same like dept 10)
30 4
I know this is fairly easy and I believe it's done using a join condition, but I just can't get my head around it.
Thanks guys!
You need to LEFT JOIN this with your department table. A LEFT JOIN returns all the rows from the left table, even if they don't have matches in the right table.
SELECT d.deptno, COUNT(e.deptno) number_of_jobs
FROM dept d
LEFT JOIN emp e ON e.deptno = d.deptno AND e.job = 'SALESMAN'
GROUP BY d.deptno
ORDER BY number_of_jobs ASC
Note that you have to use COUNT(e.deptno) rather than COUNT(*), otherwise it will count the row with null values from the emp table. When you give a column name to COUNT, it only counts the non-null values of that column, so you'll get 0 for the rows with no match.

Representing 'not in' subquery as join

I am trying to convert the following query:
select *
from employees
where emp_id not in (select distinct emp_id from managers);
into a form where I represent the subquery as a join. I tried doing:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id!=b.emp_id;
I also tried:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id not in b.emp_id;
But it does not give the same result. I have tried the 'INNER JOIN' syntax as well, but to no avail. I have become frustrated with this seemingly simple problem. Any help would be appreciated.
Assume employee Data set of
Emp_ID
1
2
3
4
5
6
7
Assume Manger data set of
Emp_ID
1
2
3
4
5
8
9
select *
from employees
where emp_id not in (select distinct emp_id from managers);
The above isn't joining tables so no Cartesian product is generated... you just have 7 records you're looking at...
The above would result in 6 and 7 Why? only 6 and 7 from Employee Data isn't in the managers table. 8,9 in managers is ignored as you're only returning data from employee.
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id!=b.emp_id;
The above didnt' work because a Cartesian product is generated... All of Employee to all of Manager (assuming 7 records in each table 7*7=49)
so instead of just evaluating the employee data like you were in the first query. Now you also evaluate all managers to all employees
so Select * results in
1,1
1,2
1,3
1,4
1,5
1,8
1,9
2,1
2,2...
Less the where clause matches...
so 7*7-7 or 42. and while this may be the answer to the life universe and everything in it, it's not what you wanted.
I also tried:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id not in b.emp_id;
Again a Cartesian... All of Employee to ALL OF Managers
So this is why a left join works
SELECT e.*
FROM employees e
LEFT OUTER JOIN managers m
on e.emp_id = m.emp_id
WHERE m.emp_id is null
This says join on ID first... so don't generate a Cartesian but actually join on a value to limit the results. but since it's a LEFT join return EVERYTHING from the LEFT table (employee) and only those that match from manager.
so in our example would be returned as e.emp_Di = m.Emp_ID
1,1
2,2
3,3
4,4
5,5
6,NULL
7,NULL
now the where clause so
6,Null
7,NULL are retained...
older ansii SQL standards for left joins would have been *= in the where clause...
select *
from employees a, managers b
where a.emp_id *= b.emp_id --I never remember if the * is the LEFT so it may be =*
and b.emp_ID is null;
But I find this notation harder to read as the join can get mixed in with the other limiting criteria...
Try this:
select e.*
from employees e
left join managers m on e.emp_id = m.emp_id
where m.emp_id is null
This will join the two tables. Then we discard all rows where we found a matching manager and are left with employees who aren't managers.
Your best bet would probably be a left join:
select
e.*
from employees e
left join managers m on e.emp_id = m.emp_id
where
m.emp_id is null;
The idea here is you're saying that you want to select everything from employees, including anything that matches in the manager table based on emp_id and then filtering out the rows that actually have something in the manager table.
Use Left Outer Join instead
select e.*
from employees e
left outer join managers m
on e.emp_id = m.emp_id
where m.emp_id is null
left outer join will preserve the rows from m table even if they do not have a match i e table based on the emp_id field. The we filter on where m.emp_id is null - give me all the rows from e where there's no matching record in m table.
A bit more on the subject can be found here:
Visual representation of joins
from employees a, (select distinct emp_id from managers) b implies cross join - all posible combinations between tables (and you needed left outer join instead)
The MINUS keyword should do the trick:
SELECT e.* FROM employees e
MINUS
Select m.* FROM managers m
Hope that helps...
select *
from employees
where Not (emp_id in (select distinct emp_id from managers));