how to concatenate or append current value with existing value in Datastage - datastage

I need achive below requirement i.e
Input -- at very first time
Order value
1111 aaa
222 bbb
333 ccc
in the target (Insert) I will have
Order value
Order value
1111 aaa
222 bbb
333 ccc
----------Input -- at second time
Order value
1111 Aaa1
222 Bbb2
333 ccc
Out put must be
Order value
1111 aaa Aaa1
222 bbb Bbb2
So on
I need to keep appending change values for the corresponding key column ..
111 aaa aaa1 aaa2 aaa3 ..like this
Please help

You can follow these steps:
Use a CDC stage. Here by default it keeps the following functions: 0 for copy/duplicate record,1 for insert ,b2 for delete, 3 for update.
Now the link which would be carrying the copy record simply connect it to a transformer where declare a stage variable that would be incremented by 1.
Next in the field derivation write as CONCAT(string,'Initcaps(a'),'aa',svarcount,'')

Related

PostgreSQL: Count Number of Occurrences in Columns

BACKGROUND
I have three large tables (employee_info, driver_info, school_info) that I have joined together on common attributes using a series of LEFT OUTER JOIN operations. After each join, the resulting number of records increased slightly, indicating that there are duplicate IDs in the data. To try and find all of the duplicates in the IDs, I dumped the ID columns into a temp table like so:
Original Dump of ID Columns
first_name
last_name
employee_id
driver_id
school_id
Mickey
Mouse
1234
abcd
wxyz
Donald
Duck
2423
heca
qwer
Mary
Poppins
1111
acbe
aaaa
Wiley
Cayote
1234
strf
aaaa
Daffy
Duck
1256
acbe
pqrs
Bugs
Bunny
9999
strf
yxwv
Pink
Panther
2222
zzzz
zzaa
Michael
Archangel
0000
rstu
aaaa
In this overly simplified example, you will see that IDs 1234 (employee_id), strf (driver_id), and aaaa (school_id) are each duplicated at least once. I would like to add a count column for each of the ID columns, and populate them with the count for each ID used, like so:
ID Columns with Counts
first_name
last_name
employee_id
employee_id_count
driver_id
driver_id_count
school_id
school_id_count
Mickey
Mouse
1234
2
abcd
1
wxyz
1
Donald
Duck
2423
1
heca
1
qwer
1
Mary
Poppins
1111
1
acbe
1
aaaa
3
Wiley
Cayote
1234
2
strf
2
aaaa
3
Daffy
Duck
1256
1
acbe
1
pqrs
1
Bugs
Bunny
9999
1
strf
2
yxwv
1
Pink
Panther
2222
1
zzzz
1
zzaa
1
Michael
Archangel
0000
1
rstu
1
aaaa
3
You can see that IDs 1234 and strf each have 2 in the count, and aaaa has 3. After generating this table, my goal is to pull out all records where any of the counts are greater than 1, like so:
All Records with One or More Duplicate IDs
first_name
last_name
employee_id
employee_id_count
driver_id
driver_id_count
school_id
school_id_count
Mickey
Mouse
1234
2
abcd
1
wxyz
1
Mary
Poppins
1111
1
acbe
1
aaaa
3
Wiley
Cayote
1234
2
strf
2
aaaa
3
Bugs
Bunny
9999
1
strf
2
yxwv
1
Michael
Archangel
0000
1
rstu
1
aaaa
3
Real World Perspective
In my real-world work, the JOIN'd table contains 100 columns, 15 different ID fields and over 30,000 records, and the final table came out to be 28 more than the original. This may seem like a small amount, but each of the 28 represent a broken link that we must fix.
Is there a simple way to get the counts populated like in the second table above? I have been wrestling with this for hours already, and have not been able to make this work. I tried some aggregate functions, but they cannot be used in table UPDATE operations.
The COUNT function, when used as an analytic function, can do what you want here, e.g.
WITH cte AS (
SELECT *,
COUNT(employee_id) OVER (PARTITION BY employee_id) employee_id_count,
COUNT(driver_id) OVER (PARTITION BY driver_id) driver_id_count,
COUNT(school_id) OVER (PARTITION BY school_id) school_id_count
FROM yourTable
)
SELECT *
FROM cte
WHERE
employee_id_count > 1
driver_id_count > 1
school_id_count > 1;

Autoincrement in query

I need to create a query which increment value of current row by 8% to previous row.
Table (let's name it money) contains one row (and two columns), and it looks like
AMOUNT ID
100.00 AAA
I just need to populate a data from this table like this way (one select from this table, eg. 6 iterations):
100.00 AAA
108.00 AAA
116.64 AAA
125.97 AAA
136.04 AAA
146.93 AAA
You can do that with a common table expression.
E.g. if your source looks like this:
db2 "create table money(amount decimal(31,2), id varchar(10))"
db2 "insert into money values (100,'AAA')"
You can create the input data with the following query (I will include counter column for clarity):
db2 "with
cte(c1,c2,counter)
as
(select
amount, id, 1
from
money
union all
select
c1*1.08, c2, counter+1
from
cte
where counter < 10)
select * from cte"
C1 C2 COUNTER
--------------------------------- ---------- -----------
100.00 AAA 1
108.00 AAA 2
116.64 AAA 3
125.97 AAA 4
136.04 AAA 5
146.92 AAA 6
158.67 AAA 7
171.36 AAA 8
185.06 AAA 9
199.86 AAA 10
To populate the existing table without repeating the existing row you use e.g. an insert like this:
$ db2 "insert into money
with
cte(c1,c2,counter)
as
(select
amount*1.08, id, 1
from
money
union all
select
c1*1.08, c2, counter+1
from
cte
where counter < 10) select c1,c2 from cte"
$ db2 "select * from money"
AMOUNT ID
--------------------------------- ----------
100.00 AAA
108.00 AAA
116.64 AAA
125.97 AAA
136.04 AAA
146.93 AAA
158.68 AAA
171.38 AAA
185.09 AAA
199.90 AAA
215.89 AAA
11 record(s) selected.

How to concatenate columns in update statement

I have this table:
t:([] name:("aaa";"bbb";"ccc";"dddd"); side:(1;2;1;2))
Now I want to add a new column "concatenated", which contains a symbol, which is the concatenation of both values for each row:
I would assume that I have to do this with an each-both adverb, but this here does not work:
update concatenated:((`$name),'(`$side)) from t
How would I have to change this? Thanks.
Your attempt is close the issue with it works if you convert the 'side' column to string format first
I've added two versions one where the concatenation does not merge the 2 values and one where they are merged as a single symbol
q)t:([] name:("aaa";"bbb";"ccc";"dddd"); side:(1;2;1;2))
q)update conc:((`$name),'`$string side) from t
name side conc
------------------
"aaa" 1 aaa 1
"bbb" 2 bbb 2
"ccc" 1 ccc 1
"dddd" 2 dddd 2
q)update conc:(`$name,'string side) from t
name side conc
-----------------
"aaa" 1 aaa1
"bbb" 2 bbb2
"ccc" 1 ccc1
"dddd" 2 dddd2
Hope this helps

How to summarize the data generated in crystal report

I have created a Crystal report whose sample output is below:
C_name P_name Code P. Rev F. rev C.rev
ABC AAA ABC-1-1 100 1100 1100
ABC AAA ABC-1-2 200 1200 1200
ABC AAA ABC-1-3 300 1300 2300
XYZ BBB XYZ-1-1 200 1200 2200
XYZ BBB XYZ-1-2 150 1150 3150
DEF CCC DEF-5-1 400 1400 1400
DEF CCC DEF-5-6 100 1100 2100
DEF CCC DEF-5-9 200 1200 4200
DEF DDD DEF-8-11 300 1300 2300
DEF DDD DEF-8-12 400 1400 400
Now, I want to add up the values for max value of Code. For example, ABC have 3 codes out of which ABC-1-3 is the latest code. So I want to dsiplay one record for these 3 records and add up the revenue values for 3 records and display it in one row only. The final output should look like below:
ABC AAA ABC-1-3 600 3600 4600
XYZ BBB XYZ-1-2 350 2350 5350
DEF CCC DEF-5-9 700 3700 7700
DEF DDD DEF-8-12 700 2700 2700
Please help..
Thanks
you can insert Group and P_name is the Group field which you select to create your group then in Running Total Fields and create new field for each of P.Rev F.rev C.rev in Evaluate and Reset part click on change of group and chose Type of summary Sum and for the Code field do the same,just chose Type of summary maximum. now you can add new fields to your Group and see the result.

Getting wrong Count of the Combinations Between Two Fields of a File thru OUTFIL

i need to include this condition:
1) Total no.of records per combination of field1 and field3 (INCLUDE=(1,2,8,3,CH,A)
INPUT FILE: FIELD1 AND FIELD3 have 5 combinations,if you see in example below
field1 field2 field3 field4
AA 00000 123 ABC
AA 00000 123 ABC
AA 00000 456 ABC
BB 00000 123 ABC
BB 00000 123 ABC
BB 00000 789 ABC
AA 00000 567 ABC
OUTPUT FILE: gets 5 rows, one for each combination, gives no.of occurrences for it
FIELD1 FIELD3 COUNT-OF-COMBINATION
AA 123 2
AA 456 1
AA 567 1
BB 123 2
AA 789 1
My method is:
//SYSIN DD *
SORT FIELDS=COPY
OPTION COPY
OUTFIL REMOVECC,NODETAIL,
TRAILER1=(1,2,'ON',8,3,'=',COUNT=(M11,LENGTH=10)))
/*
Answer i got is:
AA ON 123 = 7
which is wrong:
its should have been
AA ON 123 = 2
AA ON 456 = 1
AA ON 567 = 1
BB ON 123 = 2
AA ON 789 = 1
You have:
SORT FIELDS=COPY
OPTION COPY
OUTFIL REMOVECC,NODETAIL,
TRAILER1=(1,2,'ON',8,3,'=',COUNT=(M11,LENGTH=10)))
First problem is you have SORT FIELDS=COPY and OPTION COPY. These mean the same thing. Remove one or the other (I tend to use OPTION COPY).
Next, you have a spare right parenthesis.
Then you are using TRAILER1. There are three types of TRAILERn: 1 is "at the end of the report"; 2 is at the end of a page; 3 is at a control break.
You use TRAILER1, so at the end of your file you get one record, containing file totals.
After that, your positions for the TRAILER1 match the output, not the input file.
Which brings us to the fact that you are not running those Sort control cards with that data. The control cards have a syntax error which means they don't run. Correcting the cards and retaining the TRAILER1 gets AAON567=0000000007.
Which brings us to the control break, which is what you have missing.
You define a control break with SECTIONS. TRAILER3 is part of SECTIONS.
Fixing everything except your output format:
OPTION COPY
OUTFIL REMOVECC,NODETAIL,
SECTIONS=(1,2,
15,3,
TRAILER3=(1,2,
'ON',
15,3,
'=',
COUNT=(M11,
LENGTH=10)))
Which gives you:
AAON123=0000000002
AAON456=0000000001
BBON123=0000000002
BBON789=0000000001
AAON567=0000000001
If you want column headings, look at how to use HEADER3 (HEADER1 and HEADER2 would also work in this simple case). If you want "page totals" look at TRAILER2. If you want file totals, use TRAILER1.