I have used stackoverflow for years and usually I have been able to find what I need through searching but I apologize, I couldn't find an example like this. I am extremely new to this stuff but love doing it at work to assist with various projects.
I work in the insurance claims industry, we have insurance claims as electronic files. The supervisors do reviews of these files, simply called a file review. I have an Access db which I use to track all the file reviews at work.
Sometimes as Claims Supervisors we are doing a file review on our OWN associates, meaning the associate reports to me. Other times I am doing a file review on an associate who reports to a DIFFERENT supervisor.
My master table looks like this (with lots of other columns) and each record indicates a file review:
ClaimNumber | Supervisor | Reviewer
----------- | -----------| ------------
1---------- | ---Bob-----| Bob
2---------- | ---Bob-----| Amy
3---------- | ---John----| Amy
4---------- | ---John----| Dean
5---------- | ---John----| John
6---------- | ---Dean----| Dean
7---------- | ---Dean----| Dean
8---------- | ---Dean----| Bob
9---------- | ---Kyle----| Bob
10--------- | ---Bob-----| Shawn
Depending on if a join is used, please note that sometimes a supervisor name is not listed a single time in the reviewer column. Likewise, sometimes a reviewer name is not listed a single time in the supervisor column.
So, I need a single query that returns
Reviewer ------ TotalReviews ---- Self_Reviews ---- Cross_Reviews
The reviewer is all the unique reviewers
TotalReviews is a count of how many times that reviewer's name is listed in reviewer column
Self reviews is a count of how many times that reviewer's name is listed in the reviewer column where their name is equal to the supervisor name for that particular record
Cross reviews is a count of how many times that reviewer's name is listed in the reviewer column where their name is NOT equal to the supervisor name for that particular record
Bob has 3 reviews total, 1 self, 2 cross
Amy has 2 reviews total, 0 self, 2 cross
Dean has 3 reviews total, 2 self, 1 cross
John has 1 review, 1 self, 0 cross
Shawn has 1 review, 0 self, 1 cross
I know this seems complex to me but I am hoping someone can assist to make it much easier. I am just using Access so any oracle or other SQL syntax may not work.
There is a simple trick you can use here: summing over indicator functions.
The indicator function for self reviews would be Switch(Supervisor = Reviewer,1,True,0)
.
That leads to the following query:
SELECT
T.Reviewer,
COUNT(*) AS TotalReviews,
SUM(Switch(T.Supervisor = T.Reviewer,1,True,0)) AS Self_Reviews,
SUM(Switch(T.Supervisor <> T.Reviewer,1,True,0)) AS Cross_Reviews
FROM TableName T
GROUP BY T.Reviewer
In other environments than Access, the indicator function would typically be the fllowing:
CASE WHEN Supervisor = Reviewer THEN 1 ELSE 0 END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.