sql - 来自年份和日期列的年龄 Netezza sql
问题描述
我有像 ID 这样的数据元素;开始日期; 出生年。我想根据 AGE 列中所示的birth_year 列和 start_date 计算 Age。
我只能想到用'-01-01'填充出生年份,然后使用该列计算年龄。
在 Netezza SQL 中是否有另一种从出生年份和开始日期获取年龄的有效方法?
解决方案
使用extract SQL 函数从 start_date 获取年份并计算年龄
例如
select
start_date,
birth_year,
-- assuming birth_year is an integer
extract(year from start_date) - birth_year as age
from ...
会给
START_DATE | BIRTH_YEAR | AGE
------------+------------+-----
2000-01-15 | 1985 | 15
2010-06-20 | 1990 | 20
2005-12-15 | 1992 | 13
2007-08-17 | 1998 | 9
2020-09-15 | 1999 | 21
(5 rows)
获取更详细年龄的另一种方法是使用Netezza 'age' SQL 函数-
select
start_date,
birth_year,
age(start_date, to_date(birth_year, 'YYYY'))
from...
这将导致
START_DATE | BIRTH_YEAR | AGE
------------+------------+--------------------------
2000-01-15 | 1985 | 15 years 14 days
2010-06-20 | 1990 | 20 years 5 mons 19 days
2005-12-15 | 1992 | 13 years 11 mons 14 days
2007-08-17 | 1998 | 9 years 7 mons 16 days
2020-09-15 | 1999 | 21 years 8 mons 14 days
(5 rows)
推荐阅读
- android - 如何使用 Retrofit 将这个 Api URL 映射到“?”之后的这些“{parameter}” 询问
- react-native - 带有垂直标题的水平平面列表
- c# - 如何在 C# 客户端中检测 SignalR 断开连接
- mysql - MySQL在插入之前触发更改主ID
- hive - 从 HIVE 表中选择时如何替换换行符
- android - 在 Android 应用中,Google 登录弹出窗口未加载
- bash - find + sed 仅在不存在时将行附加到文件的开头
- node.js - 有没有更好的方法在 swagger-tools API 中实现 OAuth 安全性?
- php - 在使用模板文件时,functions.php 中的查询不返回帖子
- vb.net - 我的代码适用于大多数但不是所有行