简体   繁体   中英

(ORACLE) (SQL) run a function on a sql query with a temporary view and aggregate functions

I have this sql code:

WITH
    test (start_date, end_date)
    AS
        (
            SELECT MIN(date),
                   MAX(date)
                   FROM table
        )
    SELECT start_date + LEVEL - 1 AS "DATE"
    FROM test
CONNECT BY LEVEL <= end_date - start_date + 1
    ORDER BY "DATE"

;

My question is I would like to run a function alongside the above returning a column of dates. The function will generate week numbers. Can't seem to figure out how to make a added field/column to the above sql query. If the sql function seems a bit vague then say eg I want to add a column to the above query how can I do that.

The final table should look like the below:

DATE           |    WEEK_NUMBER
01-JAN_22      |             1
02-JAN-22      |             1

and so on...

Just add to_char function call with appropriate format model (line #6)

SQL> WITH
  2     test (id, start_date, end_date)
  3     AS
  4        (SELECT 1, DATE '2022-02-28', DATE '2022-03-22' FROM DUAL)
  5      SELECT start_date + LEVEL - 1 datecolumn,
  6             TO_CHAR (start_date + LEVEL - 1, 'iw') week         --> here
  7        FROM test
  8  CONNECT BY LEVEL <= end_date - start_date + 1
  9    ORDER BY datecolumn;

DATECOLUMN WEEK
---------- -----
28.02.2022 09
01.03.2022 09
02.03.2022 09
03.03.2022 09
04.03.2022 09
05.03.2022 09
06.03.2022 09
07.03.2022 10
08.03.2022 10
09.03.2022 10
10.03.2022 10
11.03.2022 10
12.03.2022 10
13.03.2022 10
14.03.2022 11
15.03.2022 11
16.03.2022 11
17.03.2022 11
18.03.2022 11
19.03.2022 11
20.03.2022 11
21.03.2022 12
22.03.2022 12

23 rows selected.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM