计算oracle中表中的列数

时间:2016-11-04 04:37:47

标签: sql oracle

我有这种格式的表格。现在我需要计算第5个月和第6个月的P数。我需要在第5个月中获得所有P但在第6个月只需要P到第15天。

picture  我试过的代码是::

<div>

我试过的下一个代码是::

<div [ngSwitch]="'viewMode'">
  <template [ngSwitchCase]="'map'" ngSwitchDefault> 
     Map View Content...   
   </template>      
   <template [ngSwitchCase]="'list'"> 
      List View Content...
  </template>
</div>

2 个答案:

答案 0 :(得分:3)

这可能是一种方法,不需要动态SQL:

SELECT count(case when value = 'P' then 1 end)
FROM   horribleTable
UNPIVOT (value FOR dy IN (
                            DAY1  as '1',
                            DAY2  as '2',
                            DAY3  as '3',
                            DAY4  as '4',  
                            DAY5  as '5',
                            DAY6  as '6',
                            DAY7  as '7',
                            DAY8  as '8',
                            DAY9  as '9',
                            DAY10 as '10',
                            DAY11 as '11',
                            DAY12 as '12',
                            DAY13 as '13',
                            DAY14 as '14',
                            DAY15 as '15',
                            DAY16 as '16',
                            DAY17 as '17',
                            DAY18 as '18',
                            DAY19 as '19',
                            DAY20 as '20',
                            DAY21 as '21',
                            DAY22 as '22',
                            DAY23 as '23',
                            DAY24 as '24',
                            DAY25 as '25',
                            DAY26 as '26',
                            DAY27 as '27',
                            DAY28 as '28',
                            DAY29 as '29',
                            DAY30 as '30',
                            DAY31 as '31')
)
where (mth = 5 or (mth = 6 and dy <= 15))
  and yr = 2016    --  optional
;

UNPIVOT将您的列转换为行,以便您可以根据日期应用过滤器; countcase只检查值'P',而月份和年份的过滤器很简单

答案 1 :(得分:1)

表格设计很糟糕,但查询相对较长但仍然很容易:

select month,
  case when day1 = 'P' then 1 else 0 end +
  case when day2 = 'P' then 1 else 0 end +
  case when day3 = 'P' then 1 else 0 end +
  case when day4 = 'P' then 1 else 0 end +
  ...
  case when day31 = 'P' then 1 else 0 end as days
from mytable
where month = 5
union all
select month,
  case when day1 = 'P' then 1 else 0 end +
  case when day2 = 'P' then 1 else 0 end +
  case when day3 = 'P' then 1 else 0 end +
  case when day4 = 'P' then 1 else 0 end +
  ...
  case when day15 = 'P' then 1 else 0 end as days
from mytable
where month = 6

这为您提供每月的总和。如果你想要两个月的一笔钱而不是:

select month,
  case when day1 = 'P' then 1 else 0 end +
  case when day2 = 'P' then 1 else 0 end +
  case when day3 = 'P' then 1 else 0 end +
  case when day4 = 'P' then 1 else 0 end +
  ...
  case when day15 = 'P' then 1 else 0 end +
  case when month = 5 and day16 = 'P' then 1 else 0 end +
  case when month = 5 and day17 = 'P' then 1 else 0 end +
  ...
  case when month = 5 and day31 = 'P' then 1 else 0 end as days
from mytable
where month in (5, 6);

以下是给定的第二个查询:from_month,:from_day,:till_month,:till_day:

select month,
  case when day1 = 'P' 
       and not (month = :from_month and 1 < :from_day)
       and not (month = :till_month and 1 > :till_day)
  then 1 else 0 end +
  case when day2 = 'P'
       and not (month = :from_month and 2 < :from_day)
       and not (month = :till_month and 2 > :till_day)
  then 1 else 0 end +
  ...
  case when day31 = 'P'
       and not (month = :from_month and 31 < :from_day)
       and not (month = :till_month and 31 > :till_day)
  then 1 else 0 end as days
from mytable
where month between :from_month and :till_month;

但是更好地改变你的表格设计,所以查询会更简单,打字更少: - )