如何在sql中计算行中的重复数据?

时间:2014-02-28 09:44:56

标签: sql oracle row

我的表有很多列,其中包括attendanceregister

  • 如果员工提出更新day1 ='1'和
  • 如果员工提出更新day2 ='1'和
  • 如果员工缺席更新day3 ='2'等,
  • 如果星期天它更新day5 ='0'

这是我的表:

Employee id | Month | day1 | day2 | day3 | day4| day5| ......| day31
      1     |  Jan  |  1   |   1  |  2   |   1 |  0  | ......|  1   
      2     |  Jan  |  2   |   1  |  2   |   1 |  1  | ......|  1

我想获得员工ID = 1,

How to Count present days  and absent days ?

3 个答案:

答案 0 :(得分:1)

使用UNPIVOT将列转换为行。有关详细信息,请参阅the article。以下是您案例的使用示例:

with t as (
select 1234 emp_id,
       'Feb' mon,
       2 day1,
       1 day2,
       2 day3,
       1 day4,
       2 day5,
       1 day6,
       2 day7,
       1 day8,
       2 day9,
       1 day10,
       2 day11,
       1 day12,
       2 day13,
       1 day14,
       2 day15,
       1 day16,
       2 day17,
       1 day18,
       2 day19,
       1 day20,
       2 day21,
       1 day22,
       2 day23,
       1 day24,
       2 day25,
       1 day26,
       2 day27,
       1 day28,
       0 day29,
       0 day30,
       0 day31 from dual)
select emp_id,
       mon,
       sum(nullif(presence_code, 2)) presence_count,
       sum(nullif(presence_code, 1)) / 2 absence_count
  from (select *
          from t unpivot(presence_code for presence_day in (day1,
                                                            day2,
                                                            day3,
                                                            day4,
                                                            day5,
                                                            day6,
                                                            day7,
                                                            day8,
                                                            day9,
                                                            day10,
                                                            day11,
                                                            day12,
                                                            day13,
                                                            day14,
                                                            day15,
                                                            day16,
                                                            day17,
                                                            day18,
                                                            day19,
                                                            day20,
                                                            day21,
                                                            day22,
                                                            day23,
                                                            day24,
                                                            day25,
                                                            day26,
                                                            day27,
                                                            day28,
                                                            day29,
                                                            day30,
                                                            day31))) q
 group by emp_id, mon;

    EMP_ID MON PRESENCE_COUNT ABSENCE_COUNT
---------- --- -------------- -------------
      1234 Feb             14            14

答案 1 :(得分:0)

您可以尝试使用Decode功能

select Decode(day1, 1, 1, 0) + -- <- treat 1 as 1, all other values as 0
       Decode(day2, 1, 1, 0) +
       Decode(day3, 1, 1, 0) + 
       ... 
       Decode(day31, 1, 1, 0) as PresentCount,
       Decode(day1, 2, 1, 0) + -- <- treat 2 as 1, all other values as 0
       Decode(day2, 2, 1, 0) +
       Decode(day3, 2, 1, 0) + 
       ... 
       Decode(day31, 2, 1, 0) as AbsentCount 
  from MyTable
 where (Employee_Id = 1)
 -- and (Month = 'Feb') -- <- uncomment this if you want February only         

P.S。可能你应该重新设计你的表

答案 2 :(得分:0)

我知道这不是你问题的答案。

但是如何使用一个整数列来表示所有当前日期?

e.g:

值1:第1天出现

值3:第1天和第2天出现

价值8:第4天出现

因此,您可以将表格布局更改为:

  

员工ID |月| openDays

整数适合存储长达31天(甚至可以存储长达32天)。