sql - Using Aggregate and Max functions -


i'm having problems below query, working in sql server.

select           emp_id= case employee_id         when ''             rtrim(last_name) + '_' + rtrim(first_name)             + '_' + rtrim(gender) + '_'             + rtrim(race_ethnicity_code) + '_'             + rtrim(high_degree_code) + '_' + rtrim(position_code) + '_'             + rtrim(assignment_code)             else employee_id             end ,          last_name, first_name,          assign_perc,              assignment_num,         cast((total_salary)as numeric (18,2))* cast((assign_perc) numeric (18,2)) salary,         total_salary employee  order last_name, first_name, district_name 

my script simple extraction of columns, creating unique key through case statement emp_id when null. problem im having when multiplying assign_perc total_salary when person has multiple assignments , taking max salary when sales person listed once. example - expected results:

enter image description here

john smith part time worker having 1 assignment being listed 1 time, assign_perc less 1 still need max salary rather aggregate total (assign_perc*total_salary). thank help.

as noted above, excessively messy since you'll need use case statement determine virtual employee_id. much simpler if can either refactor case statement udf, or store result in table goodness sake!

to clarify going - want join employee table on table containing counts each employee. count table this:

select employee_id, count(*) employee_count employee group employee_id 

joining them this:

select ... employee join (select employee_id, count(*) employee_count       employee       group employee_id) ec on employee.employee_id = ec.employee_id 

your calculated salary become:

        case              when ec.employee_count > 1                  cast((total_salary)as numeric (18,2))* cast((assign_perc) numeric (18,2))              else total_salary          end salary, 

and here complete query substituting monstrous case statement in place of 'employee_id':

select           case employee.employee_id         when ''             rtrim(last_name) + '_' + rtrim(first_name)             + '_' + rtrim(gender) + '_'             + rtrim(race_ethnicity_code) + '_'             + rtrim(high_degree_code) + '_' + rtrim(position_code) + '_'             + rtrim(assignment_code)             else employee.employee_id             end emp_id,          last_name, first_name,          assign_perc,          assignment_num,         case              when ec.employee_count > 1                  cast((total_salary)as numeric (18,2))* cast((assign_perc) numeric (18,2))              else total_salary          end salary,         total_salary employee join (select case employee.employee_id         when ''             rtrim(last_name) + '_' + rtrim(first_name)             + '_' + rtrim(gender) + '_'             + rtrim(race_ethnicity_code) + '_'             + rtrim(high_degree_code) + '_' + rtrim(position_code) + '_'             + rtrim(assignment_code)             else employee.employee_id             end employee_id, count(*) employee_count        employee       group case employee.employee_id         when ''             rtrim(last_name) + '_' + rtrim(first_name)             + '_' + rtrim(gender) + '_'             + rtrim(race_ethnicity_code) + '_'             + rtrim(high_degree_code) + '_' + rtrim(position_code) + '_'             + rtrim(assignment_code)             else employee.employee_id             end) ec    on case employee.employee_id         when ''             rtrim(last_name) + '_' + rtrim(first_name)             + '_' + rtrim(gender) + '_'             + rtrim(race_ethnicity_code) + '_'             + rtrim(high_degree_code) + '_' + rtrim(position_code) + '_'             + rtrim(assignment_code)             else employee.employee_id             end = ec.employee_id order last_name, first_name, district_name 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -