LOAD csv file with specified column values - database

I have 2 csv files that contains 4 cols(c1,c2,c3,c4) and created a table that contain 5 columns (a1,a2,a3,a4,a5).
Now I want to load those two files into the tables separately such that for each time I can have a contant value that goes in a1 column of the table.
Values in csv file 1
c1,c2,c3,c4
............
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Values in csv file 2
c1,c2,c3,c4
............
5 6 7 8
5 6 7 8
5 6 7 8
5 6 7 8
resulting table should be
a1, a2, a3, a4 ,a5
..................
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value2 5 6 7 8
my_value2 5 6 7 8
my_value2 5 6 7 8
my_value2 5 6 7 8
I tried this but clearly doesn't work and I read the load documentation from the IBM site but I wasn't able to find anything.
load from path\file1 of del insert into table_name(my_value1, c1,c2,c3,c4)
load from path\file2 of del insert into table_name(my_value2, c1,c2,c3,c4)

You have got some good suggestions in the comments, so this is just another way to do it:
db2 "create table target(a1 varchar(20) not null, a2 int, a3 int, a4 int, a5 int)"
db2 "alter table target alter column a1 set default 'my_value1'"
db2 "load from ./f1.csv of del insert into target (a2,a3,a4,a5)"
db2 "alter table target alter column a1 set default 'my_value2'"
db2 "load from ./f2.csv of del insert into target (a2,a3,a4,a5)"
db2 "alter table target alter column a1 drop default"
db2 "select * from target"
A1 A2 A3 A4 A5
-------------------- ----------- ----------- ----------- -----------
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value1 1 2 3 4
my_value2 5 6 7 8
my_value2 5 6 7 8
my_value2 5 6 7 8
my_value2 5 6 7 8
8 record(s) selected.

Related

Multi level sorting for a column cell in SQL server

I am having a table column which contains a number of series(comma separated multiple values), in which i can apply sorting till first number(column name - Test_Values) but unable to continue sorting for the next numbers.
the actual tables are:
ID Test
1 Test1
2 Test2
3 Test3
4 Test4
5 Test5
AND
new_ID Test_ID T_Values
1 1 2
2 1 5
3 1 9
4 2 3
5 2 4
6 2 7
7 3 3
8 3 4
9 3 6
10 4 4
11 4 11
12 4 9
13 5 4
14 5 5
15 5 9
Currently my table looks like :
ID Test Test_Values
1 Test1 2,5,9
2 Test2 3,4,7
3 Test3 3,4,6
4 Test4 4,11,9
5 Test5 4,5,9
and required output is
ID Test Test_Values
1 Test1 2,5,9
3 Test3 3,4,6
2 Test2 3,4,7
5 Test5 4,5,9
4 Test4 4,11,9
using this the below i can achieve sorting till first number:
cast(substring(Test_Values,1,CHARINDEX(',',Test_Values)-1) as integer)
can anyone suggest, thanks in advance

Get unique values in matrix with Matlab

I'm looking for fastest way to get unique values in matrix with Matlab! I have a matrix like this:
1 2
1 2
1 3
1 5
1 23
2 1
3 1
3 2
3 2
3 2
4 17
4 3
4 17
and need to get something like this:
1 2
1 3
1 5
1 23
2 1
3 1
3 2
4 3
4 17
Actually I need unique values by combination of columns in each row.
Have a look at matlabs unique() function with the argument 'rows'.
C = unique(A,'rows')
https://de.mathworks.com/help/matlab/ref/unique.html

How can I create a 3D array by stenciling the columns of a 2D array in MATLAB?

Suppose I have a 2D array called A. I want to create a 3D array called B, whose "pages" are select columns of a stencil moving across A, column-by-column. For example, the first page of B might be the 1st, 3rd, and 5th columns of A. Then the second page would be the 2nd, 4th, and 6th columns of A, etc.
Anyone have an efficient way of doing this is MATLAB?
Thanks!
I am guessing you are looking for this -
%%// Given 2D array
A = randi(10,4,12)
t1 = reshape(A,size(A,1)*2,[]);
t2 = reshape(t1',size(A,2)/2,[],2); %%//'
B = permute(t2,[2 1 3]) %%// Output 3D array
Output -
A =
5 10 3 5 6 8 4 3 8 10 8 7
10 8 3 7 6 10 9 2 7 8 8 5
10 4 7 8 6 4 5 4 1 1 3 7
7 7 6 6 1 10 5 8 9 4 3 3
B(:,:,1) =
5 3 6 4 8 8
10 3 6 9 7 8
10 7 6 5 1 3
7 6 1 5 9 3
B(:,:,2) =
10 5 8 3 10 7
8 7 10 2 8 5
4 8 4 4 1 7
7 6 10 8 4 3
Of course, there is an alternative straight-forward approach for this special case -
B(:,:,1)=A(:,1:2:end);
B(:,:,2)=A(:,2:2:end);

Reshape acast() remove missing values

I have this dataframe:
df <- data.frame(subject = c(rep("one", 20), c(rep("two", 20))),
score1 = sample(1:3, 40, replace=T),
score2 = sample(1:6, 40, replace=T),
score3 = sample(1:3, 40, replace=T),
score4 = sample(1:4, 40, replace=T))
subject score1 score2 score3 score4
1 one 2 4 2 2
2 one 3 3 1 2
3 one 1 2 1 3
4 one 3 4 1 2
5 one 1 2 2 3
6 one 1 5 2 4
7 one 2 5 3 2
8 one 1 5 1 3
9 one 3 5 2 2
10 one 2 3 3 4
11 one 3 2 1 3
12 one 2 5 2 1
13 one 2 4 1 4
14 one 2 2 1 3
15 one 1 3 1 4
16 one 1 6 1 3
17 one 3 4 2 2
18 one 3 2 1 3
19 one 2 5 3 1
20 one 3 6 2 1
21 two 1 6 3 4
22 two 1 2 1 2
23 two 3 2 1 2
24 two 1 2 2 1
25 two 2 3 1 3
26 two 1 5 3 3
27 two 2 4 1 4
28 two 2 6 2 4
29 two 1 6 2 2
30 two 1 5 1 4
31 two 2 1 2 4
32 two 3 6 1 1
33 two 1 1 3 1
34 two 2 4 2 3
35 two 2 1 3 2
36 two 2 3 1 3
37 two 1 2 3 4
38 two 3 5 2 2
39 two 2 1 3 4
40 two 2 1 1 3
Note that the scores have different ranges of values. Score 1 ranges from 1-3, score 2 from -6, score 3 from 1-3, score 4 from 1-4
I'm trying to reshape data like this:
library(reshape2)
dfMelt <- melt(df, id.vars="subject")
acast(dfMelt, subject ~ value ~ variable)
Aggregation function missing: defaulting to length
, , score1
1 2 3 4 5 6
one 6 7 7 0 0 0
two 8 9 3 0 0 0
, , score2
1 2 3 4 5 6
one 0 5 3 4 6 2
two 5 4 2 2 3 4
, , score3
1 2 3 4 5 6
one 10 7 3 0 0 0
two 8 6 6 0 0 0
, , score4
1 2 3 4 5 6
one 3 6 7 4 0 0
two 3 5 5 7 0 0
Note that the output array includes scores as "0" if they are missing. Is there any way to stop these missing scores being outputted by acast?
In this case, you might do better sticking to base R's table feature. I'm not sure that you can have an irregular array like you are looking for.
For example:
> lapply(df[-1], function(x) table(df[[1]], x))
$score1
x
1 2 3
one 9 6 5
two 11 4 5
$score2
x
1 2 3 4 5 6
one 2 5 4 3 3 3
two 4 2 2 3 4 5
$score3
x
1 2 3
one 9 5 6
two 4 11 5
$score4
x
1 2 3 4
one 4 4 8 4
two 2 6 5 7
Or, using your "long" data:
with(dfMelt, by(dfMelt, variable,
FUN = function(x) table(x[["subject"]], x[["value"]])))
Since each "score" subset is going to have a different shape, you will not be able to preserve the array structure. One option is to use lists of two-dim arrays or data.frames. eg:
# your original acast call
res <- acast(dfMelt, subject ~ value ~ variable)
# remove any columns that are all zero
apply(res, 3, function(x) x[, apply(x, 2, sum)!=0] )
Which gives:
$score1
1 2 3
one 7 8 5
two 6 8 6
$score2
1 2 3 4 5 6
one 4 2 6 4 1 3
two 2 5 3 4 3 3
$score3
1 2 3
one 5 10 5
two 5 11 4
$score4
1 2 3 4
one 5 4 4 7
two 4 6 6 4

Updating syntax

I have the following scenario:
Table is _etblpricelistprices
Columns are as follows:
iPriceListNameID iPricelistNameID iStockID fExclPrice
1 1 1 10
2 2 1 20
3 3 1 30
4 4 1 40
5 5 1 100
6 6 1 200
7 7 1 300
8 8 1 400
9 1 2 1000
10 2 2 2000
11 3 2 3000
12 4 2 4000
13 5 2 50
14 6 2 40
15 7 2 30
16 8 2 20
There are only two stock items here, but a lot more in the DB. The first column is the PK which auto-increments. The second column is the Pricelist. The pricelist is split as follows. (1-4) is current pricing and (5-8) is future pricing. the third column is the stock item's ID, and the fourth column, the pricing of the item.
I need a script to update this table to swap the future and current pricing per item. Please help
Observe, if you will, that swapping the iPricelistNameID values will achieve the same overall effect as swapping the fExclPrice values, and can be perfomed using a formula:
UPDATE _etblpricelistprices
SET
iPricelistNameID = CASE
WHEN iPricelistNameID > 4 THEN iPricelistNameID - 4
ELSE iPricelistNameID + 4
END

Resources