Oracle aggregate functions on strings - oracle

I have an employee with multiple managers. The manager name field has (firstname,lastname) and the email field has(last.first#email.com).There is no Mgr id.
So, when I try to group this by employee id to get the max of Mgr name and email, some times I end up getting the wrong name/email id combination.
ex:
person Mgr_name Mgr_email
------- --------- ----------
111 brad,pitt pitt.brad#test.com
111 mike,clark clark.mike#test.com
when I group it by person and get the max(mgr_name),mgr_email, I get
person max(Mgr_name) max(Mgr_email)
------- --------- ----------
111 mike,clark pitt.brad#test.com
How do I get the correct email/name combination?

Use row_number analytical function instead:
with t(person ,Mgr_name , Mgr_email) as (
select 111 ,'brad,pitt' , 'pitt.brad#test.com' from dual union all
select 111 ,'mike,clark' , 'clark.mike#test.com' from dual )
select person ,Mgr_name , Mgr_email from (
select t1.*, row_number() over (order by mgr_name) num from t t1)
where num = 1
This get max mgr_name with correct email.
Output:
PERSON MGR_NAME MGR_EMAIL
---------- ---------- -------------------
111 brad,pitt pitt.brad#test.com

You could use a subselect to obtain the max mgr_name for each person in the table then join it back to the base results to limit to only display each persons "Max" manager...
SELECT t1.Person, t1.Mgr_name, t1.mgr_email
FROM tableName t1
INNER JOIN (Select max(mgr_name) mname, Person from TableName group by person) t2
on t1.mgr_name = t2.mname
and t2.Person = T1.Person

Related

I want to delete duplication with condition from a table in PLSQL

I want to delete the dup lines using PLSQL. The sample of the table is below
Policy #
Price
Dealno for Loan #
Price of Loan
PersonID
123
10
Loan123
1,000
abc
123
10
Loan123
3,000
abc
456
10
Loan456
500
xyz
456
10
Loan456
500
null
As you can see, in the case of Policy #123, I try to get the line with the highest amount of Price of Loan. Which mean the Price of Loan for 3,000.
For Policy #456, I want to get the one without null value.
Is there a way for me to achieve that in PLSQL.
Thank you
This query identifies if a row is OK (rn = 1) or if is is a duplicated copy (rn > 1) based on your definition
select POLICY#, PRICE, LOAN#, PRICE_LOAN, PERSON_ID,
row_number() over (partition by POLICY# order by PRICE_LOAN desc, PERSON_ID nulls last) as rn
from tab
;
POLICY# PRICE LOAN# PRICE_LOAN PER RN
---------- ---------- -------- ---------- --- ----------
123 10 loan123 3000 abc 1
123 10 loan123 1000 abc 2
456 10 loan4563 500 xyz 1
456 10 loan4563 500 2
Note that you use row_number where you partition by on the unique key and order by so that you get first the row that should be taken.
So to get the duplicates only you use this query
with rn as (
select POLICY#, PRICE, LOAN#, PRICE_LOAN, PERSON_ID,
row_number() over (partition by POLICY# order by PRICE_LOAN desc, PERSON_ID nulls last) as rn
from tab
)
select * from rn where rn > 1;
POLICY# PRICE LOAN# PRICE_LOAN PER RN
---------- ---------- -------- ---------- --- ----------
123 10 loan123 1000 abc 2
456 10 loan4563 500 2
Based on this you write the DELETE statement (enclose in BEGIN ... END if you insist in PL/SQL)
delete from tab where rowid in
(
with rn as (
select POLICY#, PRICE, LOAN#, PRICE_LOAN, PERSON_ID,
row_number() over (partition by POLICY# order by PRICE_LOAN desc, PERSON_ID nulls last) as rn
from tab
)
select rowid from rn where rn > 1
);
You may check if the delete worked fine ....
select * from tab;
POLICY# PRICE LOAN# PRICE_LOAN PER
---------- ---------- -------- ---------- ---
123 10 loan123 3000 abc
456 10 loan4563 500 xyz
... and commit

oracle procedure that update values based on amount

I have the following table (SEMINARS):
ID AMOUNT
6936120606 320.51
6978332460 261.05
6940456367 0
6973328053 438.68
6951690568 533.56
sum(1690.47)
I have the value 980 that I need to credit one by one the seminars amount
start from biggest amount to lowest.
for example the 980 value must will credit as following:
6951690568 533,56 (-533.56)=0 446.44 (980.00-533.56=446.44)
6973328053 438,68 (-438.68)=0 7.76 (446.44-438.68= 7.76)
6936120606 320,51 (- 7.76)=312.75 0.00 (0)
any idea?
In Oracle 11g you can use recursive query, this gave me desired output:
with
s as (select row_number() over (order by amount desc) rn, id, amount from seminars),
t(rn, id, amount, left1, left2) as (
select rn, id, amount, greatest(amount-980,0), 980-amount from s where rn = 1
union all
select s.rn, s.id, s.amount,
s.amount-least(left2, s.amount), greatest(left2-s.amount, 0)
from s join t on s.rn = t.rn+1 and left2>0)
select * from t
Output and SQLFiddle:
RN ID AMOUNT LEFT1 LEFT2
---------- --------------- ---------- ---------- ----------
1 6951690568 533.56 0 446.44
2 6973328053 438.68 0 7.76
3 6936120606 320.51 312.75 0

Getting multiple column values in single row

This is my actual result and i have used for store details of client
SID ATTRIBUTE_VALUES ATTRIBUTE_ID
---------- ---------------- -----------
20 101010 MEMBERSHIP_NO
20 ALLEN MEMBERSHIP_NAME
20 WARD MEMBERSHIP_LNAME
30 101011 MEMBERSHIP_NO
30 MARTIN MEMBERSHIP_NAME
30 BLAKE MEMBERSHIP_LNAME
In this I need to retrieve the details based on membership no and also using sid. sid is unique number .
I need to show the result as
SID MEMBERSHIP_NO MEMBERSHIP_NAME MEMBERSHIP_LNAME
----- ------------- --------------- ----------------
20 101010 ALLEN WARD
30 101011 MARTIN BLAKE
Above 3 attributes are constant using that values only client number, names are inserts into table. Im stuck with this...I need to solution for it.
This is the query i have used for retrieve the details. But it returns empty
SELECT sid, LISTAGG(attribute_value, ',') WITHIN GROUP
(ORDER BY attribute_value) AS att FROM customer_attributes WHERE
sid ='20' AND attribute_value='101010'
AND attribute_id ='MEMBERSHIP_NO'
AND attribute_id ='MEMBERSHIP_NAME'
AND attribute_id ='MEMBERSHIP_LNAME'
GROUP BY SID;
You can do this by join. LISTAGG wont create 3 columns:
select
a.sid,
a.ATTRIBUTE_VALUES MEMBERSHIP_NO,
b.ATTRIBUTE_VALUES MEMBERSHIP_NAME,
c.ATTRIBUTE_VALUES MEMBERSHIP_LNAME
from tbl a
join tbl b on a.sid = b.sid
join tbl c on c.sid = b.sid
where a.ATTRIBUTE_ID = 'MEMBERSHIP_NO'
and b.ATTRIBUTE_ID = 'MEMBERSHIP_NAME'
and c.ATTRIBUTE_ID = 'MEMBERSHIP_LNAME'
Demo sqlfiddle
This also gives the same result;
select
sid,
Max(DECODE(ATTRIBUTE_ID, 'MEMBERSHIP_NO', ATTRIBUTE_VALUES, '')) MEMBERSHIP_NO,
Max(DECODE(ATTRIBUTE_ID, 'MEMBERSHIP_NAME', ATTRIBUTE_VALUES, '')) MEMBERSHIP_NAME,
Max(DECODE(ATTRIBUTE_ID, 'MEMBERSHIP_LNAME', ATTRIBUTE_VALUES, '')) MEMBERSHIP_LNAME
from tbl
group by sid
order by sid
Same can be done by Pivot also
select *
from tbl
pivot(
max(ATTRIBUTE_VALUES) for ATTRIBUTE_ID in (
'MEMBERSHIP_NO' MEMBERSHIP_NO,
'MEMBERSHIP_NAME' MEMBERSHIP_NAME,
'MEMBERSHIP_LNAME' MEMBERSHIP_LNAME
)
)
order by sid

display manager name and count of employees reporting him in employees table

I want to display manager_name and count of employees reporting him in employees table.I want to sort the data based on count IE maximum employees reporting to a manager should come first.
I tried to write self join but i could not get the out put .
EMPLOYEE_ID FIRST_NAME MANAGER_ID SALARY HIRE_DATE
198 Donald 124 2600 21-JUN-99
199 Douglas 124 2600 13-JAN-00
200 Jennifer 101 4400 17-SEP-87
201 Michael 100 13000 17-FEB-96
202 Pat 201 6000 17-AUG-97
203 Susan 101 6500 07-JUN-94
204 Hermann 101 10000 07-JUN-94
205 Shelley 101 12000 07-JUN-94
206 William 205 8300 07-JUN-94
100 Steven 24000 17-JUN-87
101 Neena 100 17000 21-SEP-89
the table name is employees and i want to see names also
You can use the aggregate function COUNT and ORDER BY clause
You didn't mention the table name assuming the table name as EMPLOYEES, below query would help you.
SELECT MANAGER_ID, COUNT(EMPLOYEE_ID) as EMP_COUNT
FROM EMPLOYEES
GROUP BY MANAGER_ID
ORDER BY EMP_COUNT DESC;
Here EMP_COUNT is the column alias name.If you don't want any column alias you can simply use the query below.
SELECT MANAGER_ID, COUNT(EMPLOYEE_ID)
FROM EMPLOYEES
GROUP BY MANAGER_ID
ORDER BY COUNT(EMPLOYEE_ID) DESC;
If you want to sort by ascending order instead of DESC you can use ASC.
We can get this output using an analytical function:
SELECT E.EMPID,E.EMPNAME as "Manager Name",M.EMPNAME AS "Employee Name",count(*) over(partition by e.empid) reportee_count
from empmgid m,empmgid e where M.MAGID=e.EMPID order by reportee_count desc;
Please employ the following SQL-Query:
SELECT
e.empno,
e.ename,
e1.empcnt
FROM
emp e,
(
SELECT
mgr,
COUNT(*) empcnt
FROM
emp
GROUP BY
mgr
) e1
WHERE
e.empno = e1.mgr;
-- Restricting which manager is having two employees working under them
-----------------------------------------------------------------------
SELECT E1.* FROM
(
SELECT E1.EMPNO,E1.ENAME AS EMPLOYE,
M1.ENAME AS MANAGERS,
COUNT(*)
OVER
(
PARTITION BY E1.EMPNO
) EMPCNT
FROM EMP E1,EMP M1
WHERE M1.MGR=E1.EMPNO
) E1
WHERE EMPCNT = 2;
select count(distinct manager_id) from employees;
select count(distinct manager_id) from employees;
Ans:
COUNT(DISTINCTMANAGER_ID)
18

Table Variable Style Entities in Oracle

I've been looking around for awhile for something in Oracle that acts like a table variable in SQL Server. I've found people asking questions like this here on SO and people always say "Yes, Oracle has that" but the examples show that the entities are not like SQL Server at all. Can someone show me how to perform the below simple TSQL solution in Oracle?
declare #users table (
ID int,
Name varchar(50),
Age int,
Gender char(1)
)
;with users as (
select 1001 as ID, 'Bob' as Name, 25 as Age, 'M' as Gender
union
select 1021 as ID, 'Sam' as Name, 29 as Age, 'F'
)
insert into #users (ID, Name, Age, Gender)
select * from users
declare #grades table (
UserID int,
ClassID int,
Grade int
)
;with grades as (
select 1001 as UserID , 120 as ClassID, 4 as Grade
Union
select 1001 as UserID , 220 as ClassID, 2 as Grade
Union
select 1021 as UserID , 130 as ClassID, 4 as Grade
Union
select 1021 as UserID , 230 as ClassID, 4 as Grade
Union
select 1021 as UserID , 340 as ClassID, 2 as Grade
)
insert into #grades
select * from grades
select u.ID, u.Name, GPA = AVG(cast(g.grade as decimal))
from #users u
inner join #grades g on u.ID=g.UserID
group by u.ID, u.Name
Some answers may tell you that Oracle has table variables, and it does to a certain extent. However, most answers will tell you that you should not be doing this in Oracle at all; there's simply no need.
In your case I would simply use a CTE:
with users as (
select 1001 as ID, 'Bob' as Name, 25 as Age, 'M' as Gender from dual
union
select 1021 as ID, 'Sam' as Name, 29 as Age, 'F' from dual
)
, grades as (
select 1001 as UserID , 120 as ClassID, 4 as Grade from dual
Union
select 1001 as UserID , 220 as ClassID, 2 as Grade from dual
Union
select 1021 as UserID , 130 as ClassID, 4 as Grade from dual
Union
select 1021 as UserID , 230 as ClassID, 4 as Grade from dual
Union
select 1021 as UserID , 340 as ClassID, 2 as Grade from dual
)
select u.ID, u.Name, AVG(g.grade) as gpa
from users u
join grades g on u.ID = g.UserID
group by u.ID, u.Name
UPDATE: The answer I've been trying to get for a long time is in Ben's comment below which I include here:
"There is no variable, which you can create on the fly and join to other tables in standard SQL #wcm, yes. There is a number of different type of objects that can be created that will allow you to do this, but not exactly as you would in T-SQL".
If I understand correctly, then if I needed temporary storage of data that is confined to being visible to my session, then I'd be using a global temporary table. Probably more overhead than storing in memory, but plenty of advantages too -- gathering statistics on them, indexing them, and the ability to store data without regard to memory consumption.

Resources