Skip to content
Advertisement

Split Row and Paste to Different Tables Based on Column

I have a table like this. Table is populated each time an order is complete. One order can have one or many compartments.

Please help write an SQL script that takes the above and splits it into two new tables like so:

Table 1

Table 2

I’ve tried using the DISTINCT command as suggested;

Which returns the error;

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

If the script can keep track of already processed rows so as to avoid duplication each time it runs, that would be the icing on the cake.

Advertisement

Answer

This is how I’d do:

Edit: Even so, I see your master table as a child table, from where you must create Header’s table. It’s enough. I mean your Table can be my TChilds table, and key can be OrderId + Plant. (I don’t know what means plant in this table)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement