on 2014 May 17 7:00 PM
I have a table that stores a structure. With the main material and component. The components are numbered.
Now I want to insert two components in the end of all components per main material in the table. These two new components are similar for all materials. How do I do this the best way?
Ex:
Material----Component----RowRn---- ---------------------------------- Label2------EVO_121---------1----- Label2------EVO_243---------2----- Label2------EVO_638---------3----- Label3------EVO_311---------1----- Label3------EVO_120---------2----- Label3------EVO_081---------3----- Label3------EVO_987---------4-----
The two new components are NewComp1 and NewComp2 I want it to loo like this:
Material----Component----RowRn---- ---------------------------------- Label2------EVO_121---------1----- Label2------EVO_243---------2----- Label2------EVO_638---------3----- Label2------NewComp1--------4----- Label2------NewComp2--------5----- Label3------EVO_311---------1----- Label3------EVO_120---------2----- Label3------EVO_081---------3----- Label3------EVO_987---------4----- Label3------NewComp1--------5----- Label3------NewComp2--------6-----
Request clarification before answering.
CREATE TABLE component ( Material VARCHAR ( 10 ), Component VARCHAR ( 10 ), RowRn INTEGER ); INSERT component VALUES ( 'Label2', 'EVO_121', 1 ); INSERT component VALUES ( 'Label2', 'EVO_243', 2 ); INSERT component VALUES ( 'Label2', 'EVO_638', 3 ); INSERT component VALUES ( 'Label3', 'EVO_311', 1 ); INSERT component VALUES ( 'Label3', 'EVO_120', 2 ); INSERT component VALUES ( 'Label3', 'EVO_081', 3 ); INSERT component VALUES ( 'Label3', 'EVO_987', 4 ); COMMIT; INSERT component SELECT Material, 'NewComp1' AS Component , MAX ( RowRn ) + 1 AS RowRn FROM component GROUP BY Material; INSERT component SELECT Material, 'NewComp2' AS Component , MAX ( RowRn ) + 1 AS RowRn FROM component GROUP BY Material; COMMIT; SELECT Material, Component, RowRn FROM component ORDER BY Material, RowRn; Material Component RowRn ---------- ---------- ----------- Label2 EVO_121 1 Label2 EVO_243 2 Label2 EVO_638 3 Label2 NewComp1 4 Label2 NewComp2 5 Label3 EVO_311 1 Label3 EVO_120 2 Label3 EVO_081 3 Label3 EVO_987 4 Label3 NewComp1 5 Label3 NewComp2 6
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The code is almost the same (see revised answer)... MAX plus 1 is now used for both INSERT statements because they are separate statements. Beware code like this, that it isn't accidentally rerun... either that, or add WHERE clauses to prevent problems if the NewComp rows already exist. Also understand that the SELECT part of an INSERT SELECT does not "see" any of the rows that the INSERT is inserting... I forget the technical word used to describe this behavior, but in the ancient language spoken by The People it is SQL-Anywhere-Does-Things-The-Way-They-Should-Be-Done.
Why would you check if component='NewComp1' and component='NewComp2' are the second last and last row with respect to RowRn? Isn't it enough to check if the Components exist at all for the Material?
What would be the result if the components NewComp1 and/or NewComp2 exist for a Material? Would you add the components anyway? Would you move the component(s) to the end?
What would be the result if NewComp1 is the last one and NewComp2 is the second last one, means, the Components are last for the Material but not in correct order?
What is the primary key?
First read this: http://dcx.sybase.com/1100/en/dbreference_en11/insert-statement.html
Then this MAY be a solution for you (it sorts existing NewCompN to the end):
insert into MaterialComponent
on existing update
select Material, 'NewComp1', 1+max(RowRn)
from MaterialComponent
where Component not in ('NewComp1', 'NewComp2')
group by Material
;
insert into MaterialComponent
on existing update
select Material, 'NewComp2', 1+max(RowRn)
from MaterialComponent
where Component <> 'NewComp2'
group by Material
;
commit
;
@Breck: Sorry, I know I must not do this, avoiding your attempt to bring this learner on the right path. But apparently a very strange variation of "Heartbleed" has attacked my mind so I coudn't resist. 😉
@Chris: Nice play on "heartbleed" 🙂
Without knowing what the primary key is, it's hard to tell how SQL Anywhere will know if a row is "existing" or not, and which column(s) will be subject to the "update" part of "on existing update"... maybe MERGE will help.
Plus testing... results can be shocking surprising when GROUP BY is involved.
User | Count |
---|---|
46 | |
6 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.