永洪社区
标题:
如何在数据集中直接生成一组数
[打印本页]
作者:
yhdata_x6S3fRoO
时间:
2024-7-19 09:29
标题:
如何在数据集中直接生成一组数
比如我想要一个近三年的年月数据 如2024.07
2024.06
2024.06
...
写内嵌数据集或者导入Excel不够动态,因为每个月都要更新最新的月份
能不能直接在数据集里用语句生成一个嘞
作者:
朝花夕拾
时间:
2024-7-19 09:29
试试这两个,我用gpt生成的
SELECT DISTINCT
TO_VARCHAR(ADD_MONTHS(CURRENT_DATE, - n), 'YYYY.MM') AS YearMonth
FROM
(
SELECT
(YEAR(CURRENT_DATE) - 3 + sequence_index) * 12 + month_index AS n
FROM
series_generate_integer(0, 11) AS month_index
CROSS JOIN series_generate_integer(0, 2) AS sequence_index
);
复制代码
SELECT DISTINCT
TO_VARCHAR(ADD_MONTHS(CURRENT_DATE, months_back), 'YYYY.MM') AS YearMonth
FROM
(
SELECT 0 AS months_back FROM DUMMY
UNION ALL SELECT 1 FROM DUMMY
UNION ALL SELECT 2 FROM DUMMY
UNION ALL SELECT 3 FROM DUMMY
UNION ALL SELECT 4 FROM DUMMY
UNION ALL SELECT 5 FROM DUMMY
UNION ALL SELECT 6 FROM DUMMY
UNION ALL SELECT 7 FROM DUMMY
UNION ALL SELECT 8 FROM DUMMY
UNION ALL SELECT 9 FROM DUMMY
UNION ALL SELECT 10 FROM DUMMY
UNION ALL SELECT 11 FROM DUMMY
) AS months_table
CROSS JOIN DUMMY;
复制代码
作者:
朝花夕拾
时间:
2024-7-19 09:40
Oracle:
SELECT TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), - (LEVEL - 1)), 'YYYY.MM') AS YearMonth
FROM DUAL
CONNECT BY LEVEL <= 36
ORDER BY YearMonth DESC;
复制代码
Mysql:
SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL n MONTH), '%Y.%m') AS YearMonth
FROM (
SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL
SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL
SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL
SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL
SELECT 30 UNION ALL SELECT 31 UNION ALL SELECT 32 UNION ALL SELECT 33 UNION ALL SELECT 34 UNION ALL
SELECT 35
) AS numbers
WHERE DATE_FORMAT(DATE_SUB(NOW(), INTERVAL n MONTH), '%Y-%m-01') >= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 36 MONTH), '%Y-%m-01')
ORDER BY YearMonth DESC;
复制代码
作者:
yhdata_x6S3fRoO
时间:
2024-7-19 11:50
朝花夕拾 发表于 2024-7-19 09:40
Oracle:
我是HANA, 这两个我试了都会报错~
欢迎光临 永洪社区 (https://club.yonghongtech.com/)
Powered by Discuz! X3.4