如何计算Postgresql中两个YYYYMM整数值之间的月数?
DATA:
| Date1 | Date2 |
|--------|--------|
| 201608 | 201702 |
| 201609 | 201610 |
期望的输出:
| Date1 | Date2 | MonthsBetweenInclusive | MonthsBetweenExclusive |
|--------|--------|------------------------|------------------------|
| 201608 | 201702 | 7 | 6 |
| 201609 | 201610 | 2 | 1 |
我查看了PostgreSQL日期函数文档,但是我找不到一个对YYYYMM整数值进行操作的解决方案。
答案 0 :(得分:2)
with t(d1,d2) as (values(201608,201702),(201609,201610))
select
*,
((d2/100*12)+(d2-d2/100*100))-((d1/100*12)+(d1-d1/100*100))
from t;
答案 1 :(得分:1)
with t (date1, date2) as (values
(201608,201702),(201609,201610)
)
select array_length(
array((
select generate_series(
to_date(date1::text, 'YYYYMM'),
to_date(date2::text, 'YYYYMM'),
'1 month'
)
)), 1
)
from t
;
array_length
--------------
7
2
答案 2 :(得分:1)
还有更多方法 - 什么是正确的,取决于你的情况:
select extract( months from (justify_interval(to_timestamp('201610','YYYYMM') -
to_timestamp('201609','YYYYMM'))));
┌───────────┐
│ date_part │
╞═══════════╡
│ 1 │
└───────────┘
(1 row)
或
CREATE OR REPLACE FUNCTION month_number(date)
RETURNS int AS $$
SELECT ((EXTRACT(year FROM $1) - 1900) * 12 +
EXTRACT(month FROM $1))::int
$$ LANGUAGE sql;
SELECT month_number(to_date('201702','YYYYMM')) -
month_number(to_date('201608','YYYYMM'));
┌──────────┐
│ ?column? │
╞══════════╡
│ 6 │
└──────────┘
(1 row)
或
SELECT (to_date('201702','YYYYMM') -
to_date('201608','YYYYMM'))/30;
┌──────────┐
│ ?column? │
╞══════════╡
│ 6 │
└──────────┘
(1 row)
答案 3 :(得分:0)
Fatal error: Uncaught exception 'CKSource\CKFinder\Exception\InvalidConfigException' with message 'The temporary folder is not writable for CKFinder' in /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/Config.php:330 Stack trace: #0 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/Config.php(73): CKSource\CKFinder\Config->validate() #1 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/CKFinder.php(91): [error] 30729#0: *100469095 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught exception 'CKSource\CKFinder\Exception\InvalidConfigException' with message 'The temporary folder is not writable for CKFinder' in /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/Config.php:330
Stack trace:
#0 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/Config.php(73): CKSource\CKFinder\Config->validate()
#1 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/CKFinder.php(91): CKSource\CKFinder\Config->__construct('/home/stroylocm...')
#2 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/pimple/pimple/src/Pimple/Container.php(113): CKSource\CKFinder\CKFinder->CKSource\CKFinder\{closure}(Object(CKSource\CKFinder\CKFinder))
#3 /home/stroylocman/www/htdocs/ckfinder/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/CKFinder.php(185): Pimple\Container->offsetGet('config')
答案 4 :(得分:0)
select yt.date1,
yt.date2,
trunc(EXTRACT(EPOCH from age(to_timestamp(yt.date2::TEXT, 'YYYYMM'),to_timestamp(yt.date1::TEXT, 'YYYYMM')))/(3600*24*30)),
trunc(EXTRACT(EPOCH from age(to_timestamp(yt.date2::TEXT, 'YYYYMM'),to_timestamp(yt.date1::TEXT, 'YYYYMM')))/(3600*24*30)) +1
from your_table yt