Note that there are some explanatory texts on larger screens.

plurals
  1. POCounting a cell up per Objects
    primarykey
    data
    text
    <p>i got a problem once again :D a little info first: im trying to copy data from one table to an other table(structure is the same). now one cell needs to be incremented, beginns per group at 1 (just like a histroy).</p> <p>i have this table:</p> <pre><code>create table My_Test/My_Test2 ( my_Id Number(8,0), my_Num Number(6,0), my_Data Varchar2(100)); </code></pre> <p>(my_Id, my_Num is a nested PK)</p> <p>if i want to insert a new row, i need to check if the value in <code>my_id</code> already exists.<br> if this is true then i need to use the next <code>my_Num</code> for this Id. </p> <p>i have this in my Table:</p> <pre><code>My_Id My_Num My_Data 1 1 'test1' 1 2 'test2' 2 1 'test3' </code></pre> <p>if i add now a row for <code>my_Id</code> 1, the row would look like this: i have this in my Table:</p> <pre><code>My_Id My_Num My_Data 1 3 'test4' </code></pre> <p>this sounds pretty easy ,now i need to make it in a SQL and on SQL Server i had the same problem and i used this:</p> <pre><code>Insert Into My_Test (My_Id,My_Num,My_Data) SELECT my_Id, ( SELECT CASE ( CASE MAX(a.my_Num) WHEN NULL THEN 0 Else Max(A.My_Num) END) + b.My_Num WHEN NULL THEN 1 ELSE ( CASE MAX(a.My_Num) WHEN NULL THEN 0 Else Max(A.My_Num) END) + b.My_Num END From My_Test A where my_id = 1 ) ,My_Data From My_Test2 B where my_id = 1; </code></pre> <p>this Select gives null back if no Rows are found in the subselect</p> <p>is there a way so i could use max in the case? and if it give null back it should use 0 or 1?</p> <p>Edit: Im usung now this: </p> <pre><code>Insert INTO My_Test ( My_Id,My_Num,My_Data ) SELECT B.My_Id, ( SELECT COALESCE(MAX(a.My_Num),0) + b.my_Num FROM My_Test A Where a.My_Id = b.My_Id) ,b.My_Data FROM My_Test2 B WHERE My_Id = 1 </code></pre> <p>THX to Bharat and OMG Ponies </p> <p>greets<br> Auro</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload