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));
sstan
  • 35,425
  • 6
  • 48
  • 66
Hendrien
  • 325
  • 1
  • 10
  • 20

1 Answers1

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;
sstan
  • 35,425
  • 6
  • 48
  • 66
Levesque
  • 874
  • 7
  • 6