如何在oracle sql count中使用计算值

ztmd8pv5  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(420)

在我的视图中有一个名为birth\u date的列。我想用这一栏和今天的日期来计算年龄,并计算年龄段中的员工人数。例如:40至49岁的员工等。
在纯sql语句中的其他答案也很受欢迎。
我的代码如下:

$count = StaffEmploymentListView::find()
        ->select([
            'COUNT(*) AS TEACHING_COUNT', 
            'BIRTH_DATE' => 'BIRTH_DATE'
        ])
        ->where(['GENDER' => 'MALE', 'JOB_CADRE' => 'ACADEMIC'])
        ->andFilterHaving(['>=', "TRUNC(months_between(sysdate, BIRTH_DATE) / 12)", 40])
        ->andFilterHaving(['<', "TRUNC(months_between(sysdate, BIRTH_DATE) / 12)", 70])
        ->groupBy(['BIRTH_DATE'])
        ->all();

我计算的年龄如下所示:

TRUNC(months_between(sysdate, BIRTH_DATE) / 12)

完成此操作后,如果尝试访问模型中的教学计数变量,则该变量为空。

py49o6xq

py49o6xq1#

您可以计算年龄段之间的记录数,而无需编写任何代码 SQL 也。

$from = date('Y-m-d', strtotime('-49 years'));
$to = date('Y-m-d', strtotime('-40 years'));
$query = StaffEmploymentListView::find()
        ->where(['>=', 'BIRTH_DATE', $from])
        ->andWhere(['<=', 'BIRTH_DATE', $to]);

// You can get the count
$count = $query->count();

// Or use the query to display the results
echo Html::tag('h1',
    'Found ' . $query->count() . ' clients born between ' .
    Yii::$app->formatter->asDate($from) . ' and ' . 
    Yii::$app->formatter->asDate($to)
);
$dataProvider = new ActiveDataProvider([
    'query' => $query
]);

echo GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'name',
            'BIRTH_DATE:date', 
            [
                'attribute' => 'BIRTH_DATE',
                'value' => static function($model) {
                    return floor((time() - strtotime($model->BIRTH_DATE)) / 31556926);
                },
                'label' => 'Age'
            ]
        ]
]);

既然您已经在使用yii,那么直接使用它的方法似乎是有意义的。在本例中是count()方法。

bsxbgnwa

bsxbgnwa2#

我将用sql示例来回答。我不知道上面的代码在php中是如何工作的,但是由于按“出生日期”分组,每个年龄段会有多行,返回时可能会出现问题。
如果我知道您需要的是sum over count(*),但是如果查询没有在任何地方重复使用,并且您不需要count per birth\u date,那么您可以从查询构造中删除group by。
见下文,

create table StaffEmployment
  ( name varchar2(100)
  , gender varchar2(10)
  , job_cadre varchar2(50)
  , birth_date date
  );
----------------------------------------------------------------------  
insert all
  into staffemployment(name,gender,job_cadre,birth_date)
  values('A','M','Acdemic',sysdate-(45*365))
  into staffemployment(name,gender,job_cadre,birth_date)
  values('B','M','Acdemic',sysdate-(45*365))
  into staffemployment(name,gender,job_cadre,birth_date)
  values('C','F','Acdemic',sysdate-(70*365))
  into staffemployment(name,gender,job_cadre,birth_date)
  values('D','F','Acdemic',sysdate-(10*365))
SELECT * FROM dual;
----------------------------------------------------------------------  
select t.*,TRUNC(months_between(sysdate, BIRTH_DATE) / 12) age
 from StaffEmployment t;
----------------------------------------------------------------------  
NAME   GENDER     JOB_CADRE  BIRTH_DAT        AGE
----- ----------  ---------- --------- ----------
A      M          Acdemic    13-AUG-75         44
B      M          Acdemic    13-AUG-75         44
C      F          Acdemic    19-AUG-50         69
D      F          Acdemic    04-AUG-10          9
---------------------------------------------------------------------- 
select BIRTH_DATE,count(*)
  from StaffEmployment t
 where TRUNC(months_between(sysdate, BIRTH_DATE) / 12) >= 40 
   and TRUNC(months_between(sysdate, BIRTH_DATE) / 12) < 70
 group by BIRTH_DATE;
----------------------------------------------------------------------  
BIRTH_DAT   COUNT(*)
--------- ----------
13-AUG-75          2
19-AUG-50          1
----------------------------------------------------------------------  
And if we do a sum you get the actual count over all rows
----------------------------------------------------------------------  
select SUM(count(*)) teacing_count
  from StaffEmployment t
 where TRUNC(months_between(sysdate, BIRTH_DATE) / 12) >= 40 
   and TRUNC(months_between(sysdate, BIRTH_DATE) / 12) < 70
 group by BIRTH_DATE;
---------------------------------------------------------------------- 
TEACING_COUNT
-------------
            6

相关问题