SQL Query to find designation of employee - sql

I have 2 tables employee and job_role as below. I need to write a SQL query to find designation of each employee by joining this table.
Input Table
1.Employee
e_id e_name Salary Commission
-------------------------------------
1 ABC 1000 10
2 CDE 2000 4
3 GHI 3500 40
4 JKL 5000 3
5 MNO 1200 25
6 XYZ 3000 2
2.Job_role
Designation Sal_min Sal_max Commission_Min Commission_Max
-----------------------------------------------------------------
Associate 1000 2000 0 5
Lead 2001 3000 6 10
Manager 3001 5000 11 50
Problem: To find designation of each employee based on below logic using SQL Query
Logic:
if sal between 1000 and 2000
AND Commission between 0 and 5 then Associate
if sal between 2001 and 3000
OR Commission between 6 and 10 then Lead
if sal between 3001 and 5000
OR Commission between 11 and 50 then Manager
Desired output:
e_id e_name Salary Commision Designation
----------------------------------------------
1 ABC 1000 10 Lead
2 CDE 2000 4 Associate
3 GHI 3500 40 Manager
4 JKL 5000 3 Manager
5 MNO 1200 25 Manager
6 XYZ 3000 2 Lead
My Attempt:
select e_id,e_name,salary,commision,
case when designation='Associate' And commision between Commission_Min and Commission_Min
then 'Associate'
else designation end designation
from employee e left outer join job_role
on salary between sal_min and sal_max;
Issue: How to check AND condition(commission) only for Associate not for other?

Try This
select e.*,j.Designation from
employee e
left outer join
Job_role j
on (j.Designation="Associate" and
(e.Salary between j.Sal_min and j.Sal_max) and (e.Commission between j.Commission_Min and j.Commission_Max))
or(j.Designation not in("Associate") and
((e.Salary between j.Sal_min and j.Sal_max) or (e.Commission between j.Commission_Min and j.Commission_Max)))
order by e.e_id ;

Related

Cumulative over table rows with condition Oracle PL/SQL

I have two tables:
Employees:
employee_id field max_amount
3 a 3000
4 a 3000
1 a 1600
2 a 500
4 b 4000
2 b 4000
3 b 1700
ordered by employee, field, amount desc.
Amounts (pol, premia,field):
pol premia field **assign_to_employee**
11 900 a 3
44 1000 a 3
55 1400 a 4
77 500 a 3
88 1300 a 1
22 800 b 4
33 3900 b 2
66 1300 b 4
Assign Stats Table:
employee_id field max_amount true_amount remain
3 a 3000 2400 600
4 a 3000 1400 1600
1 a 1600 1300 300
2 a 500 0 500
4 b 4000 2100 1900
2 b 4000 3900 100
3 b 1700 0 1700
The output : assign_to_employee field (merged to amounts table).
Algoritem wise : The method is to assign pol's to employees until the premia needs to be added to the cumulative_sum is bigger then the max amount per employee listed in the employees table. You always start with the employess with most max amount until you cannot add any other pols to the employee.
I start with the employees with the grater max_amount per field.
I keep doing this until no pols remains to be assign.
Can you help me solve this?
Thank you.

SQL query to select specific fields, process sum and count from two tables

i have these two tables
ORDERS
id order_id e_id e_name
1 1000 1001 Tom
2 1009 1001 Tom
3 1010 1001 Tom
4 1011 1002 Parker
5 1012 1002 Parker
6 1013 1003 Rohan
Transactions
id order_id amount status
1 1000 100 success
2 1009 80 success
3 1010 100 failed
4 1011 50 success
6 1012 50 success
7 1013 100 failed
i would like to join two tables, select fields, process sum count and filter like this
e_id e_name amount_sum total_counts total_success_amount success_count
1001 Tom 280 3 180 2
1002 Parker 100 2 100 2
1003 Rohan 100 1 0 0
this is what i tried
use card;
SELECT COUNT(orders.order_id) as `total_counts`,
COUNT(CASE WHEN transactions.status = 'success' THEN 1 END) as `success_count`,
SUM(0 + transactions.amount) as `amount_sum`, orders.e_id,
orders.e_name
FROM orders
LEFT JOIN transactions
ON transactions.order_id=orders.order_id
GROUP BY (orders.e_id), (orders.order_id),
(orders.e_name), (transactions.amount), (transactions.status);
i tried many queries also not able to achieve it. Suggest me query to get my operation.

SQL help needed (oracle application express)

Given this information
Among all projects chen work on, list the name of project that has lowest budget.
EMPID NAME SALARY DID
---------------------------
1 kevin 32000 2
2 joan 42000 1
3 brian 37000 3
4 larry 82000 5
5 harry 92000 4
6 peter 45000 2
7 peter 68000 3
8 smith 39000 4
9 chen 71000 1
10 kim 46000 5
11 smith 46000 1
PID EMPID HOURS
-----------------
3 1 30
2 3 40
5 4 30
6 6 60
4 3 70
2 4 45
5 3 90
3 3 100
6 8 30
4 4 30
5 8 30
6 7 30
6 9 40
5 9 50
4 6 45
2 7 30
2 8 30
2 9 30
1 9 30
1 8 30
1 7 30
1 5 30
1 6 30
2 6 30
PID PNAME BUDGET DID
---------------------------------------
1 DB development 8000 2
2 network development 6000 2
3 Web development 5000 3
4 Wireless development 5000 1
5 security system 6000 4
6 system development 7000 1
This is what I've written so far:
select min(budget), pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
Which gives me
MIN(BUDGET) PNAME
--------------------------
8000 DB development
6000 security system
6000 network development
7000 system development
How can I select just the lowest (the minimum budget) from these values? Thanks for any help.
use explicit join syntax,
Here is one way using analytic function row_number, sequence number given based on budget with lowest being the first.
with cte
as
(
select row_number() over ( order by budget asc) as rn, pname, budget
from employee e
join workon w
on e.empid = w.empid
join project p
on p.pid = w.pid and name = 'chen'
)
select pname, budget from cte where rn =1
select budgets.budget, budgets.pname
from
(
select min(budget) as budget, pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
order by budget asc
) budgets
where rownum = 1

Please help me to solve this [duplicate]

This question already has an answer here:
How can I perform this aggregate?
(1 answer)
Closed 9 years ago.
I have crated two table one is cutomer and other one is ord
select * from customers;
Customer table
1 101 jun 23 yyyy 15000
2 102 jas 24 zzzz 10000
3 103 fat 20 kkkk 20000
4 104 jini 40 llll 30000
5 105 michael 30 dddd 25000
6 106 das 25 hhhh 10000
7 107 vijay 26 mmmm 12000
8 108 thanku 31 jjjj 26000
9 109 vishnu 34 gggg 24000
10 110 vas 28 ffff 18000
select * from ord;
This is order table
1 12/11/2013 1:00:00 AM 102 2500
2 202 12/11/2013 4:14:17 AM 102 3000
3 203 12/9/2013 9:18:16 PM 103 2000
4 204 12/8/2013 12:00:00 PM 102 1000
5 205 12/24/2013 107 2000
This is tha union command that I have used
select c.name,c.salary,o.amount
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id;
then the resulting table is
1 jas 10000 1000
2 jas 10000 3000
3 jas 10000 2500
4 fat 20000 2000
5 vijay 12000 2000
I want resulting table like this
1 jas 10000 6500
2 fat 20000 2000
3 vijay 12000 2000
plz help me for solving this.
group by c.name, c.salary with sum(salary) is what you want:
select c.name, c.salary, sum(o.amount )
from CUSTOMERS c
inner join ord o on c.id=o.customer_id
group by c.name, c.salary;
try this if it will work.
select c.name,c.salary,sum(o.amount)
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
group by 1,2;
Thanks.
select c.name,c.salary,SUM(o.amount )
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
GROUP BY c.name,c.salary
I think this will work
Use Left Join or RIGHT JOIN
select c.name,c.salary,o.amount
from CUSTOMERS c
left join ord o
on c.id=o.customer_id;

List the name of employee who workon one project sponsored by his/her division and also work on one project that is not sponsord by his/her division

The code I have for this is
select name
from employee e, workon w
where e.empid = w.empid
and pid in
(select pid
from workon
where did in
(select did
from employee ee
where e.did = ee.did))
group by name
But I know that isn't right, since I also need to find someone who works in a project outside of his division. The problem is that I'm not too sure how to do that.
Tables
Employee
EMPID NAME SALARY DID
1 kevin 32000 2
2 joan 46200 1
3 brian 37000 3
4 larry 82000 5
5 harry 92000 4
6 peter 45000 2
7 peter 68000 3
8 smith 39000 4
9 chen 71000 1
10 kim 46000 5
11 smith 46000 1
Workon
PID EMPID HOURS
3 1 30
2 3 40
5 4 30
6 6 60
4 3 70
2 4 45
5 3 90
3 3 100
6 8 30
4 4 30
5 8 30
6 7 30
6 9 40
5 9 50
4 6 45
2 7 30
2 8 30
2 9 30
1 9 30
1 8 30
1 7 30
1 5 30
1 6 30
2 6 30
Project
PID PNAME BUDGET DID
1 DB development 8000 2
2 network development 6000 2
3 Web development 5000 3
4 Wireless development 5000 1
5 security system 6000 4
6 system development 7000 1
select e.name
from employee e
where
-- Projects in department
exists (
select *
from
workon w
join project p
on w.pid = p.pid
and p.did = e.did
where w.empid = e.empid
)
-- Projects out of department
and exists (
select *
from
workon w
join project p
on w.pid = p.pid
and p.did != e.did
where w.empid = e.empid
)