Grouping partial table data on SQL -
i have table data below. in scenario, employee can have either 1 or 0 or (1 , 0) never employee exists combination of multiple 1 , multiple 0.
employee status
<table> <tr> <th> employee</th> <th> status </th> <tr> <tr> <td>abc </td> <td> 1</td> </tr> <tr> <td>abc </td> <td> 1</td> </tr> <tr> <td>pqr </td> <td> 1</td> </tr> <tr> <td>xyz </td> <td> 0</td> </tr> </table>
below expected output.
<table> <tr> <th> employee</th> <th> status </th> <tr> <tr> <td>abc </td> <td> $</td> </tr> <tr> <td>pqr </td> <td> yes</td> </tr> <tr> <td>xyz </td> <td> no</td> </tr> </table>
that means, if there duplicate records status should $, if 1 yes , if 0 no.
thank in advance helping on this
what have tried:
;with ctee (rownumber, employeename, [status]) (select a.rownumber, a.employeename, case when a.rownumber > 1 '' when a.rownumber = 1 , working = 0 'n' else 'y' end [status] (select row_number() over(partition employeename order employeename) [rownumber], employeename, [status] [table] group employeename, [status] ) ) select employeename, [status] ctee c1 (rownumber = 1 , employeename not in (select employeename ctee employeename = c1.employeename , [status] = '')) or rownumber =2
there lot of approaches this, here 1 uses derived tables count()
:
select t.employee, case when cnt > 1 '$' when cnt = 1 , status = 1 'yes' else 'no' end 'status' (select distinct employee, status table) t inner join (select count(status) 'cnt', employee table group employee) t2 on t2.employee = t.employee
Comments
Post a Comment