将季节和年份列表创建为常数值

时间:2019-04-08 20:27:08

标签: sql oracle

我想生成当前年份和未来3年的季节和年份列表,即(FA2019WI2019SP2019SU2019)。然后,它将被送入WHERE子句中的另一个查询。

现在每个季度我有4条语句,我只是UNION一起使用。有更短的方法吗?

SELECT 'FA' || YYYY AS SSYYYY FROM (
SELECT to_number(to_char(SYSDATE, 'YYYY'))+LEVEL-1 AS YYYY FROM dual
CONNECT BY LEVEL <= 4)

UNION

SELECT 'WI' || YYYY AS SSYYYY FROM (
SELECT to_number(to_char(SYSDATE, 'YYYY'))+LEVEL-1 AS YYYY FROM dual
CONNECT BY LEVEL <= 4)

UNION

SELECT 'SP' || YYYY AS SSYYYY FROM (
SELECT to_number(to_char(SYSDATE, 'YYYY'))+LEVEL-1 AS YYYY FROM dual
CONNECT BY LEVEL <= 4)

UNION

SELECT 'SU' || YYYY AS SSYYYY FROM (
SELECT to_number(to_char(SYSDATE, 'YYYY'))+LEVEL-1 AS YYYY FROM dual
CONNECT BY LEVEL <= 4)

3 个答案:

答案 0 :(得分:4)

您可以使用_Generic两个CROSS JOIN查询,例如:

CONNECT BY

返回:

| SSYYYY |
| :----- |
| FA2019 |
| SP2019 |
| SU2019 |
| WI2019 |
| FA2020 |
| SP2020 |
| SU2020 |
| WI2020 |
| FA2021 |
| SP2021 |
| SU2021 |
| WI2021 |
| FA2022 |
| SP2022 |
| SU2022 |
| WI2022 |

Demo on DB Fiddle

答案 1 :(得分:3)

"dependencies": { "@angular/animations": "~7.2.0", "@angular/common": "~7.2.0", "@angular/compiler": "~7.2.0", "@angular/core": "~7.2.0", "@angular/forms": "~7.2.0", "@angular/platform-browser": "~7.2.0", "@angular/platform-browser-dynamic": "~7.2.0", "@angular/router": "~7.2.0", "@ngrx/core": "github:ngrx/core", "@ngrx/store": "github:ngrx/store", "core-js": "^2.5.4", "cors": "^2.8.5", "ngrx": "^2.0.1", "rxjs": "^6.4.0", "rxjs-compat": "^6.4.0", "tslib": "^1.9.0", "zone.js": "~0.8.26" }, "devDependencies": { "@angular-devkit/build-angular": "~0.13.0", "@angular/cli": "~7.3.1", "@angular/compiler-cli": "~7.2.0", "@angular/language-service": "~7.2.0", "@types/node": "~8.9.4", "@types/jasmine": "~2.8.8", "@types/jasminewd2": "~2.0.3", "codelyzer": "~4.5.0", "jasmine-core": "~2.99.1", "jasmine-spec-reporter": "~4.2.1", "karma": "~3.1.1", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "~2.0.1", "karma-jasmine": "~1.1.2", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.4.0", "ts-node": "~7.0.0", "tslint": "~5.11.0", "typescript": "~3.2.2" } } cross join一起使用

connect by level <= 4

Demo

答案 2 :(得分:2)

尝试以下操作:

   SELECT DECODE(MOD(level, 4), 1, 'FA', 2, 'WI', 3, 'SP', 'SU') ||   
          TO_CHAR(EXTRACT (YEAR FROM SYSDATE) + FLOOR((LEVEL-1) / 4))
     FROM DUAL 
  CONNECT BY LEVEL <= 16;