Using a cursor in a stored procedure - stored-procedures

I want to find the min, max and average for each quarter using stored procedure and cursors. Can't figure out where am I going wrong.
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE seven2
IS
CURSOR c1 IS
SELECT
qtrcode as QTR,
cast(MIN(salaryoffered) as decimal(4,2)) as MIN,
cast(MAX(salaryoffered) as decimal(4,2)) as MAX,
cast(AVG(salaryoffered) as decimal(4,2)) as AVG
FROM
interview
GROUP BY
qtrcode
ORDER BY
qtrcode
BEGIN
FOR qtrcode in c1
LOOP
dbms_output.put_line(min || ' ' ||max|| ' ' ||avg);
END LOOP;
END;
/
Can we add padding functions to the above query and if so how?

I have created the interview table with just 2 columns for the example purpose and inserted some dummy data, see the changes in the procedure, it works fine now
SQL>
SQL> desc interview
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------
SALARYOFFERED NUMBER(10,2)
QTRCODE VARCHAR2(4)
SQL>
SQL> CREATE OR REPLACE PROCEDURE seven2
2 IS
3 CURSOR c1 IS
4 SELECT
5 qtrcode as QTR,
6 cast(MIN(salaryoffered) as decimal(10,4)) as MIN,
7 cast(MAX(salaryoffered) as decimal(10,4)) as MAX,
8 cast(AVG(salaryoffered) as decimal(10,4)) as AVG
9 FROM
10 interview
11 GROUP BY
12 qtrcode
13 ORDER BY
14 qtrcode ;
15
16 BEGIN
17 FOR qtrcode in c1
18 LOOP
19 dbms_output.put_line('The qtr is ' || qtrcode.qtr ||' The min sal is '|| qtrcode.min || ' The max sal is ' || qtrcode.max|| ' The avg sal is' ||qtrcode.avg);
20 END LOOP;
21 END;
22 /
Procedure created.
SQL> exec seven2;
The qtr is Q1 The min sal is 137.07 The max sal is 964.66 The avg sal is 580.8748
The qtr is Q2 The min sal is 127.79 The max sal is 938.49 The avg sal is 550.52
The qtr is Q3 The min sal is 231.1 The max sal is 992.45 The avg sal is 672.59
The qtr is Q4 The min sal is 103.19 The max sal is 960.13 The avg sal is 431.318
PL/SQL procedure successfully completed.
SQL>

Related

Google Sheets auto increment column B with empty cells restarting from 1 at each new category String in Column A instead of continuous incrementing

I found this partial solution to my problem:
Google Sheets auto increment column A if column B is not empty
With this formula:
=ARRAYFORMULA(IFERROR(MATCH($B$2:$B&ROW($B$2:$B),FILTER($B$2:$B&ROW($B$2:$B),$B$2:$B<>""),0)))
What I need is the same but instead of continuous numbers I'd need it to restart incrementing from 1 at each new category string on an adjacent column (column A in example below, categories strings are A, B, C, D etc.).
For example:
Problem with formula in C12 and C15 (added numbers 1 and 2)
Needed result in column D, as with D11 and D19 restarts incrementing from 1 at new category string)
1
needed result
2
A
1
1
1
3
A
4
A
5
A
1
2
2
6
A
7
A
8
A
9
A
1
3
3
10
A
11
B
1
4
1
12
B
1
13
B
14
C
1
5
2
15
C
2
16
C
17
C
1
6
3
18
C
19
D
1
7
1
20
D
21
D
22
D
1
8
2
23
D
24
D
1
9
3
25
D
26
D
27
D
1
10
4
28
D
29
D
try:
=INDEX(IF(B2:B="",,COUNTIFS(A2:A&B2:B, A2:A&B2:B, ROW(A2:A), "<="&ROW(A2:A))))
or:
=INDEX(IF(B2:B="",,COUNTIFS(A2:A&IF(B2:B<>"", 1, ), A2:A&IF(B2:B<>"", 1, ), ROW(A2:A), "<="&ROW(A2:A))))
Here's another similar solution.
=ArrayFormula(if(B2:B="",,countifs(A2:A,A2:A,B2:B,"<>",row(A2:A),"<="&row(A2:A))))

InfluxDB: High cardinality for specific shards

I'm querying data from different shards and used EXPLAIN to check how many series are being fetched for that particular date range.
> SHOW SHARDS
.
.
658 mydb autogen 658 2019-07-22T00:00:00Z 2019-07-29T00:00:00Z 2020-07-27T00:00:00Z
676 mydb autogen 676 2019-07-29T00:00:00Z 2019-08-05T00:00:00Z 2020-08-03T00:00:00Z
.
.
Executing EXPLAIN for data from shard 658 and it's giving expected result in terms of number of series. SensorId is only tag key and as date range fall into only shard it's giving NUMBER OF SERIES: 1
> EXPLAIN select "kWh" from Reading where (SensorId =~ /^1186$/) AND time >= '2019-07-27 00:00:00' AND time <= '2019-07-28 00:00:00' limit 10;
QUERY PLAN
----------
EXPRESSION: <nil>
AUXILIARY FIELDS: "kWh"::float
NUMBER OF SHARDS: 1
NUMBER OF SERIES: 1
CACHED VALUES: 0
NUMBER OF FILES: 2
NUMBER OF BLOCKS: 4
SIZE OF BLOCKS: 32482
But when I run the same query on date range that falls into shard 676, number of series is 13140 instead of just one.
> EXPLAIN select "kWh" from Reading where (SensorId =~ /^1186$/) AND time >= '2019-07-29 00:00:00' AND time < '2019-07-30 00:00:00';
QUERY PLAN
----------
EXPRESSION: <nil>
AUXILIARY FIELDS: "kWh"::float
NUMBER OF SHARDS: 1
NUMBER OF SERIES: 13140
CACHED VALUES: 0
NUMBER OF FILES: 11426
NUMBER OF BLOCKS: 23561
SIZE OF BLOCKS: 108031642
Environment info:
System info: Linux 4.4.0-1087-aws x86_64
InfluxDB version: InfluxDB v1.7.6 (git: 1.7 01c8dd4)
Update - 1
On checking field cardinality, I observed a spike in RAM.
> SHOW FIELD KEY CARDINALITY
Update - 2
I've rebuilt the indexes, but the cardinality is still high.
Update - 3
I found out that shard has "SensorId" as tag as well as field that causing high cardinality when querying with the "SensorId" filter.
> SELECT COUNT("SensorId") from Reading GROUP BY "SensorId";
name: Reading
tags: SensorId=
time count
---- -----
1970-01-01T00:00:00Z 40
But when I'm checking tag values with key 'SensorId', it's not showing empty string that present in the above query.
> show tag values with key = "SensorId"
name: Reading
key value
--- -----
SensorId 10034
SensorId 10037
SensorId 10038
SensorId 10039
SensorId 10040
SensorId 10041
.
.
.
SensorId 9938
SensorId 9939
SensorId 9941
SensorId 9942
SensorId 9944
SensorId 9949
Update - 4
Inspected data using influx_inspect dumptsm and re-validated that null tag values are present
$ influx_inspect dumptsm -index -filter-key "" /var/lib/influxdb/data/mydb/autogen/235/000008442-000000013.tsm
Index:
Pos Min Time Max Time Ofs Size Key Field
1 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 5 103 Reading 1001
2 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 108 275 Reading 2001
3 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 383 248 Reading 2002
4 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 631 278 Reading 2003
5 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 909 278 Reading 2004
6 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 1187 184 Reading 2005
7 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 1371 103 Reading 2006
8 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 1474 250 Reading 2007
9 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 1724 103 Reading 2008
10 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 1827 275 Reading 2012
11 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 2102 416 Reading 2101
12 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 2518 103 Reading 2692
13 2019-08-01T01:46:31Z 2019-08-01T17:42:03Z 2621 101 Reading SensorId
14 2019-07-29T00:00:05Z 2019-07-29T05:31:07Z 2722 1569 Reading,SensorId=10034 2005
15 2019-07-29T05:31:26Z 2019-07-29T11:03:54Z 4291 1467 Reading,SensorId=10034 2005
16 2019-07-29T11:04:14Z 2019-07-29T17:10:16Z 5758 1785 Reading,SensorId=10034 2005

How to query data from txt file into AspenTech IP21 Historian?

I have process data in this format in a txt file.
testTag testTag2
10 18
6 15
7 15
9 19
Please help me to build a SQLPlus script so that after every 5 seconds one of these values orderly should update IP_INPUT_VALUE field of testTag & testTag2.
Option to reschedule the query after every 5 seconds, can be used just in case required.
Please help.
This issue is resolved by myself after multiple attempts. All i had to do is changed the txt format to below format and run the query
A B
NAME VALUE
----------------------------------------
testTag 10 6 7 9
testTag2 18 15 15 19
SQLPLus Query:
local tagname char(24);
local value real;
local x,y integer;
y=2;
for x = y to 5 do
wait 00:00:05.00;
for (select line as ln from 'c:\data\Data.txt') do
tagname = substring(1 of ln between' ');
value = substring (x of ln between ' ');
UPDATE ip_analogdef SET IP_INPUT_VALUE = value,
QSTATUS(IP_INPUT_VALUE) = 'Good'
where name=tagname;
y=y+1;
end;
end;

Sum data in column with criteria in row

I wish to make a formula to sum up the value with 2 criteria, example show as below:-
A B C D E
1 1-Apr 2-Apr 3-Apr 4-Apr
2 aa 1 4 7 10
3 bb 2 5 8 11
4 cc 3 6 9 12
5
6 Criteria 1 bb
7 Range start 2-Apr-16
8 Range End 4-Apr-16
9 Total sum #VALUE!
tried formula
1 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(C7,B1:E1,0)))
* Only return 1 cell value
2 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(">="&C7,B1:E1,0)))
* Showed N/A error
3 SUMIFS(B2:E4,A2:A4,C6,B1:E1,">="&C7,B1:E1,"<="&C8)
* Showed #Value error
Hereby I attached a link of picture for better understanding :
Can anyone help me on the formula?
I figured out the solution with step evaluation:
=SUMIF(B1:F1,">="&C7,INDEX(B2:F4,MATCH(C6,A2:A4,0),0)) -
SUMIF(B1:F1,">"&C8,INDEX(B2:F4,MATCH(C6,A2:A4,0),0))

How to read decimal in SPSS syntax?

I want to read the following data in SPSS :
ID Age Sex GPA
----------------
1 17 M 5
2 16 F 5
3 17 F 4.75
4 18 M 5
5 19 M 4.5
My attempt:
DATA LIST / ID 1 AGE 2-3 SEX 4(A) GPA 5-8.
BEGIN DATA
117M5
216F5
317F4.75
418M5
519M4.5
END DATA.
LIST.
But the output is
ID AGE SEX GPA
---------------
1 17 M 5
2 16 F 5
3 17 F 5
4 18 M 5
5 19 M 5
How can I get the decimals?
You data is as expected, it is just the format of the GPA variable was incorrectly set to not have any decimals. You can simply use whats below to set it to show the decimals.
FORMATS GPA (F3.2).
Alternatively you can also try this
DATA LIST / ID 1 AGE 2-3 SEX 4(A) GPA 5-7(F,2).
BEGIN DATA
117M500
317F475
END DATA.
LIST.

Resources