ORA-02287: sequence number not allowed here

前端 未结 3 1035
遥遥无期
遥遥无期 2020-12-17 10:05

I am trying to select values from two tables and insert them into one table and calculate the number of placements in total per year. I keep getting an error saying sequence

相关标签:
3条回答
  • 2020-12-17 10:28

    Do it like this You can use the sequence like this, For the group by part i recommend you follow the other answer from @Tony INSERT INTO placement_cal ( cal_id.nextval , EXTRACT(YEAR FROM start_date) , count(placement_id) FROM placement group by year);

    INSERT INTO placement_cal (
    cal_id.nextval  , EXTRACT(YEAR FROM start_date) , count(placement_id)
    FROM placement_two
    group by year);
    
    0 讨论(0)
  • 2020-12-17 10:33

    You can get the reason in FAQ

    The following are the cases where you can't use a sequence:

    For a SELECT Statement:

    • In a WHERE clause
    • In a GROUP BY or ORDER BY clause
    • In a DISTINCT clause
    • Along with a UNION or INTERSECT or MINUS
    • In a sub-query
    0 讨论(0)
  • 2020-12-17 10:45

    This query raises the exception:

    SELECT cal_id.nextval  , EXTRACT(YEAR FROM start_date) , count(placement_id)
    FROM placement
    group by year;
    

    This is because you cannot select a sequence value in a query with a group by clause.

    Also, a group by clause must include all non-aggregate expressions from the select clause, which yours doesn't. I'm guessing that year is the alias for EXTRACT(YEAR FROM start_date), in which case this is the query you need:

    INSERT INTO placement_cal
    SELECT cal_id.nextval, year, cnt FROM
    ( SELECT EXTRACT(YEAR FROM start_date) year, count(placement_id) cnt
      FROM placement
      group by EXTRACT(YEAR FROM start_date)
    );
    
    0 讨论(0)
提交回复
热议问题