How can I run a vlookup function in SQL within the same table? - sql

I'm fairly new to SQL and struggling to find a good way to run the following query.
I have a table that looks something like this:
NAME JOB GRADE MANAGER NAME
X 7 O
Y 6 X
Z 5 X
A 4 Z
B 3 Z
C 2 Z
In this table, it shows that Y and Z report into X, and A, B and C report into Z.
I want to create a computed column showing the grade each person's most senior direct report or "n/a" if they don't manage anyone. So that would look something like this:
NAME JOB GRADE MANAGER NAME GRADE OF MOST SENIOR REPORT
X 7 O 6
Y 6 X N/A
Z 5 X 4
A 4 Z N/A
B 3 Z N/A
C 2 Z N/A
How would I do this?

SELECT g.*,isnull(convert(nvarchar, (SELECT max(g2.GRADE)
FROM dbo.Grade g2 WHERE
g2.manager =g.NAME AND g2.NAME!=g.NAME )),'N/A') as most_graded
FROM dbo.Grade g
The max will find out the topmost graded
Input
X 7 O
y 6 X
Z 5 X
A 6 Z
C 2 Z
Output
X 7 O 6
y 6 X N/A
Z 5 X 6
A 6 Z N/A
C 2 Z N/A

Something like this:
select name, job_grade, manager_name,
(select max(job_grade) from grades g2
where g2.manager_name = g1.name) as grade_of_most_recent_senior
from grades g1
order by name;
The above is ANSI SQL and should work on any DBMS.
SQLFiddle example: http://sqlfiddle.com/#!15/e0806/1

Related

Trying to mark first encounter in every group pandas

I have a df with 5 columns:
What i'm trying to do is mark value of first customer interaction after talking to human in every specific group.
Hopefully the outcome would be like this:
What I have tried is shifting type column to put previous row in front of type to check if its customer and prev row is human. However, I can't figure out a grouping option to get min index for each group for each occurrence.
This works:
k = pd.DataFrame(df.groupby('group').apply(lambda g: (g['type'].eq('customer') & g['type'].shift(1).eq('human')).pipe(lambda x: [x.idxmax(), x[::-1].idxmax()])).tolist())
df['First'] = ''
df['Last'] = ''
df.loc[k[1], 'First'] = 'F'
df.loc[k[1], 'Last'] = 'L'
Output:
>>> df
group type First Last
0 x bot
1 x customer
2 x bot
3 x customer
4 x human
5 x customer F
6 x human
7 x customer L
8 y bot
9 y customer
10 y bot
11 y customer
12 y human
13 y customer F
14 y human
15 y customer L
16 z bot
17 z customer
18 z bot
19 z customer
20 z human
21 z customer F
22 z human
23 z customer L
24 z customer
25 z customer

Pandas - find value in column based on values from another column and replace date in different column

I have a df that looks like below:
ID Name Supervisor SupervisorID
1 X Y
2 Y C
3 Z Y
4 C Y
5 V X
What I need is to find SupervisorID. I can find his ID by checking it in column Name and that I will see his ID so if supervisor is Y then I see that in column Name there is Y so his ID id 2. DF should looks like below:
ID Name Supervisor SupervisorID
1 X Y 2
2 Y C 4
3 Z Y 2
4 C Y 2
5 V X 1
Do you have any idea how to solve this?
Thanks for help and best regards
Use Series.map with DataFrame.drop_duplicates for unique Names, because in real data duplicates:
df['SupervisorID']=df['Supervisor'].map(df.drop_duplicates('Name').set_index('Name')['ID'])
print (df)
ID Name Supervisor SupervisorID
0 1 X Y 2
1 2 Y C 4
2 3 Z Y 2
3 4 C Y 2
4 5 V X 1

Access dynamic AVG query based on filter

I've the following table called tblX:
id
type
nr
1
Z
3
2
Y
3
3
X
3
4
X
5
5
Z
6
6
X
2
I've the following query called qryX based on the table tblX
id
type
nr
total_avg
1
Z
3
3.66
2
Y
3
3.66
3
X
3
3.66
4
X
5
3.66
5
Z
6
3.66
6
X
2
3.66
The SQL looks like this:
SELECT tblX.*, DAvg("nr","tblX") AS total_avg
FROM tblX;
When I'm filtering the data in the query bases on type X I would like the following total_avg result
id
type
nr
total_avg
3
X
3
3.33
4
X
5
3.33
6
X
2
3.33
Instead of 3.33 it gives the originally 3.66.
How can I adjust the aggregated function DAvg("nr","tblX") to be dynamic?
I've been searching for a while but most of the solutions were only for power bi.
The goal is to centralize my calculations in a query instead of multiple separated calculation fields in forms and reports!
How about this:
SELECT
tblX.*,
DAvg("nr", "tblX", "[type] = '" & [type] & "'") AS total_avg
FROM
tblX;

Aggregate symmetric pairs pandas

How do I aggregate symmetric pairs in pandas?
I have a dataframe which looks like this:
X Y count
A B 2
B A 1
C D 5
D C 3
My output should look like this:
X Y count
A B 3
C D 8
Thank you!
I used to have the same problem before , And this is my solution
df1=df[['X','Y']].apply(sorted,1)
df.groupby([df1.X,df1.Y])['count'].sum().reset_index(name='count')
Out[400]:
X Y count
0 A B 3
1 C D 8

Group records from Datatable by Linq and add them to a Dictionary

I have a datatable which is filled from sql query. Sample Query:
A B C D E
1 1 x y z
1 1 x y z
1 2 x y z
2 1 x y z
i want to group them like this one: (A and B will be unique)
A B C D E
1 1 x y z
1 1 x y z
A B C D E
1 2 x y z
A B C D E
2 1 x y z
i tried with linq but could not properly done it. i checked similar questions but they did not solve my problem. It does not have to be linq but i thought if i use linq and group them by using Dictionary that would be good solution for me.