I am trying to create an Oracle view that will show two columns:
Employee_ID, Department_ID
To get department_id, I need to pass an employee_id into a function which I am doing today with:
select * from TABLE(user.fn_department('dept',employee_id)
This will give me only a list of department_ids for the employee_id I pass into it.
I can get a list of unique employees from the employee table.
How do I combine these two outputs?
Thank you!
You can use something like
CREATE OR REPLACE VIEW USER_DEPT_VIEW AS
SELECT e.EMPLOYEE_ID,
USER.FN_DEPARTMENT('dept', e.EMPLOYEE_ID) AS DEPARTMENT_ID
FROM EMPLOYEE e;
which you could then use as e.g.
SELECT *
FROM USER_dEPT_VIEW
WHERE EMPLOYEE_ID = 1234
Best of luck.
Try this,
SELECT b.employee_id, a.department_id
FROM employees b, table(user.fn_department('dept',b.employee_id)) a
ORDER by b.employee_id
Related
I'm a student and this is my first year of learning Oracle SQL. On the exam, I used this code:
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING INSTR(TO_CHAR(department_id),'5')!=1 AND MAX(salary)<1000;
and the professor said that I should be using something like
SELECT DEPARTMENT_ID, MAX(SALARY)
FROM EMPLOYEES
WHERE SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)<>'5'
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY) < 1000;
We got the same table so my question is when these two can display different results. I'm aware that data processing is different but as he said that was not the problem. The problem is not is using the INSTR function but not using WHERE.
The Where clause workn on the raw contents of the rows
so you filter the dataset that is evaluated for the select clause
WHERE SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)<>'5'
don't select the rows with
SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)='5'
so these rows are not used for
SELECT DEPARTMENT_ID, MAX(SALARY) ..GROUP BY DEPARTMENT_ID
HAVING work on the result of the selected result so also the rows with SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)='5' should be processed
in your case each value for the column DEPARTMENT_ID is always selected because is mentioned in group by and then both the query should return the same result
I'm trying to insert records for couple of columns from a physical table into a temp table with customized IDENTITY. It creates the identity column (field name = idnum), but the values are 0 for all rows. I'm using below code. If anyone can help me what I'm doing wrong would be greatly appreciated.
Note: I'm trying this is Sybase ASE 15.7
SELECT
* INTO #achu_test
FROM (SELECT TOP 10
idnum = IDENTITY(8),
First_Name,
Last_Name
FROM Employees) myTable
My bad! I misplaced the IDENTITY. instead of using it before "* INTO", I used inside the Subquery.
SELECT idnum = IDENTITY(8),* INTO #achu_test
FROM (SELECT TOP 10 First_Name, Last_Name FROM Employees) myTable
A good sleep might have given the result for me :)
I'm trying to get an sqlplus table to give me the workers of each company who make more than the average salary of that company.
(Ex. First National Bank's average salary is $50,000. James and Jane work there and make $80,000, Billy makes $40,000. Fredco's average salary is $30,000. Employee Mark makes $35,000, and Timmy has $25,000 salary. James, Jane, and Mark's names are given as a result.)
However, I've gotten every error under the sun from trying to get it to run correctly. Any help or hints would be very appreciated.
Here's the code for the table's creation. (Ignore the foreign key references: linking to other tables that don't have any of the data needed for this query.)
create table works
(employee_name varchar(20) PRIMARY KEY,
company_name varchar(30) NOT NULL,
salary numeric(9, 2) NOT NULL,
foreign key (employee_name) references employee,
foreign key (company_name) references company);
And here's what I have. As of right now, error ORA-01427 is the response. (single row subquery returns more than one row.)
select employee_name
from works
where salary > (select avg(salary)
from works
group by company_name);
Thank you for any hints or help in advance.
use below query:
select w.employee_name from works w
join (select company_name,avg(salary) salary
from works
group by company_name) q on w.company_name=q.company_name and w.salary > q.salary;
SQLFIDDLE DEMO
How about something like:
select employee_name
from (select employee_name, salary, avg(salary) over (partition by company_name) avg_co_salary
from works)
where salary > avg_co_salary;
It may be faster than doing two passes through the works table, but then again, perhaps not - it all depends on your data, indexes etc. You'd have to test.
I have table called company_emp. In that table I have 6 columns related to employees:
empid
ename
dob
doj, ...
I have another table called bday. In that I have only 2 columns; empid and dob.
I have this query:
select empid, dob
from company_emp
where dob like '01/05/2011'
It shows some list of employees.
In the same way I have queried with table bday it listed some employees.
Now I want to update the company_emp table for employees who have date '01/05/2011'.
I have tried a query like this:
update company_name a
set dob = (select dob from bday b
where b.empid=a.empid
and to_char(a.dob,'dd/mm/yyyy') = '01/05/2011'}
Then all the records in that row becoming null. How can I fix this query?
You're updating every row in the company_name/emp table.
You can fix that with a correlated subquery to make sure the row exists, or more efficiently by placing a primary or unique key on bday.empid and querying:
update (
select c.dob to_dob,
d.dob from_dob
from company_emp c join dob d on (c.empid = d.empid)
where d.dob = date '2011-05-01')
set to_dob = from_dob
Syntax not tested.
I'm new to oracle and having a problem with one of my SQL Queries.
There are 2 Users: User1 and User2:
Tab1 Tab2
-------- --------
EmpNo EmpNo
EmpName EmpName
ContactNo Salary
Location
User2 has all privileges in User1.Tab1, and there is no foreign key relationship between the two tables.
The Problem:
I wanted to add a column in tab2 "NameDesignation" And I wanted to insert the value in this column after checking the following condition:
WHEN User1.Tab1.EmpNo = User2.Tab2.EmpNo THEN
INSERT INTO Tab2 VALUES (&designation)
I really have no idea how to do this, and was hoping for a little help. Any thoughts?
try this:
update user2.tab2.empno t2
set NameDesignation= &designation
where exists (select ''
from user1.tab1 t1
where t1.empno=t2.empno)
(statement updated to match the edited question)
You would need a set of triggers,
After insert or update:
CREATE OR REPLACE TRIGGER tab1_after_changed
AFTER INSERT OR UPDATE
ON tab1
FOR EACH ROW
BEGIN
DELETE FROM User2.Tab2 WHERE EmpNo=:NEW.EmpNo;
INSERT INTO User2.Tab2(EmpNo,EmpName,NameDesignation)
VALUES (:NEW.EmpNo,:NEW.EmpName, (SELECT DesignationName FROM Designation where DesignationID=:NEW.DesignationID));
END;
I just imagined a table with Designation (DesignationID number, DesignationName varchar2(xx)), and Tab1 having DesignationID(number).