I'm running on a 2012 MS-SQL server and have a table USER with Age, Gender among other fields and a SALES table with sales records.
I'm currently calculating the Sales Leaderboard showing an list of Sales People ordered by their TOP Sales so to give an example this list returns various sales rep based on their Top Sales. Somewhere in the middle of the list we have Mr. Thomas which let's say is #4th.
My current task is to display how Thomas compares to sales reps that have the same Age as him and also how he compares with sales rep that have the same gender as him. The calculation will return a different result than the overall list described above.
My ideal stored procedure would receive 1 param (UserId) and return the following single record values: OverallPosition, OverallPositionTotalCount, AgePosition, AgeTotalCount, GenderPosition, GenderTotalCount
DATA SAMPLE:
CREATE TABLE dbo.User
(
UserId int NOT NULL IDENTITY (1, 1),
Name nvarchar(50) NOT NULL,
Age int NULL,
Gender nvarchar(10) NULL
)
1, James, 30, 'male'
2, Monica, 27, 'female'
3, Paul, 30, 'male'
4, Thomas, 30, 'male'
5, Mike, 22, 'male'
6, Sabrina, 30, 'female'
CREATE TABLE dbo.Sales
(
SalesId int NOT NULL IDENTITY (1, 1),
UserId int NOT NULL,
TotalSale int NOT NULL
) ON [PRIMARY]
1, 1, $900,000
2, 1, $1,000,000
3, 2, $900,000
4, 2, $400,000
5, 3, $750,000
6, 3, $300,000
7, 4, $875,000
8, 5, $700,000
9, 5, $1,200,000
10, 6, $850,000
Sales Leaderboard list
SELECT u.UserId, u.Name, MAX(s.TotalSale) as TopSale, Count(*) OVER () AS TotalCount
FROM User u
INNER JOIN Sales s on s.UserId = u.UserId
GROUP BY u.UserID, u.Name
ORDER BY TopSale DESC
OFFSET (@PageIndexSelected) * @PageCountSelected ROWS
FETCH NEXT @PageCountSelected ROWS ONLY
Ideal Calculation Results
Since Thomas (userId 4) is 30 of Age and 'male', his Stats should look like this
OverallPosition = 4; OverallPositionTotalCount = 6 (i.e 4 out of 6)
$1,200,000 Mike
$1,000,000 James
$900,000 Monica
$875,000 Thomas
$850,000 Sabrina
$750,000 Paul
AgePosition = 2; AgeTotalCount = 4 (i.e. 2 out of 4)
$1,000,000 James
$875,000 Thomas
$850,000 Sabrina
$750,000 Paul
GenderPosition = 3; GenderTotalCount = 4 (i.e 3 out of 4)
$1,200,000 Mike
$1,000,000 James
$875,000 Thomas
$750,000 Paul
Note
The expected result is ONLY the values for OverallPosition, OverallPositionTotalCount, AgePosition, AgeTotalCount, GenderPosition, GenderTotalCount for a single user (the stored procedure will receive the UserId as param) and NOT the actual list.
EXPECTED RETURN
OverallPosition = 4,
OverallPositionTotalCount = 6,
AgePosition = 2,
AgeTotalCount = 4,
GenderPosition = 3,
GenderTotalCount = 4
As I stated on my comments, I really don't know how to approach this problem. I hope that somebody will be willing to help !!