年月日总和 - Oracle SQL
Sum year, month and day - Oracle SQL
一年=365天,一个月=31天
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 10 as month, 25 as day from dual
需要结果为:
years month day
3 4 4
我们可以根据条件使用一些算法来计算天数
one year = 365 days
one month = 31 days
- 年份:(年 * 365 + 月 * 31 + 日)除以 365 得到数字的总年份。
- Month : (month * 31 + day) 除以 31 得到总月数,但我们需要从中得到 mod 因为如果总月数大于 12 则将被携带.
作为这个查询。
select CAST(SUM((year * 365 + month * 31 + day) / 365) AS INT) years ,
CAST(MOD(SUM(month * 31 + day)/31, 12) AS INT) month,
MOD(SUM(day) , 31) day
from
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 10 as month, 25 as day from dual
) t1;
如果 1 年 = 365 天,1 个月 = 31 天,则:
SELECT TRUNC(SUM(year*365 + month*31 + day)/365) AS years,
TRUNC(MOD(SUM(month*31 + day), 365)/31) AS month,
MOD(MOD(SUM(month*31 + day), 365), 31) AS day
FROM
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 13 as month, 25 as day from dual
) t1;
输出:
YEARS
MONTH
DAY
3
7
11
如果 1 年 = 12 个月,1 个月 = 31 天,那么您需要:
SELECT TRUNC(SUM(year + month/12 + day/31/12)) AS years,
TRUNC(MOD(SUM(month + day/31), 12)) month,
MOD(SUM(day) , 31) day
FROM
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 13 as month, 25 as day from dual
) t1;
输出:
YEARS
MONTH
DAY
3
7
4
db<>fiddle here
一年=365天,一个月=31天
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 10 as month, 25 as day from dual
需要结果为:
years month day
3 4 4
我们可以根据条件使用一些算法来计算天数
one year = 365 days
one month = 31 days
- 年份:(年 * 365 + 月 * 31 + 日)除以 365 得到数字的总年份。
- Month : (month * 31 + day) 除以 31 得到总月数,但我们需要从中得到 mod 因为如果总月数大于 12 则将被携带.
作为这个查询。
select CAST(SUM((year * 365 + month * 31 + day) / 365) AS INT) years ,
CAST(MOD(SUM(month * 31 + day)/31, 12) AS INT) month,
MOD(SUM(day) , 31) day
from
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 10 as month, 25 as day from dual
) t1;
如果 1 年 = 365 天,1 个月 = 31 天,则:
SELECT TRUNC(SUM(year*365 + month*31 + day)/365) AS years,
TRUNC(MOD(SUM(month*31 + day), 365)/31) AS month,
MOD(MOD(SUM(month*31 + day), 365), 31) AS day
FROM
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 13 as month, 25 as day from dual
) t1;
输出:
YEARS MONTH DAY 3 7 11
如果 1 年 = 12 个月,1 个月 = 31 天,那么您需要:
SELECT TRUNC(SUM(year + month/12 + day/31/12)) AS years,
TRUNC(MOD(SUM(month + day/31), 12)) month,
MOD(SUM(day) , 31) day
FROM
(
Select 1 as year, 5 as month, 10 as day from dual
union all
Select 1 as year, 13 as month, 25 as day from dual
) t1;
输出:
YEARS MONTH DAY 3 7 4
db<>fiddle here