0

I have the following table:

Table1

And all I want to do is add two new rows per PersonID i.e.

Table 2

These are just small snip bits from the actual table, as this table contains large amount of data.

Any help would be greatly appreciated.

8
  • so essentially you want to duplicate each row based on personID? Commented Nov 29, 2016 at 12:25
  • 1
    What is the logic? Commented Nov 29, 2016 at 12:25
  • It is completely unlear what you're asking about. Please describe what are the rules which define adding rows (by the way in question you wrote columns) to table. Commented Nov 29, 2016 at 12:27
  • Please also tag your question to make clear what DBMS you are using. Commented Nov 29, 2016 at 12:30
  • 1
    Any help would be greatly appreciated. ... Any explanation about the rules for adding data would also be appreciated. Commented Nov 29, 2016 at 12:35

2 Answers 2

1

Use INSERT?

INSERT INTO yourTable (ID, PersonID, JobID, Verified)
SELECT NULL, PersonID, MAX(JobID) + 1, 0
FROM yourTable
GROUP BY PersonID
UNION ALL
SELECT NULL, PersonID, MAX(JobID) + 2, 0
FROM yourTable
GROUP BY PersonID

Explanation:

Inserting NULL for the ID column should force the database to assign the next sequence, assuming that ID be an autoincrement column. If it is not, then maybe consider changing this. For each of the new records, the JobID sequence is just continued, hence my use of n+1 and n+2. And you seem to be using zero as the default value of Verified, so this was hard coded.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for everyone's replies/comments.

I managed to resolve my question by using a CURSOR (see below). It's not brilliant but it does the job.

enter image description here

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.