Type error of getting average by id in KDB - kdb

I am trying make a function for the aggregate consumption by mid in a kdb+ table (aggregate value by mid). Also this table is being imported from a csv file like this:
table: ("JJP";enlist",")0:`:data.csv
Where the meta data is for the table columns is:
mid is type long(j), value(j) is type long and ts is type timestamp (p).
Here is my function:
agg: {select avg value by mid from table}
but I get the
'type
[0] get select avg value by mid from table
But the type of value is type long (j). So I am not sure why I can't get the avg I also tried this with type int.

Value can't be used as a column name because it is keyword used in kdb+. Renaming the column should correct the issue.

value is a keyword and should not be used as a column name.
https://code.kx.com/q/ref/value/
You can remove it as a column name using .Q.id
https://code.kx.com/q/ref/dotq/#qid-sanitize
q)t:flip`value`price!(1 2;1 2)
q)t
value price
-----------
1 1
2 2
q)t:.Q.id t
q)t
value1 price
------------
1 1
2 2
Or xcol
https://code.kx.com/q/ref/cols/#xcol
q)(enlist[`value]!enlist[`val]) xcol t
val price
---------
1 1
2 2

You can rename the value column as you read it:
flip`mid`val`ts!("JJP";",")0:`:data.csv

Related

KDB How to update column values

I have a table which has column of symbol type like below.
Name
Value
First
TP_RTD_FRV
Second
RF_QWE_FRV
Third
KF_FRV_POL
I need to update it as below, wherever I have FRV, I need to replace it with AB_FRV. How to achieve this?
Name
Value
First
TP_RTD_AB_FRV
Second
RF_QWE_AB_FRV
Third
KF_AB_FRV_POL
q)t
name v
---------------
0 TP_RTD_FRV
1 RF_QWE_FRV
2 KF_FRV_POL
3 THIS
4 THAT
q)update `$ssr[;"FRV";"AB_FRV"]each string v from t
name v
------------------
0 TP_RTD_AB_FRV
1 RF_QWE_AB_FRV
2 KF_AB_FRV_POL
3 THIS
4 THAT
or without using qSQL
q)#[t;`v;]{`$ssr[;"FRV";"AB_FRV"]each string x}
name v
------------------
0 TP_RTD_AB_FRV
1 RF_QWE_AB_FRV
2 KF_AB_FRV_POL
3 THIS
4 THAT
Depending on the uniqueness of the data, you might benefit from .Q.fu
q)t:1000000#t
q)\t #[t;`v;]{`$ssr[;"FRV";"AB_FRV"]each string x}
2343
q)\t #[t;`v;].Q.fu {`$ssr[;"FRV";"AB_FRV"]each string x}
10

Change the column type to 'Date' from 'Character varying' in PostgreSQL 11.0

I have a postgreSQL 11.0 table with dates in below format, but the column type 'character varying'.
id primary_completion_date
0 December2019
1 April2020
2 September2021
3 September2022
4 December2021
Is it possible to convert the column type to 'date'. When I try to convert it to date, It changes the column content to below:
id primary_completion_date
0 12-2019-01
1 04-2020-01
2 09-2021-01
3 09-2022-01
4 12-2021-01
I am using following statement in my script.
alter table tbl alter primary_completion_date type date using to_date(primary_completion_date, 'MonthYYYY')
How can I retain the column content as the input but change the column type to 'Date'.
How can I retain the column content as the input but change the column type to 'Date'*"
You can't because December2019 is not a valid value for a date column.
But you can always format the date value when you retrieve it:
select id, to_char(primary_completion_date, 'MonthYYYY') as formatted_completion_date
from the_table

Extracting data from a table in kdb

Kdb question:
They're multiple rows in a table and I want to check if all the rows if the column meets a condition.
So the column StartDay = ***
How can I check each single row for that column?
Select from t where StartDay = '$"***"
Just gives me type errors.
Any help would be appreciated !
Assuming the column StartDay is of date type like in the following example
q)show t:([]StartDay:.z.d+til 3;number:til 3;sym:`abc`def`ghi)
StartDay number sym
---------------------
2021.02.19 0 abc
2021.02.20 1 def
2021.02.21 2 ghi
Then the following query will work
q)select from t where StartDay=2021.02.19
StartDay number sym
---------------------
2021.02.19 0 abc
The example you have given seems like you are trying to query a column of symbol type. Here are two examples of that
q)select from t where sym=`$"ghi"
StartDay number sym
---------------------
2021.02.21 2 ghi
q)select from t where sym=`ghi
StartDay number sym
---------------------
2021.02.21 2 ghi
Perhaps the following guide on where in q-sql will help.

How do I define a record type for JSON in postgres

Looking at postgres documentation for JSON functions (https://www.postgresql.org/docs/9.6/static/functions-json.html), there is a section I don't understand about expanding a JSON object into a set of rows.
The docs give a sample use of this function: json_populate_recordset(base anyelement, from_json json) as select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]')
But I'm not sure what that first argument (null::myrowtype) is -- a table definition?
The description of this function is: Expands the outermost array of objects in from_json to a set of rows whose columns match the record type defined by base (see note below).
None of the notes at the bottom seemed relevant. I'm hoping for a better explanation with sample code to understand it all.
The 2nd notice is the one of interest in the doc as it explains how missing fields/values are handled
Note: In json_populate_record, json_populate_recordset, json_to_record
and json_to_recordset, type coercion from the JSON is "best effort"
and may not result in desired values for some types. JSON keys are
matched to identical column names in the target row type. JSON fields
that do not appear in the target row type will be omitted from the
output, and target columns that do not match any JSON field will
simply be NULL.
json_populate_recordset maps the name of the json object to the column name in the table given as first argument.
create table public.test (a int, b text);
select * from json_populate_recordset(null::public.test, '[{"a":1,"b":"b2"},{"a":3,"b":"b4"}]');
a | b
---+----
1 | b2
3 | b4
(2 rows)
--Wrong column name:
select * from json_populate_recordset(null::public.test, '[{"a":1,"c":"c2"},{"a":3,"c":"c4"}]');
a | b
---+---
1 |
3 |
(2 rows)
--Wrong datatype:
select * from json_populate_recordset(null::public.test, '[{"a":1.1,"b":22},{"a":3.1,"b":44}]');
ERROR: invalid input syntax for integer: "1.1"
Alternatively, instead of using the column name/type from an existing table, you can define the columns on the fly
select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text);
a | b
---+-----
1 | foo
2 |
(2 rows)
--> note that default type cast occurs ("2" is mapped to 2), missing fields are ignored (b, in second record) as well as fields not defined (c)

Compress Date Range

I have a bunch of records in the format of
DATE TYPE
---------- ------
06/21/2014 TYPE A
06/22/2014 TYPE A
06/23/2014 TYPE A
06/24/2014 TYPE B
06/25/2014 TYPE C
06/26/2014 TYPE C
06/27/2014 TYPE A
...
I would like to print out a report that showed the range of type where these parameters existed in sequence.
START END LENGTH
---------- ---------- ------
TYPE A 06/21/2014 06/23/2014 3
TYPE B 06/24/2014 06/24/2014 1
TYPE C 06/25/2014 06/26/2014 2
TYPE A 06/27/2014 06/27/2014 1
Is there any way to display the data in this format from the schema I have been given?
Try the following:
1) Create a group by "type". In the group expert, configure it so that it will not order the result (i think it is called "in original order").
2) Suppress the detail section.
3) In the group footer, show the "type" field, put a summarize of minimum "date" (the start), put a summarize of maximum "date" (the end), put a summarize of count (the length).