1
votes

I am running this SQL on Oracle (Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production):

CREATE TABLE R_SEQUENCES_COUNT1
  (
    DS_ID NUMBER,
    LINE_TIME TIMESTAMP(6),
    DAY_ID  DATE GENERATED ALWAYS   AS (TRUNC(LINE_TIME)),
    HOUR_ID  DATE GENERATED ALWAYS  AS (TRUNC(LINE_TIME,'HH24')) ,
    MINUTE_ID DATE GENERATED ALWAYS AS (TRUNC(LINE_TIME,'MI')) ,
    SECOND_ID DATE GENERATED ALWAYS AS (LINE_TIME),
    R_ID       NUMBER,
    SEQUENCE_ID NUMBER
  )NOLOGGING
  TABLESPACE TWC_DATA_SPACE
  PARTITION BY LIST (DS_ID)
  SUBPARTITION BY LIST(DAY_ID)
  (PARTITION DS_ID_OTHER VALUES (DEFAULT) 
  (SUBPARTITION DS_ID_OTHER_DAY_ID_OTHER VALUES (DEFAULT)))

and get this ERROR:

Error at Command Line:8 Column:40
Error report:
SQL Error: ORA-54016: Invalid column expression was specified

what is wrong?

2

2 Answers

1
votes

as you want it as a date use:

SECOND_ID DATE GENERATED ALWAYS AS (cast(LINE_TIME as date)),

otherwise you could do :

SECOND_ID TIMESTAMP GENERATED ALWAYS AS (LINE_TIME + numtodsinterval(0, 'day')),

though in that case not sure why you'd want an exact copy of the column?

eg for the second one:

SQL> CREATE TABLE R_SEQUENCES_COUNT1
  2    (
  3      DS_ID NUMBER,
  4      LINE_TIME TIMESTAMP(6),
  5      DAY_ID  DATE GENERATED ALWAYS   AS (TRUNC(LINE_TIME)),
  6      HOUR_ID  DATE GENERATED ALWAYS  AS (TRUNC(LINE_TIME,'HH24')) ,
  7      MINUTE_ID DATE GENERATED ALWAYS AS (TRUNC(LINE_TIME,'MI')) ,
  8      SECOND_ID TIMESTAMP GENERATED ALWAYS AS (LINE_TIME+numtodsinterval(0, 'day')),
  9      R_ID       NUMBER,
 10      SEQUENCE_ID NUMBER
 11    )NOLOGGING
 12  /

Table created.

SQL> insert into  R_SEQUENCES_COUNT1 (ds_id, line_time) values (1, systimestamp);

1 row created.

SQL> @print_Table "select * from  R_SEQUENCES_COUNT1"
DS_ID                         : 1
LINE_TIME                     : 14-mar-2013 09:56:31.104921
DAY_ID                        : 14-mar-2013 00:00:00
HOUR_ID                       : 14-mar-2013 09:00:00
MINUTE_ID                     : 14-mar-2013 09:56:00
SECOND_ID                     : 14-mar-2013 09:56:31.104921
R_ID                          :
SEQUENCE_ID                   :
-----------------
0
votes

In your creation

//set condition is invalid
SECOND_ID DATE GENERATED ALWAYS AS (LINE_TIME)



// change to this...
SECOND_ID DATE GENERATED ALWAYS AS (TIMESTAMP)

The data type TIMESTAMP allows for fractions of seconds. If you convert it to a DATE the fractional seconds will be removed - e.g.

SO IN SELECTING SECONDS..

select cast(systimestamp as date) from dual;

Another way i think is...

is to TRUNC() a timestamp to seconds you can cast it to a date

CAST( timestamp AS DATE)

then perform the TRUNC's as like:

TRUNC(CAST(timestamp AS DATE), 'YEAR')