Hi,
I have a situation where i have multiple batch and the each batch are
sequence numbers. For each batch, the number should start with 1.
For example:
Col.no1 Col.no2
------ -------
batch1 1
batch1 2
batch1 3
batch 2 1
batch 2 2
batch 3 1
batch4 1
..... .....
Is there a easy way to do it in DB2 while creating a table?
Identity column in DB2 simply creates numbers in sequence.
Please advise
Jignesh 12 3042 to***********@gmail.com wrote: Hi,
I have a situation where i have multiple batch and the each batch are sequence numbers. For each batch, the number should start with 1. For example:
Col.no1 Col.no2 ------ ------- batch1 1 batch1 2 batch1 3
batch 2 1 batch 2 2
batch 3 1
batch4 1 ..... .....
Is there a easy way to do it in DB2 while creating a table?
Identity column in DB2 simply creates numbers in sequence.
Please advise Jignesh
Are your batches finite? You could create a sequence/batch and write a
function doing dynamic SQL to use the appropriate sequence.
Otherwise you could use thr ROW_NUMBER() OVER(PARTITION BY
<batchcolumn>) OLAP function possibly offset by the max number for the
batch before the insert.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Will the batches ever have items removed? If they are, will they need
reordering?
You may need a FUNCTION to reorder everything for you. If gaps in the
sequence to not matter, a SEQEUNCE can be used.
B.
Hi Serge,
Nopes, my batch are infinite. I created a sequence but it just
increments the sequence number even after a having a new batch.
Please advise.
Jignesh Are your batches finite? You could create a sequence/batch and write a function doing dynamic SQL to use the appropriate sequence.
Otherwise you could use thr ROW_NUMBER() OVER(PARTITION BY <batchcolumn>) OLAP function possibly offset by the max number for the batch before the insert.
Cheers Serge
-- Serge Rielau DB2 Solutions Development IBM Toronto Lab
Can someone show some example of inserting records with sequence number
in a batch?
regards
Jignesh to***********@gmail.com wrote: Hi Serge,
Nopes, my batch are infinite. I created a sequence but it just increments the sequence number even after a having a new batch.
Please advise. Jignesh
Are your batches finite? You could create a sequence/batch and write a function doing dynamic SQL to use the appropriate sequence.
Otherwise you could use thr ROW_NUMBER() OVER(PARTITION BY <batchcolumn>) OLAP function possibly offset by the max number for the batch before the insert.
Cheers Serge
-- Serge Rielau DB2 Solutions Development IBM Toronto Lab to***********@gmail.com wrote: Can someone show some example of inserting records with sequence number in a batch?
How does your batch processing look like. Can you paraphrase with pseudo
code?
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Serge,
Here's the description of table.
Table: receipt_check
c1: receipt_id char(11) not null ,
c2: receipt_seq int not null generated by default as identity (start
with 1, increment with 1, .........),
c3: receipt_dt timestamp,
c4: receipt_user_nm varchar(35)
Records in the table receipt_check looks something like below:
c1 c2
--- -----
111-22-3333 1
111-22-3333 2
111-33-4444 3
111-33-4444 4
111-33-4444 5
222-44-5555 6
I want to create a new column called c5 (for grouping c1 and c2). And
the table/records should display something like below:
c1 c2
c5 (new column)
--- -----
--------------------
111-22-3333 1
1
111-22-3333 2
2
111-33-4444 3
1
111-33-4444 4
2
111-33-4444 5
3
222-44-5555 6
1
Any help is greatly appreciated.
regards
Jignesh to***********@gmail.com wrote: Serge,
Here's the description of table.
Table: receipt_check
c1: receipt_id char(11) not null , c2: receipt_seq int not null generated by default as identity (start with 1, increment with 1, .........), c3: receipt_dt timestamp, c4: receipt_user_nm varchar(35)
Records in the table receipt_check looks something like below:
c1 c2 --- ----- 111-22-3333 1 111-22-3333 2 111-33-4444 3 111-33-4444 4 111-33-4444 5 222-44-5555 6
I want to create a new column called c5 (for grouping c1 and c2). And the table/records should display something like below:
c1 c2 c5 (new column) --- ----- -------------------- 111-22-3333 1 1 111-22-3333 2 2 111-33-4444 3 1 111-33-4444 4 2 111-33-4444 5 3 222-44-5555 6 1
OK, this will populate your new column:
UPDATE (SELECT c5, row_number(partition by c1 order by c2) as rn
FROM receipt_check) AS U
SET c5 = rn;
To maintain it (I assume you only do INSERT....) you will need:
CREATE TRIGGER trg1 BEFORE INSERT ON receipt_check
REFERENCING NEW AS n FOR EACH ROW
SET n.c5 = (SELECT COUNT(1) + 1 FROM receipt_check WHERE n.c1 = c1)
assuming single row insert. For multi row insert you will need to
replace +1 with the scratchpad counter function in sqllib/samples/c
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Hi Serge,
I am little confused so let me display the entire scenario one more
time.
Here's real scenario.
I have table called 'validation' in one database. And the columns are
as follows:
receipt_id char(11) not null,
receipt_seq integer not null,
payee_num char(5),
payment_amt decimal(16),
email_addr varchar(35),
payee_addr1 varchar(35),
payee_addr2 varchar(35),
payee_addr3 varchar(30)
The table validation already has data and it looks something like this:
receipt_id receipt_seq payee_num
------------- --------------- -------------
001100AB 1 12341
001100AB 1 23232
001100AB 1 65432
001100BA 1 23424
001100BA 1 64352
001100BB 1 23627
001100BB 1 90201
002130CC 1 89292
002530DA 1 67782
Now, i wanted to load the data from table validation into a new table
called validation_new with new column (receipt_num) in the new table
but in another database.
The new column (receipt_num) in a new table (validation_new) will look
like this:
receipt_id char(11) not null,
receipt_seq integer not null,
receipt_num integer not null,
payee_num char(5),
payment_amt decimal(16),
email_addr varchar(35),
payee_addr1 varchar(35),
payee_addr2 varchar(35),
payee_addr3 varchar(30)
And the record should look something like this:
receipt_id receipt_seq receipt_num payee_num
------------- --------------- --------------- -------------
001100AB 1 1 12341
001100AB 1 2 23232
001100AB 1 3 65432
001100BA 1 1 23424
001100BA 1 2 64352
001100BB 1 1 23627
001100BB 1 2 90201
002130CC 1 1 89292
002530DA 1 1 67782
So, how to load the data from the flat file with new receipt_num?
Any help would be of greatly appreciated.
Thanks
Jignesh
Serge Rielau wrote: to***********@gmail.com wrote: Serge,
Here's the description of table.
Table: receipt_check
c1: receipt_id char(11) not null , c2: receipt_seq int not null generated by default as identity (start with 1, increment with 1, .........), c3: receipt_dt timestamp, c4: receipt_user_nm varchar(35)
Records in the table receipt_check looks something like below:
c1 c2 --- ----- 111-22-3333 1 111-22-3333 2 111-33-4444 3 111-33-4444 4 111-33-4444 5 222-44-5555 6
I want to create a new column called c5 (for grouping c1 and c2). And the table/records should display something like below:
c1 c2 c5 (new column) --- ----- -------------------- 111-22-3333 1 1 111-22-3333 2 2 111-33-4444 3 1 111-33-4444 4 2 111-33-4444 5 3 222-44-5555 6 1 OK, this will populate your new column: UPDATE (SELECT c5, row_number(partition by c1 order by c2) as rn FROM receipt_check) AS U SET c5 = rn;
To maintain it (I assume you only do INSERT....) you will need: CREATE TRIGGER trg1 BEFORE INSERT ON receipt_check REFERENCING NEW AS n FOR EACH ROW SET n.c5 = (SELECT COUNT(1) + 1 FROM receipt_check WHERE n.c1 = c1)
assuming single row insert. For multi row insert you will need to replace +1 with the scratchpad counter function in sqllib/samples/c
Cheers Serge -- Serge Rielau DB2 Solutions Development IBM Toronto Lab
Simply import into the new table and set the column to a default value.
Then run the UPDATE statement I provided to populate the column.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Hi Serge,
Appreciate all your kind replies. It looks simple, but still i m facing
some problem while importing data. From the previous mail, I have created a new column with 'default 0'.
While i import from flat file (of del), the first row is getting
inserted, while other records are rejected by violating primary key
constraints and getting SQL0803N.
Any idea wat is causing this?
thanks
Jignesh
Serge Rielau wrote: Simply import into the new table and set the column to a default value. Then run the UPDATE statement I provided to populate the column.
Cheers Serge -- Serge Rielau DB2 Solutions Development IBM Toronto Lab to***********@gmail.com wrote: Hi Serge,
Appreciate all your kind replies. It looks simple, but still i m facing some problem while importing data.
From the previous mail, I have created a new column with 'default 0'. While i import from flat file (of del), the first row is getting inserted, while other records are rejected by violating primary key constraints and getting SQL0803N.
Any idea wat is causing this?
I assume your new PK includes teh new column?
Of course that won't work until teh update is doen.
So, leave the PK away, import, update, then ALTER TABLE ADD PRIMARY KEY.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
altast, it work the way i want.
Here's what i did.
1.Created a temp table.
2.loaded the data into the temp table.
3. exported the data from the temp table with rownumber () over ...
4. created new table with the new table. (without the PK)
5. loaded the data into the new table with the selected column (leaving
all not null columns)
6. updated the new column (receipt_num) with the update with partition
by and order by.
7. created pk.
Thanks for your help.
Regards
Jignesh This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul Reddin |
last post by:
Hi,
(V8.1 Fp2)
Our application uses JDBC batch to execute mutiple insert statements
and we saw a strange thing this morning.
There were 4 SQL Insert statements in the batch, and we know the...
|
by: Jean-David Beyer |
last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts
of a database on each drive. The main index is on one SCSI drive all to
itself. The main data are on the other three SCSI...
|
by: Steven David |
last post by:
People,
someone help me please.
I have a webapplication, and i store the statements in xml
file. Then, when i need then i took at the file.
In one method of mine i have to add a record at the...
|
by: Eric E |
last post by:
Hi,
I have a question about sequences. I need a field to have values with
no holes in the sequence. However, the values do not need to be in order.
My users will draw a number or numbers from...
|
by: pat270881 |
last post by:
hello,
I have to implement a sequence class, however the header file is
predefined
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
| |
by: Defcon2030 |
last post by:
<bHey, can someone help me with this? I've been working on it for a
few days now, and my head's starting to spin... </b>
// FILE:ex1_imp.cxx
//
//
//
// CLASS IMPLEMENTED: sequence (see ex1.h...
|
by: davydany |
last post by:
Hey guys...a n00b Here for this site.
I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
|
by: slickdock |
last post by:
I need to break my query into 3 groups:
First 60 records (records 1-60)
Next 60 records (records 61-121)
Next 60 records (records 122-182)
Of course I could use top values 60 for the first...
|
by: altaey |
last post by:
Question Details:
Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow:
Fn = Fn-2 + Fn-1, n >= 0
F0 = 0, F1 = 1, F2 = 1
Your...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |