tags:

views:

172

answers:

1

Hello everyone

We have a table that is used for assessment-values in our tool where each value has its valueid. Dont ask me who came up with this so called design, but now I need to add multiple valueids to the end for each assessment.

So, it looks like this:

AssessmentId 1, ValueId 1, Value
AssessmentId 1, ValueId 2, Value
AssessmentId 1, ValueId 3, Value
AssessmentId 2, ValueId 1, Value
AssessmentId 2, ValueId 2, Value
AssessmentId 2, ValueId 3, Value

I now probably need a subquery that returns all distinct assessmentids and for each of those I add my rows

AssessmentId 1, ValueId 7, empty
AssessmentId 1, ValueId 8, empty
AssessmentId 1, ValueId 9, empty
AssessmentId 2, ValueId 7, empty
AssessmentId 2, ValueId 8, empty
AssessmentId 2, ValueId 9, empty
...

Honestly, I have no clue how to achieve this. Maybe a subselect with a group, but how would I then get the selected assessmentid into the new insert?

Edit: well, actually we have 709 values for each assessment and now we need 24 more at the end for additional data

foreach distinct assessmentid in table
{ 
   insert row (assessmentid, 710, null); 
   insert row (assessmentid, 711, null); 
   ... 
   insert row (assessmentid, 733, null); 
}

something like this

+3  A: 
INSERT
INTO    i_should_really_name_the_tables_i_ask_about_on_stackoverflow_com (assessmentid, valueid)
SELECT  assessmentId, valueMax + lvl
FROM    (
        SELECT  assessmentId, MAX(valueId) AS valueMax
        FROM    i_should_really_name_the_tables_i_ask_about_on_stackoverflow_com t
        GROUP BY
                assessmentId
        ) a
CROSS JOIN
        (
        SELECT  level AS lvl
        FROM    dual
        CONNECT BY
                level <= 24
        ) l
Quassnoi
thanks for the quick reply but it seems not quite what I need. I need something where I dont have to specify the assessmentid. I need to add about 24 rows with new valuedids for each assessment. This script then has to run on production data later as well :)
lostiniceland
You have 2 id's and 3 values per id. How do you get 24? Please specify the algorithm for inserting new rows.
Quassnoi
I added an algorithm-like code to the question
lostiniceland
See post update
Quassnoi
awesome. tahnks a lot
lostiniceland