Get hierarchy of all different level of managers - sql

I'm using pgAdmin4 and I have a SQL table with employee/manager HR data that looks like this:
| employee_id | email_address | full_name | band_lvl | manager_id |
| 5592 | jillr#ex.org | Jill Rhode | 20 | 6521 |
| 6421 | racheln#ex.org | Rachel Nam | 40 | 4251 |
| 2818 | todda#ex.org | Todd Alex | 25 | 6421 |
| 4251 | jalens#ex.org | Jalen Smith | 60 | 2199 |
| 6521 | tolun#ex.org | Tolu Nagoye | 30 | 2199 |
| 7831 | jina#ex.org | Ji Na | 80 | NULL |
| 2199 | zaynm#ex.org | Zayn Mate | 70 | 7831 |
Based on the first manager_id and employee_id, I'm seeking to return the following columns: Level1 Manager Name, Level1 Manager Email, Level1 Manager Band Lvl, Level1 Manager Manager's Id. I then want to do that for each manager that's a step above, until there are no higher managers.
The desired output should look like this:
| employee_id | email_address | full_name | band_lvl | manager_id | Lvl1 Mng Nm | Lvl1 Mng Email | Lvl1 Mng Band Lvl | Lvl1 Mng Mngs Id | Lvl2 Mng Nm | Lvl2 Mng Email | Lvl2 Mng Band Lvl | Lvl2 Mng Mngs Id |
| 5592 | jillr#ex.org | Jill Rhode | 20 | 6521 | Tolu Nagoye | tolun#ex.org | 30 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 |
| 6421 | racheln#ex.org | Rachel Nam | 40 | 4251 | Jalen Smith | jalens#ex.org | 60 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 |
| 2818 | todda#ex.org | Todd Alex | 25 | 6421 | Rachel Nam | racheln#ex.org | 40 | 4251 | Jalen Smith | jalens#ex.org | 60 | 2199 |
| 4251 | jalens#ex.org | Jalen Smith | 60 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL |
| 6521 | tolun#ex.org | Tolu Nagoye | 30 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL |
| 7831 | jina#ex.org | Ji Na | 80 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2199 | zaynm#ex.org | Zayn Mate | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL | NULL | NULL | NULL | NULL |
So far, this is what I've come up with, to get the first columns for the Level 1 Manager; however, I don't know where to go from here, as I'm very new to SQL:
SELECT B.employee_id,
B.email_address,
B.full_name,
B.band_lvl,
B.manager_id,
B1.full_name AS L1_mng_nm,
B1.email_address AS L1_mng_email,
B1.band_lvl AS L1_mng_band_lvl,
B1.manager_id AS L1_mgr_mgrs_id
FROM hrdata B
INNER JOIN hrdata B1 ON
B.manager_id = B1.employee_id;

Your query is close, but you would need to make a few changes to get to your desired output. To begin, I would recommend doing a LEFT JOIN as opposed to an INNER JOIN, as the INNER JOIN will not return null values and will instead drop records that it cannot find a match for in both tables (in this case, if it cannot find a match on manager_id to employee_id from the first use of hrdata to the second use of hrdata).
After that, your query should look similar to what you have already done, just with another self-join to get the second-level manager data:
SELECT B.employee_id,
B.email_address,
B.full_name,
B.band_lvl,
B.manager_id,
B1.full_name AS L1_mng_nm,
B1.email_address AS L1_mng_email,
B1.band_lvl AS L1_mng_band_lvl,
B1.manager_id AS L1_mgr_mgrs_id,
B2.full_name AS L2_mng_nm,
B2.email_address AS L2_mng_email,
B2.band_lvl AS L2_mng_band_lvl,
B2.manager_id AS L2_mgr_mgrs_id,
FROM hrdata B
LEFT JOIN hrdata B1
ON B1.employee_id = B.manager_id
LEFT JOIN hrdata B2
ON B2.employee_id = B1.manager_id

Related

Using one query's result into another

I have this query which shows the below result, I want to use this MarksObtained and take out min, max and avg of each course and show it with the second query (have provided below).
select
CourseName, StdID, MarksObtained
from
stdmarks
inner join
course on course.courseid = stdmarks.examid
+--------------------------+-------+---------------+
| CourseName | StdID | MarksObtained |
+--------------------------+-------+---------------+
| Digital Logic | 1 | 20 |
| Visual Prog | 1 | 20 |
| Computer Arch and Design | 1 | 20 |
| Digital Logic | 2 | 20 |
| Visual Prog | 2 | 20 |
+--------------------------+-------+---------------+
This is the second query
select
distinct CourseName, TeacherName, SemName
from
teacher
inner join
stdcourseteacher on teacher.teacherid = stdcourseteacher.teacherid
inner join
course on course.courseid = stdcourseteacher.courseid
inner join
semester on stdcourseteacher.semid = semester.semid
+-------------------------+-------------+----------+
| CourseName | TeacherName | SemName |
+-------------------------+-------------+----------+
| Business Communications | Dr. Iman | Fall2021 |
| Calculus - 1 | Dr. Khalid | Fall2021 |
| Calculus - 2 | Dr. Khalid | Fall2020 |
+-------------------------+-------------+----------+
So it will basically show min, max and avg of each course achieved by the students.
What I want:
+-------------------------+-------------+----------+-----+-----+-----+
| CourseName | TeacherName | SemName | Min | Max | Avg |
+-------------------------+-------------+----------+-----+-----+-----+
| Business Communications | Dr. Iman | Fall2021 | 80 | 20 | 50 |
| Calculus - 1 | Dr. Khalid | Fall2021 | 70 | 15 | 45 |
| Calculus - 2 | Dr. Khalid | Fall2020 | 85 | 15 | 50 |
+-------------------------+-------------+----------+-----+-----+-----+
Sample data:
StdMarks table:
+-------+--------+---------------+
| StdID | ExamID | MarksObtained |
+-------+--------+---------------+
| 1 | 9 | 20 |
| 1 | 10 | 20 |
| 1 | 11 | 20 |
+-------+--------+---------------+
StdCourseTeacher Table:
+-------+----------+------------+-------+
| StdID | CourseID | TeacherID | SemID |
+-------+----------+------------+-------+
| 1 | 9 | 7 | 6 |
| 1 | 10 | 7 | 6 |
| 1 | 11 | 2 | 6 |
| 2 | 9 | 7 | 6 |
| 2 | 10 | 7 | 6 |
+-------+----------+------------+-------+
Exam Table:
+--------+--------+----------+----------+-------+----------+-----------+
| ExamID | EvalID | Topic | MaxMarks | SemID | CourseID | TeacherID |
+--------+--------+----------+----------+-------+----------+-----------+
| 1 | 3 | Mid-Term | 20 | 6 | 1 | 3 |
| 2 | 3 | Mid-Term | 20 | 6 | 2 | 4 |
| 3 | 3 | Mid-Term | 20 | 6 | 3 | 7 |
+--------+--------+----------+----------+-------+----------+-----------+
Course Table:
+----------+---------------------------+----------+
| CourseID | CourseName | Semester |
+----------+---------------------------+----------+
| 1 | Calculus - 1 | 1 |
| 2 | Business Communications | 1 |
| 3 | Introduction To Computing | 1 |
+----------+---------------------------+----------+
Semester Table:
+-------+------------+
| SemID | SemName |
+-------+------------+
| 1 | Spring2020 |
| 2 | Summer2020 |
+-------+------------+
Teacher Table:
+-----------+-------------+
| TeacherID | TeacherName |
+-----------+-------------+
| 2 | Dr. Ahmed |
| 3 | Dr. Khalid |
+-----------+-------------+
I think you want to use group by in order to use aggregate functions as follows:
select CourseName, TeacherName, SemName, min(MarksObtained), Max(MarksObtained), avg(MarksObtained)
from teacher T
inner join CT on CT.teacherid = T.teacherid
inner join course C on C.courseid = CT.courseid
inner join semester S on S.semid = CT.semid
inner join stdmarks M on M.examid = C.courseid
group by CourseName, TeacherName, SemName

How to check dates condition from one table to another in SQL

Which way we can use to check and compare the dates from one table to another.
Table : inc
+--------+---------+-----------+-----------+-------------+
| inc_id | cust_id | item_id | serv_time | inc_date |
+--------+---------+-----------+-----------+-------------+
| 1 | john | HP | 40 | 17-Apr-2015 |
| 2 | John | HP | 60 | 10-Jan-2016 |
| 3 | Nick | Cisco | 120 | 11-Jan-2016 |
| 4 | samanta | EMC | 180 | 12-Jan-2016 |
| 5 | Kerlee | Oracle | 40 | 13-Jan-2016 |
| 6 | Amir | Microsoft | 300 | 14-Jan-2016 |
| 7 | John | HP | 120 | 15-Jan-2016 |
| 8 | samanta | EMC | 20 | 16-Jan-2016 |
| 9 | Kerlee | Oracle | 10 | 2-Feb-2017 |
+--------+---------+-----------+-----------+-------------+
Table: Contract:
+-----------+---------+----------+------------+
| item_id | con_id | Start | End |
+-----------+---------+----------+------------+
| Dell | DE2015 | 1/1/2015 | 12/31/2015 |
| HP | HP2015 | 1/1/2015 | 12/31/2015 |
| Cisco | CIS2016 | 1/1/2016 | 12/31/2016 |
| EMC | EMC2016 | 1/1/2016 | 12/31/2016 |
| HP | HP2016 | 1/1/2016 | 12/31/2016 |
| Oracle | OR2016 | 1/1/2016 | 12/31/2016 |
| Microsoft | MS2016 | 1/1/2016 | 12/31/2016 |
| Microsoft | MS2017 | 1/1/2017 | 12/31/2017 |
+-----------+---------+----------+------------+
Result:
+-------+---------+---------+--------------+
| Calls | Cust_id | Con_id | Tot_Ser_Time |
+-------+---------+---------+--------------+
| 2 | John | HP2016 | 180 |
| 2 | samanta | EMC2016 | 200 |
| 1 | Nick | CIS2016 | 120 |
| 1 | Amir | MS2016 | 300 |
| 1 | Oracle | OR2016 | 40 |
+-------+---------+---------+--------------+
MY Query:
select count(inc_id) as Calls, inc.cust_id, contract.con_id,
sum(inc.serv_time) as tot_serv_time
from inc inner join contract on inc.item_id = contract.item_id
where inc.inc_date between '2016-01-01' and '2016-12-31'
group by inc.cust_id, contract.con_id
The result from inc table with filter between 1-jan-2016 to 31-Dec-2016 with
count of inc_id based on the items and its contract start and end dates .
If I understand correctly your problem, this query will return the desidered result:
select
count(*) as Calls,
inc.cust_id,
contract.con_id,
sum(inc.serv_time) as tot_serv_time
from
inc inner join contract
on inc.item_id = contract.item_id
and inc.inc_date between contract.start and contract.end
where
inc.inc_date between '2016-01-01' and '2016-12-31'
group by
inc.cust_id,
contract.con_id
the question is a little vague so you might need some adjustments to this query.
select
Calls = count(*)
, Cust = i.Cust_id
, Contract = c.con_id
, Serv_Time = sum(Serv_Time)
from inc as i
inner join contract as c
on i.item_id = c.item_id
and i.inc_date >= c.[start]
and i.inc_date <= c.[end]
where c.[start]>='20160101'
group by i.Cust_id, c.con_id
order by i.Cust_Id, c.con_id
returns:
+-------+---------+----------+-----------+
| Calls | Cust | Contract | Serv_Time |
+-------+---------+----------+-----------+
| 1 | Amir | MS2016 | 300 |
| 2 | John | HP2016 | 180 |
| 1 | Kerlee | OR2016 | 40 |
| 1 | Nick | CIS2016 | 120 |
| 2 | samanta | EMC2016 | 200 |
+-------+---------+----------+-----------+
test setup: http://rextester.com/WSYDL43321
create table inc(
inc_id int
, cust_id varchar(16)
, item_id varchar(16)
, serv_time int
, inc_date date
);
insert into inc values
(1,'john','HP', 40 ,'17-Apr-2015')
,(2,'John','HP', 60 ,'10-Jan-2016')
,(3,'Nick','Cisco', 120 ,'11-Jan-2016')
,(4,'samanta','EMC', 180 ,'12-Jan-2016')
,(5,'Kerlee','Oracle', 40 ,'13-Jan-2016')
,(6,'Amir','Microsoft', 300 ,'14-Jan-2016')
,(7,'John','HP', 120 ,'15-Jan-2016')
,(8,'samanta','EMC', 20 ,'16-Jan-2016')
,(9,'Kerlee','Oracle', 10 ,'02-Feb-2017');
create table contract (
item_id varchar(16)
, con_id varchar(16)
, [Start] date
, [End] date
);
insert into contract values
('Dell','DE2015','20150101','20151231')
,('HP','HP2015','20150101','20151231')
,('Cisco','CIS2016','20160101','20161231')
,('EMC','EMC2016','20160101','20161231')
,('HP','HP2016','20160101','20161231')
,('Oracle','OR2016','20160101','20161231')
,('Microsoft','MS2016','20160101','20161231')
,('Microsoft','MS2017','20170101','20171231');

Unusual two tables join

I have table Persons:
----------------------------------------
id | name | phone | house_id |
----------------------------------------
1 | Sarah | 1234567 | 101 |
2 | Joseph | 7654321 | 102 |
3 | David | 1231231 | null |
Ans second table Houses:
----------------------------------------
id | street | number |
----------------------------------------
101 | Evergreen Terrace | 742 |
102 | Baker Street | 223B |
103 | Oxford Street | 23A |
I need such output table as following:
--------------------------------------------------------------------------------
id(person)| name | phone | house_id | id(house) | street | number |
--------------------------------------------------------------------------------
1 | Sarah | 1234567 | 101 | 101 | Evergreen T...| 742 |
2 | Joseph | 7654321 | 102 | 102 | Baker Street | 223B |
3 | David | 1231231 | null | null | null | null |
4 | null | null | null | 103 | Oxford Street | 23A |
What kind of join do I need to use to achieve such result?
SELECT
A.id AS 'Person',
A.name,
A.phone,
A.house_id,
B.id AS 'House',
B.street,
B.number
FROM
Persons AS A
FULL OUTER JOIN Houses AS B
ON A.house_id = B.id

Sub query in t sql select statement

I have a query which lists users but it requires a sub query to get whether they are available today. I need the fourth column 'Availability' to iterate through each user and display if they have availability or display a null. I've tried everything I can think of sub queries, cursors etc but no joy. Any pointers welcome!
SELECT inter.authno,
inter.FirstName,
inter.Surname,
COALESCE((
Select at.typeName as [Availability]
FROM [database].[dbo].[Interviewer] inter
full join [database].[dbo].[availability] av
on inter.authno = av.authno
full join [database].[dbo].[availability_days] ad
on av.availID = ad.availID
full join [database].[dbo].[availibiltyType] at
on av.typeID = at.typeid
where exists(
select authno
from [database].[dbo].[Interviewer]
)
and ad.actualDay = '2015-05-21'
), null ) AS [Availability]
FROM [database].[dbo].[Interviewer] inter
The query gives the below results, but it should only show Available for Harry Kane and the rest should be null.
authno FirstName Surname Availability
-------------------------------------
10 Minch Yoda Available
11 Darth Vadar Available
12 Darth Maul Available
14 Obi Wan Kenobi Available
15 Qui-Gon Jinn Available
16 Darth Sidious Available
17 Boba Fett Available
24 Harry Kane Available
39 mark o'neill Available
I also tried the code suggestion below kindly provided which gives some results I need, but it shows all of the results instead of the availability type for today.
SELECT
inter.authno,
inter.FirstName,
inter.Surname,
at.typeName as [Availability]
FROM [database].[dbo].[Interviewer] inter
left JOIN [database].[dbo].[availability] av
on inter.authno = av.authno
left JOIN [database].[dbo].[availability_days] ad
on av.availID = ad.availID
and ad.actualDay = '2015-07-21'
left JOIN [database].[dbo].[availibiltyType] at
on av.typeID = at.typeid
Output:
+----+---------+-----------+--------------+
| 10 | Minch | Yoda | NULL |
+----+---------+-----------+--------------+
| 11 | Darth | Vadar | NULL |
| 12 | Darth | Maul | NULL |
| 13 | Luke | Skywalker | NULL |
| 14 | Obi Wan | Kenobi | NULL |
| 15 | Qui-Gon | Jinn | Annual Leave |
| 16 | Darth | Sidious | NULL |
| 17 | Boba | Fett | UO |
| 17 | Boba | Fett | Available |
| 18 | test22 | test33 | NULL |
| 19 | test7 | test7 | NULL |
| 22 | Bob | Marley | NULL |
| 23 | JO | JO | NULL |
| 24 | Harry | Kane | Annual Leave |
| 24 | Harry | Kane | Available |
| 24 | Harry | Kane | Available |
| 24 | Harry | Kane | Annual Leave |
| 24 | Harry | Kane | Annual Leave |
| 24 | Harry | Kane | NW |
| 24 | Harry | Kane | NW |
| 24 | Harry | Kane | Available |
| 39 | mark | o'neill | US |
+----+---------+-----------+--------------+
I also tried the below which gets me the exact results that I need only that, I need to display all users whether they have a date in the table or not. i.e. If I change the date to last weer Harry Kane disappears.
SELECT
inter.authno,
inter.FirstName,
inter.Surname,
at.typeName as [Availability]
FROM [database].[dbo].[Interviewer] inter
left JOIN [database].[dbo].[availability] av
on inter.authno = av.authno
left JOIN [database].[dbo].[availability_days] ad
on av.availID = ad.availID
left JOIN [database].[dbo].[availibiltyType] at
on av.typeID = at.typeid
where ad.actualDay = '2015-05-21'or ad.actualDay is null
Output for today:
+--------+-----------+-----------+--------------+
| authno | FirstName | Surname | Availability |
+--------+-----------+-----------+--------------+
| 10 | Minch | Yoda | NULL |
| 11 | Darth | Vadar | NULL |
| 12 | Darth | Maul | NULL |
| 13 | Luke | Skywalker | NULL |
| 14 | Obi Wan | Kenobi | NULL |
| 16 | Darth | Sidious | NULL |
| 18 | test22 | test33 | NULL |
| 19 | test7 | test7 | NULL |
| 22 | Bob | Marley | NULL |
| 23 | JO | JO | NULL |
| 24 | Harry | Kane | Available |
+--------+-----------+-----------+--------------+
Output for 2015-05-10
+--------+-----------+-----------+--------------+
| authno | FirstName | Surname | Availability |
+--------+-----------+-----------+--------------+
| 10 | Minch | Yoda | NULL |
| 11 | Darth | Vadar | NULL |
| 12 | Darth | Maul | NULL |
| 13 | Luke | Skywalker | NULL |
| 14 | Obi Wan | Kenobi | NULL |
| 16 | Darth | Sidious | NULL |
| 18 | test22 | test33 | NULL |
| 19 | test7 | test7 | NULL |
| 22 | Bob | Marley | NULL |
| 23 | JO | JO | NULL |
+--------+-----------+-----------+--------------+
If I understand correctly, you want a correlated subquery for your version of the query:
SELECT inter.authno,
inter.FirstName,
inter.Surname,
(Select at.typeName as [Availability]
FROM [database].[dbo].[availability] av join
[database].[dbo].[availability_days] ad
on av.availID = ad.availID join
[database].[dbo].[availibiltyType] at
on av.typeID = at.typeid
where inter.authno = av.authno and ad.actualDay = '2015-05-21'
) AS [Availability]
FROM [database].[dbo].[Interviewer] inter;
Some notes:
COALESCE(<x>, NULL) doesn't make sense. Just use <X>
With a subquery, you should use IFNULL() rather than COALESCE(), because SQL Server has (what I consider to be) a flawed implementation of COALESCE().
Your subquery needs to be correlated to the outer query.
I have no idea what the EXISTS clause was supposed to do. If the table has any rows, then it would always return TRUE.
There is no reason for full joins in the subquery.
I would expect your version to return the error "subquery returns more than one row".
Without seeing your table structure It's a guess, but an educated one.
Try this:
SELECT inter.authno,
inter.FirstName,
inter.Surname,
at.typeName as [Availability]
FROM [database].[dbo].[Interviewer] inter
LEFT JOIN [database].[dbo].[availability] av on inter.authno = av.authno
LEFT JOIN [database].[dbo].[availability_days] ad on av.availID = ad.availID and ad.actualDay = '2015-05-21'
LEFT JOIN [database].[dbo].[availibiltyType] at on av.typeID = at.typeid
For future sql questions you might have, Please include the relevant tables DDL, some sample data (preferably as DML statements), and the desired output.

rolling up data and getting SUM group by

I have two tables:
**OrgPhysicianMappingtbl**
org varchar
physician varchar
**PhysicianDatatbl**
physician varchar
names varchar
datapoint int
data from OrgPhysicianMappingtbl:
+---------+-----------+
| org | physician |
+---------+-----------+
| ABC123 | B032 |
| ABC123 | B023 |
| ABC123 | A022 |
| ABB443 | A32 |
| ABB332 | F23 |
| BVD222 | G23 |
| BVD222 | GG2 |
| BOS5223 | G4 |
+---------+-----------+
data from PhysicianDatatbl:
+------------+----------------------------------+-----------+
| physician | names | datapoint |
+------------+----------------------------------+-----------+
| B032 | CASH | 68 |
| B032 | MEDICAID | 89 |
| B032 | ALL THIRD PARTY | 3,769 |
| B032 | WORKERS COMP | 39 |
| B032 | US SCRIPT (PROC-UNSP) | 86 |
| B032 | MEDCO HLTH SOLUTIONS (PROC-UNSP) | 79 |
| B032 | BCBS WELLPOINT/ANTHEM/WEL UNSPEC | 76 |
| B032 | KY EMPLOYEES HLTH PLN/KEHP (KY) | 62 |
| B032 | UHC/PAC/AARP MED PDP GENERAL(KY) | 52 |
| B032 | WELLCARE OF KENTUCKY (KY) | 42 |
| B032 | CCRX MED PDP GENERAL (KY) | 39 |
| B032 | WORKERS COMP - EMPLOYER | 37 |
| B032 | HUMANA/CAREPLS MED D GENERAL(KY) | 27 |
| B032 | CIGNA MEDICARE RX PDP GNRL (KY) | 26 |
| B023 | CASH | 167 |
| B023 | MEDICAID | 34 |
| B023 | ALL THIRD PARTY | 3,165 |
| B023 | WORKERS COMP | 56 |
| B023 | WORKERS COMP - EMPLOYER | 50 |
| B023 | BLUE CHOICE PPO (TX) | 48 |
| B023 | TRICARE HUMANA MILITARY SOUTH | 47 |
| B023 | MEDCO HLTH SOLUTIONS (PROC-UNSP) | 32 |
| B023 | BROADSPIRE INC | 27 |
| B023 | ADVANCEPCS (PROC-UNSP) | 27 |
| B023 | UNITED HLTHCARE-(TX) TEXAS | 23 |
| B023 | HEALTHSPRING PDP (TX) | 18 |
| B023 | AETNA INC.-(TX) HOUSTON | 13 |
| B023 | WELLCARE MEDICARE D GENERAL(TX) | 12 |
+------------+----------------------------------+-----------+
example of desired result:
+-----------+-----------------+-----------+
| org | names | sum(datapoints|
+-----------+-----------------+-----------+
| ABC123 | CASH | 236 |
| ABC123 | MEDICAID | 123 |
| ABC123 | ALL THIRD PARTY | 6,933 |
| ABC123 | WORKERS COMP | 94 |
| … | … | … |
+-----------+-----------------+-----------+
I need to roll up my data to the ORG level. There are multiple physicians per ORG. every physician has names and datapoints associated with it. The result that I want is the sum of all datapoints per names rolled up to the org level.
Can someone please get me started on this?
here's what i tried:
select o.org,p.names,sum(p.datapoint)
from OrgPhysicianMappingtbl o
join PhysicianDatatbl p
on o.physician=p.physician
group by o.org,p.names
You can use GROUPING SETS:
select o.org, p.names, sum(p.datapoint)
from OrgPhysicianMappingtbl o
inner join PhysicianDatatbl p
on o.physician = p.physician
GROUP BY GROUPING SETS((o.org), (p.names))
see SQL Fiddle with Demo
Well!
SELECT o.org, p.names, sum(p.datapoint)
FROM OrgPhysicianMappingtbl o INNER JOIN PhysicianDatatbl p
ON o.physician = p.physician
GROUP BY o.org, p.names
http://www.sqlfiddle.com/#!2/f59c4/1
select o.org,p.names,sum(p.datapoint)
from OrgPhysicianMappingtbl o
join PhysicianDatatbl p
on o.physician=p.physician
group by o.org,p.names with ROLLUP
Even this should work
SELECT org,names,TotalDP
FROM(
SELECT
o.org
,p.names
,TotalDP = SUM(p.datapoint) OVER(PARTITION BY o.org,p.names)
,Rn = ROW_NUMBER() OVER(PARTITION BY o.org,p.names ORDER BY (SELECT 1))
FROM OrgPhysicianMappingtbl o
JOIN PhysicianDatatbl p on o.physician=p.physician)X WHERE X.Rn = 1