sql server 2005 - Horizontal Count SQL -
I'm sorry if this is a duplicate question, but I could not find my answer.
I am trying to get a calculation of how to take horizontal figures, and how often a specific number appears.
Example table
+ ------- + ------- + ------- + ------- + | Empid | KPI_A | KPIB | KPIAC | + ------- + ------- + ------- + ------- + | 232 | 1 | 3 | 3 | | 112 | 2 | 3 | 2 | | 143 | 3 | 1 | 1 | + ------- + ------- + ------- + ------- + I need to see the following :
+ ------- + -------------- + -------------- + -------------- + | EmpID | (Score of 1). (Score of 2). (Score of 3). + ------- + -------------- + -------------- + ----------- --- + | 232 | 1 | 0 | 2 | | 112 | 0 | 2 | 1 | | 143 | 2 | 0 | 1 | + ------- + -------------- + -------------- + ----------- --- + I hope that makes sense. Any help would be appreciated.
Since you are counting the data in multiple columns, it can be easy to be impossible before Calculate your KPI column, then the score You can apply the UNPIVOT function or crossover to convert your kpi columns into several rows. . Syntax will be similar: Select APID, KPI, Val on your outlet cross (select 'A', KPI_A union, select all 'B', KPIBU union all 'C' See KPIAN) C (KPI, VAL) This gets your multiple columns in several rows, with which it is easy to work:
| EMPID | KPI | Val | | ------- | ----- | ----- | | 232 | A. 1 | | 232 | B 3 | | 232 | C. 3 | | 112 | A. 2 | Now you can easily calculate the number of 1 's, 2 ' s, and 3 It is possible that you have used a composite function with a CASE expression: Select EmpId, sum (when val = 1 again 1 and 0 end) Score 4, number ( In case when val = 2 then 1 and 0 end) score_2, zodiac (case when val = 3 then 1 and 0 end) with scoreon (select from api, kpi, apply with your outlet crawl ('a', kpi_a union) Select All 'B', KPIB Union) Select 'C' Sorry, Kepiaisi) C (KPI, val)) group Apiaidi; This gives the end result:
| EMPID | SCORE_1 | SCORE_2 | SCORE_3 | | ------- | --------- | --------- | --------- | | 112 | 0 | 2 | 1 | | 143 | 2 | 0 | 1 | | 232 | 1 | 0 | 2 |
Comments
Post a Comment