IBM Informix DB 12.1 : Collect data into set/ json array without creating procedures/ functions - informix

I am working to try to collect all data belonging to a particular category into a Json structure or set.
for Example
ID
Status
Dim
A
P
5
B
Q
10
C
R
15
D
S
5
A
T
6
C
U
9
D
V
20
E
W
100
A
X
6
F
Y
69
Output should be:
ID
Collect
Max
Instances
A
{P:5,T:6,X:6}
6
3
B
{Q:10}
10
1
C
{R:15,U:9}
15
2
D
{S:5,V:20}
20
2
E
{W:100}
100
1
F
{Y:69}
69
1
I cannot create procedures since the org has not given me the appropriate privilege.
I was looking into multiset in Informix but that is confusing-
I wrote the query to try to build the first two columns, because I can write the max and instances later
select id,multiset(select status, dim from table i where i.id=t.id) Collect from table t where category='A'
This gave me 3 rows all containing the same value
id
collect
A
[IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) ]
A
[IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) ]
A
[IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) , IfxStruct. Type: row ( item_nbr int not null) ]
I would settle for a comma seperated list of values
For example
id
collect
A
"P:5","T:6","X:6"

Related

Store the result of sql and process it in informix

We have a view which contains 2 columns: pattern_start_time, pattern_end_time.
The select query in the function will convert it to minutes and using that result we are processing to get the shift unused coverage.The function is getting created but the processing is not happening and getting the below error:
SQLError[IX000]:Routine (my_list) cant be resolved.
Also please enter image description heresuggest to loop till the length of the result.
CREATE function myshifttesting(orgid int) returning int;
DEFINE my_list LIST( INTEGER not null );
DEFINE my_list1 LIST( INTEGER not null );
define i, j, sub, sub1 int;
define total int;
TRACE ON;
TRACE 'my testing starts';
INSERT INTO TABLE( my_list )
select
((extend(current, year to second) + (dots.v_shift_coverage.pattern_start_time - datetime(00:00) hour to minute) - current)::interval minute(9) to minute)::char(10)::INTEGER
from
dots.v_shift_coverage
where
org_guid = orgid;
INSERT INTO TABLE( my_list1 )
select
((extend(current, year to second) + (dots.v_shift_coverage.pattern_end_time - datetime(00:00) hour to minute) - current)::interval minute(9) to minute)::char(10)::INTEGER
from
dots.v_shift_coverage
where
org_guid = orgid;
let sub = 0;
let sub1 = 0;
let total = 0;
for j = 0 to 4
if (my_list(j) < my_list1(j))
then
if (my_list(j + 1) > my_list1(j))
then
let sub = sub + my_list(j + 1) - my_list1(j);
end if;
end if;
end for
if (my_list(0) > my_list1(4))
then
let sub1 = my_list(0) - my_list1(4);
end if;
let total = sub + sub1;
return total;
end function;
The error that you are receiving is because my_list(j) is not valid Informix syntax to access a LIST element. Informix is interpreting my_list(j) as a call to a function named mylist.
You can use a temporary table to "emulate" an array with your logic, something like this:
CREATE TABLE somedata
(
letter1 CHAR( 2 ),
letter2 CHAR( 2 )
);
INSERT INTO somedata VALUES ( 'a1', 'a2' );
INSERT INTO somedata VALUES ( 'b1', 'b2' );
INSERT INTO somedata VALUES ( 'c1', 'c2' );
INSERT INTO somedata VALUES ( 'd1', 'd2' );
INSERT INTO somedata VALUES ( 'e1', 'e2' );
DROP FUNCTION IF EXISTS forloop;
CREATE FUNCTION forloop()
RETURNING CHAR( 2 ) AS letter1, CHAR( 2 ) AS letter2;
DEFINE number_of_rows INTEGER;
DEFINE iterator INTEGER;
DEFINE my_letter1 CHAR( 2 );
DEFINE my_letter2 CHAR( 2 );
-- Drop temp table if it already exists in the session
DROP TABLE IF EXISTS tmp_data;
CREATE TEMP TABLE tmp_data
(
tmp_id SERIAL,
tmp_letter1 CHAR( 2 ),
tmp_letter2 CHAR( 2 )
);
-- Insert rows into the temp table, serial column will be the access key
INSERT INTO tmp_data
SELECT 0,
d.letter1,
d.letter2
FROM somedata AS d
ORDER BY d.letter1;
-- Get total rows of temp table
SELECT COUNT( * )
INTO number_of_rows
FROM tmp_data;
FOR iterator = 1 TO number_of_rows
SELECT d.tmp_letter1
INTO my_letter1
FROM tmp_data AS d
WHERE d.tmp_id = iterator;
-- Check if not going "out of range"
IF iterator < number_of_rows THEN
SELECT d.tmp_letter2
INTO my_letter2
FROM tmp_data AS d
WHERE d.tmp_id = iterator + 1;
ELSE
-- iterator + 1 is "out of range", return to the beginning
SELECT d.tmp_letter2
INTO my_letter2
FROM tmp_data AS d
WHERE d.tmp_id = 1;
END IF;
RETURN my_letter1, my_letter2 WITH RESUME;
END FOR;
END FUNCTION;
-- Running the function
EXECUTE FUNCTION forloop();
-- Results
letter1 letter2
a1 b2
b1 c2
c1 d2
d1 e2
e1 a2
5 row(s) retrieved.

Dividing a value between non-equal rows in order to balance them

I have a spreadsheet that's structured like:
Section Total Incoming New Total
AK 56,445 2,655 59,100
AL 58,304 796 59,100
B 55,524 3,576 59,100
C 54,272 4,828 59,100
D 53,956 5,144 59,100
S 59,161 0 59,161
-
Generated Pts 16,999
I'm trying to automate the "Incoming" column. The goal of the sheet is to balance the Totals as closely as possible by distributing the Generated Pts between each row until no more points remain, ensuring that the lowest totals are always increased first so that higher values aren't increased while lower values exist.
Is this possible in a spreadsheet? Any suggestions on how this could be done?
I made an attempt at a custom function. Two parameters are passed: the range corresponding with your Total column, and the cell containing the generated pts. Then the Incoming array is returned.
function distribute(range, value) {
var indexedRange = range.map(function (e, index) {return [e[0], e[0], index];});
indexedRange.sort(function (a, b) {return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;});
var count = 0, i = 0, limit = indexedRange.length - 1;
while (count < value) {
indexedRange[i][0] ++;
i = i == limit || indexedRange[i][0] <= indexedRange[i + 1][0] ? 0 : i + 1;
count++;
}
indexedRange.sort(function (a, b) {return a[2] < b[2] ? -1 : a[2] > b[2] ? 1 : 0;});
return indexedRange.map(function (e) {return [e[0] - e[1]];});
}
It matches your expected results, but you might want to try it out on different data to check my logic is OK.

Joining two record sets into one result including NULL values

I have sql script like below:
select a.Program,a.COUNTS_OPEN,b.COUNTS_CLOSE
from
(select Program, count(ISNULL(Program,'UNKNOWN')) COUNTS_OPEN
from table_a
WHERE (SUBMITDATE > CONVERT(datetime, '2014-08-31 23:59:59.000') and SUBMITDATE < CONVERT(datetime, '2014-10-01 00:00:00.000') )
group by Program) as a
full JOIN
(
select Program,count(ISNULL(Program,'UNKNOWN')) COUNTS_CLOSE
from table_a
WHERE (STATUS='Closed' and UPDATEDATE > CONVERT(datetime, '2014-08-31 23:59:59.000') and UPDATEDATE < CONVERT(datetime, '2014-10-01 00:00:00.000') )
group by Program) as b
on a.Program= b.Program
and in the result there are two rows of NULL, But I'm expecting for one row NULL as it's grouped by program. here is the result set:
Program COUNTS_OPEN COUNTS_CLOSE
NULL NULL 8
NULL 18 NULL
ProgramA 253 205
ProgramB 2 2
ProgramC 123 109
in stead of two rows NULL, I need one row like:
Program COUNTS_OPEN COUNTS_CLOSE
NULL 18 8
ProgramA 253 205
ProgramB 2 2
ProgramC 123 109
can't figure it out how to how to join correctly.
Without knowing your table structure this is a guess, but I think this query using conditional aggregation should accomplish what you want:
SELECT
Program, -- replace with ISNULL(Program,'UNKNOWN'), if you want UNKNOWN instead of NULL
COUNTS_OPEN = SUM(case when (SUBMITDATE > CONVERT(datetime, '2014-08-31 23:59:59.000') and SUBMITDATE < CONVERT(datetime, '2014-10-01 00:00:00.000') ) then 1 else 0 end),
COUNTS_CLOSED = SUM(case when (STATUS='Closed' and UPDATEDATE > CONVERT(datetime, '2014-08-31 23:59:59.000') and UPDATEDATE < CONVERT(datetime, '2014-10-01 00:00:00.000') ) then 1 else 0 end)
FROM table_a
GROUP BY program

IBM DB2 SQLCODE -1424, SQLSTATE 54040 when trying to create a trigger

I have tried to create the following trigger (DB2/LINUXX8664 9.7.2 running over Ubuntu 10.04.2 LTS) but I always get this:
Error report:
DB2 SQL error: SQLCODE: -1424, SQLSTATE: 54040, SQLERRMC: 2
According IBM DB2 documentation:
Too many references to transition variables and transition table columns or the row length for these references is too long. Reason code=rc.
http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/com.ibm.db2.luw.messages.sql.doc/doc/msql01424n.html
I really do not understand that explanation. Here the stored procedure and trigger:
CREATE PROCEDURE SP_INSERT_UPDATE_REPNUM (
IN RECNUM INTEGER,
IN CUSTOMER_ID INTEGER,
IN Q_CODE VARCHAR(14),
IN S_CODE VARCHAR(14),
IN REP_STATUS INTEGER,
IN P_CODE INTEGER,
IN REPNUMRG_ID INTEGER,
IN VOLG_LET VARCHAR(1),
IN REP_DATUM DATE,
IN REP_INI VARCHAR(2),
IN INGEBOEKT_DATUM DATE,
IN INGEBOEKT_INI VARCHAR(2),
IN WIJZIGING_DATUM DATE,
IN WIJZIGING_INI VARCHAR(2),
IN OMSCR_STORING VARCHAR(5),
IN OMSCR_WERKZ VARCHAR(5),
IN OMSCR_OPMERKING VARCHAR(5),
IN OMSCR_GEBREK VARCHAR(5),
IN OMSCR_MAT VARCHAR(5),
IN REP_TIJD FLOAT,
IN REP_GEDAAN CHAR(1),
IN ACTION CHAR(1)
)
SPECIFIC SP_INSERT_UPDATE_REPNUM
DYNAMIC RESULT SETS 0
DETERMINISTIC
LANGUAGE JAVA
PARAMETER STYLE JAVA
NO DBINFO
NOT FENCED
THREADSAFE
MODIFIES SQL DATA
PROGRAM TYPE SUB
EXTERNAL NAME 'RepairMigration!insertServiceReport'
and here the trigger:
CREATE TRIGGER INSERT_REPNUM
AFTER INSERT ON REPNUM
REFERENCING NEW ROW AS NROW
FOR EACH ROW MODE DB2SQL
BEGIN
CALL SP_INSERT_UPDATE_REPNUM(
NROW.RECNUM,
NROW.CUSTOMER_ID,
NROW.Q_CODE,
NROW.S_CODE,
NROW.REP_STATUS,
NROW.P_CODE,
NROW.REPNUMRG_ID,
NROW.VOLG_LET,
NROW.REP_DATUM,
NROW.REP_INI,
NROW.INGEBOEKT_DATUM,
NROW.INGEBOEKT_INI,
NROW.WIJZIGING_DATUM,
NROW.WIJZIGING_INI,
NROW.OMSCR_STORING,
NROW.OMSCR_WERKZ,
NROW.OMSCR_OPMERKING,
NROW.OMSCR_GEBREK,
NROW.OMSCR_MAT,
NROW.REP_TIJD,
NROW.REP_GEDAAN,
'I'
);
END
the table structure is thsi one:
RECNUM INTEGER 4 0 N
Q_CODE CHARACTER 14 0 N ''
U_Q_CODE CHARACTER 14 0 N
S_CODE CHARACTER 14 0 N ''
U_S_CODE CHARACTER 14 0 N
VOLG_LET CHARACTER 1 0 N ''
U_VOLG_LET CHARACTER 1 0 N
REP_DATUM DATE 4 0 N '0001-01-01'
REP_INI CHARACTER 2 0 N ''
P_CODE INTEGER 4 0 N 0
CUSTOMER_ID INTEGER 4 0 N 0
REPNUMHD_ID INTEGER 4 0 N 0
REPNUMRG_ID INTEGER 4 0 N 0
REP_STATUS SMALLINT 2 0 N 0
KONTAKT_PER CHARACTER 25 0 N ''
KONTAKT_TEL CHARACTER 20 0 N ''
OMSCR_STORING VARCHAR 512 0 N ''
OMSCR_WERKZ VARCHAR 2000 0 N ''
OMSCR_MAT VARCHAR 512 0 N ''
WIJZIGING_DATUM DATE 4 0 N '0001-01-01'
WIJZIGING_INI CHARACTER 2 0 N ''
INGEBOEKT_DATUM DATE 4 0 N '0001-01-01'
INGEBOEKT_INI CHARACTER 2 0 N ''
REP_GEDAAN CHARACTER 1 0 N ''
U_REP_GEDAAN CHARACTER 1 0 N
STATUS SMALLINT 2 0 N 0
UW_OPDRACHT CHARACTER 20 0 N ''
REP_UREN SMALLINT 2 0 N 0
REP_MINUTEN SMALLINT 2 0 N 0
OMSCR_OPMERKING VARCHAR 1008 0 N ''
OMSCR_GEBREK VARCHAR 512 0 N ''
SERVICE_NUMMER INTEGER 4 0 N 0
PRIJS_OPGAVE CHARACTER 1 0 N ''
CURRENCY SMALLINT 2 0 N 0
REP_TIJD INTEGER 4 0 N 0
After trying to execute the trigger, I get the error. How can I solve this?
UPDATE
It looks that I need to create a temporary tablespace. Listing all my tablespaces, I got some with more than 4k, for example this one:
Tablespace ID = 7
Name = TEMPSPACE1234
Type = Database managed space
Contents = All permanent data. Regular table space.
State = 0x0000
Detailed explanation:
Normal
Total pages = 1024
Useable pages = 1008
Used pages = 432
Free pages = 576
High water mark (pages) = 528
Page size (bytes) = 32768
Extent size (pages) = 16
Prefetch size (pages) = 16
Number of containers = 1
1) Create a 32K Buffer Pool
Example:
CREATE BUFFERPOOL "BUFFERPOOLLARGE" IMMEDIATE
ALL DBPARTITIONNUMS SIZE AUTOMATIC
NUMBLOCKPAGES 0
PAGESIZE 32 K;
2) Create a 32K SYSTEM TEMPORARY TABLESPACE
Example:
CREATE SYSTEM TEMPORARY TABLESPACE "TEMPSPACELARGE"
IN DATABASE PARTITION GROUP "IBMTEMPGROUP"
PAGESIZE 32 K
MANAGED BY AUTOMATIC STORAGE
EXTENTSIZE 32
BUFFERPOOL "BUFFERPOOLLARGE"
OVERHEAD INHERIT
TRANSFERRATE INHERIT
USING STOGROUP "IBMSTOGROUP"
FILE SYSTEM CACHING;
3) Run CREATE PROCEDURE Script Again.
OBS:In the examples I used DB2 automatic TABLESPACE.

How to get sum of values in grid?

For example I have grid.
//grid for answers_for_online
var answersGridForOnline5 = new Ext.grid.GridPanel({
id : 'grid_for_stats',
store : storez3,
columns : answers_columns5,
});
my column:
var answers_columns5 = [{
id: "idz",
header: 'idz',
dataIndex: "idz",
renderer: fun_f
}];
and renderer function
function fun(n, j, k, m, h, i) {
var count = store.snapshot ? store.snapshot.length : store.getCount()
var cez = k.get("scale")
var ce = ( 2 / count ) * 100
return ce + " % "
}
Question: In database I have for example: scales (that user answered on scale-question)
id | scale
1 | 4
2 | 4
3 | 1
4 | 2
How i can sum scales (and group them of course) and put this in my grid?
For example in my grid i should get:
scale | scale %
1 | 25%
2 | 25%
4 | 50%
I advise you don't attempt to do it inside Grid/Store. Instead process the data before loading it to store - for example do it in database with GROUP BY statement.
To get the sum of values in a store, you can use Store.sum()

Resources