Pandas - join two dataframes based on one column from table and combining two columns from another table - pandas

I am trying to join two tables based on one column from one table and two column from other table
Table a
name, designation
Mr. james john, manager
Mr. jim james, tester
Mr. abe james, developer
Table b
first name, last name, emp id
james,john,1
jim, james,2
abe,james,3
I want to join table a name column with table b combining "Mr. "+first name+last name.

Here's a way to do what your question asks:
res = a.join(b.assign(name='Mr. ' + b['first name'] + ' ' + b['last name']).set_index('name'), on='name')
Input:
dataframe a:
name designation
0 Mr. james john manager
1 Mr. jim james tester
2 Mr. abe james developer
dataframe b:
first name last name emp id
0 james john 1
1 jim james 2
2 abe james 3
Output:
name designation first name last name emp id
0 Mr. james john manager james john 1
1 Mr. jim james tester jim james 2
2 Mr. abe james developer abe james 3

Related

SQL query that will add a column returning a value when two other columns match (similar to vlookup in excel)

I have an employee table that lists all employees and the ID of their manager. The Manager ID refers back to the employee id in this table. I would like to add another column that returns the name of the manager since no one knows their ID numbers.
Current table is basically:
ID Name ManagerID
31 John Smith 10
32 Barb Jones 10
33 Craig Adams 32
I would like to add another column that looks up looks up the manager ID in the ID field and returns the assocaited name like below:
ID Name ManagerID ManagerName
31 John Smith 10 Ted Fish
32 Barb Jones 10 Ted Fish
33 Craig Adams 32 Barb Jones
You can use self-join here.
select e.ID, e.Name, m.ManagerID, e.Name as ManagerName
from employee e
inner join employee m
on e.ID = m.ManagerID;

How to select records only in table b

I have two tables. Table A and Table B. they both have two columns (Name and Salary). Both tables have duplicate name records but the salary is different. How can i write a query to select the name and salary of Table A where the name of table A is in table B's Name column.
MYSQL or MSSQL
Table A
Name Salary
john smith 100
john smith 100
sally smith 100
Dan smith 100
Table B
Name Salary
john smith 100
john smith 100
sally smith 100
result
Name Salary
john smith 100
john smith 100
sally smith 100
Try this:
SELECT Name, Salary
FROM Table_A
WHERE Name IN (SELECT DISTINCT Name
FROM Table_B) ;

How do I transpose multiple rows to columns in SQL

My first time reading a question on here.
I am working at a university and I have a table of student IDs and their supervisors, some of the students have one supervisor and some have two or three depending on their subject.
The table looks like this
ID Supervisor
1 John Doe
2 Peter Jones
2 Sarah Jones
3 Peter Jones
3 Sarah Jones
4 Stephen Davies
4 Peter Jones
4 Sarah Jones
5 John Doe
I want to create a view that turns that into this:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe
2 Peter Jones Sarah Jones
3 Peter Jones Sarah Jones
4 Stephen Davies Peter Jones Sarah Jones
5 John Doe
I have looked at PIVOT functions, but don't think it matches my needs.
Any help is greatly appreciated.
PIVOT was the right clue, it only needs a little 'extra' :)
DECLARE #tt TABLE (ID INT,Supervisor VARCHAR(128));
INSERT INTO #tt(ID,Supervisor)
VALUES
(1,'John Doe'),
(2,'Peter Jones'),
(2,'Sarah Jones'),
(3,'Peter Jones'),
(3,'Sarah Jones'),
(4,'Stephen Davies'),
(4,'Peter Jones'),
(4,'Sarah Jones'),
(5,'John Doe');
SELECT
*
FROM
(
SELECT
ID,
'Supervisor ' + CAST(ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Supervisor) AS VARCHAR(128)) AS supervisor_id,
Supervisor
FROM
#tt
) AS tt
PIVOT(
MAX(Supervisor) FOR
supervisor_id IN ([Supervisor 1],[Supervisor 2],[Supervisor 3])
) AS piv;
Result:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe NULL NULL
2 Peter Jones Sarah Jones NULL
3 Peter Jones Sarah Jones NULL
4 Peter Jones Sarah Jones Stephen Davies
5 John Doe NULL NULL
You will notice that the assignment to Supervisor X is done by ordering by the Supervisor-VARCHAR. If you want the ordering done differently, you might want to include an [Ordering] column; then change to ROW_NUMBER() OVER(PARTITION BY ID ORDER BY [Ordering]). Eg an [Ordering] column could be an INT IDENTITY(1,1). I'll leave that as an excercise to you if that's what's really needed.

How Do I Exclude Data Based on Column Values in SQL?

I have a table which is set up like this:
Name QuestionCd Response
John Smith 837987 3
John Smith 837988 NULL
John Smith 837991 3
John Smith 837996 3
John Smith 838003 NULL
Mary Smith 837987 1
Mary Smith 837988 1
Mary Smith 837991 3
Mary Smith 837996 1
Mary Smith 838003 5
I need to bring in the customer who has a response for all 5 questions, which would be Mary Smith. I've tried to use a case statement to flag the rows that have a response but that would be incorrect since John Smith did not answer all of the questions.
I've tried the following:
-- Case when QuestionCd between '837987' and '838069' and response is null then '' else 'X' end
-- Case when QuestionCd between '837987' and '838069' and Response is null then ''
when QuestionCd between '837987' and '838069' and Response is not null then 'x' end
Any ideas or tips on how to accomplish this? Thank you kindly for your help.
This is how I would do it.
SELECT Name
FROM
(
SELECT Name, QuestionCd
FROM <YourTable>
WHERE NOT Response IS NULL
GROUP BY Name, QuestionCd
) SQ
GROUP BY Name
HAVING COUNT(QuestionCd)=5
The sub query is to have a distinct list of question answered since Mary Smith or John Smith may have answered the question more than one.
If the question ID's are fixed you can just return the name of the person who has answered 5 unique questions
SELECT Name FROM tbl
GROUP BY Name
HAVING count(QuestionCD) = 5
Edit - fixed SQL

How to get corresponding value from query data

I am have MSSQL tables as below
Names
sno username firstname
1 jim sam
2 raj ravi
3 lion king
Marks
sno from given
1 raj jim
2 raj lion
3 lion raj
4 lion jim
Now I wanted to display my table as shown below:
Select [from],
[given]
From Marks
Where [from] = 'raj'
Output Table:
from given firstname
raj jim sam
raj lion king
How can I acheve it I am unable to figure it out?
select marks.from, marks.given, names.fistname
from marks
join names
on marks.given = names.username
where marks.from = 'raj'