2

I have an insert into statement and this statement will have a sub query where it gets all its information from. I just have one problem I have to use an primary key index which I created as a sequence. I just don't know how to insert a sequence with a sub query. Any help would be much appreciated. At the moment the insert into statement is not working but this is what I have so far.

INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
 VALUES( seq_data_demand_id2.nextval ,

      (SELECT d.name, COUNT(u.data_id) AS product_demands, 
      d.information AS dataplan_information
      FROM users u, data_plans d
      WHERE u.data_id = d.data_plan_id
      GROUP BY d.name,d.information));

1 Answer 1

4

You can just put the sequence into your select, easy peasy. (edit, whups, sorry, missed that you were grouping)

INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
select seq_data_demand_id2.nextval, ss.name, ss.product_demands, ss.dataplan_information
from(SELECT d.name, COUNT(u.data_id) AS product_demands, 
      d.information AS dataplan_information
      FROM users u, data_plans d
      WHERE u.data_id = d.data_plan_id
      GROUP BY d.name,d.information) ss;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.