Postgres String Array Comparison - arrays

I have an array of the form :
myArray = ["1234-56", "1234-567"]
My table has a column which is constructed exactly like the array and consists of a string array, we call the column : myColumn.
I want to output the rows where , one or more values of the arrays match.
My current attempt was the following :
SELECT *
FROM myTable
WHERE myColumn && myArray;
But this ends with the following error message:
ERROR: Column "56" does not exist.

A string in double quotes is an “identifier” in SQL, that is the name of a table, column, function or other object.
So when you write
SELECT * FROM mytable
WHERE mycolumn && ARRAY["56","95"];
PostgreSQL will identify "56" as a column name (it couldn't be a table in that context), and it complaint that table mytable has no column called 56.
The solution is to mark 56 as a string literal, that is, surround it with single quotes:
SELECT * FROM mytable
WHERE mycolumn && ARRAY['56','95'];

Related

Only show rows that only contain [a-z0-9]

I have a varchar column that should only contain letters and numbers and no other character, e.g. ; : , # etc
I thought this would work but it doesn't:
select * from table
where field NOT LIKE '[a-z0-9]'
What you are actually after is a LIKE with a not in the pattern:
SELECT *
FROM (VALUES('abc123'),('123'),('O''hare'),(' : , #'))V(field)
WHERE field LIKE '%[^a-z0-9]%';

Perl-Postgres column array and quotes

I want to populate in Perl a postgres column defined as a character array (Tag_List[])
Values may contains single-quote, so when constructing the SQL string, I cannot get the right syntax to enquote these kind of values, since I already must double-enquote string values and single-enquote array type (because of the curly brackets).
I've tried to escape the single-quote (with a $VAL =~ s/\x27/\x5c\x27/g;) but no way :
INSERT INTO "table-name" ("Date","Time"...,"Tag_List"[1],"Tag_List"[2],"Tag_List"[3],"Tag_List"[4],"Tag_List"[5])
VALUES ('2019/10/30', '14:17:59', .... ,'{"LastSuccessfulBackup-com.dellemc.avamar","2019-10-29 20:00:22 UTC"}','{"TAG1","tag value\'with quote"}',...
ERROR: syntax error at or near "with"
LIGNE 1 : ...20:00:22 UTC"}','{"TAG1","tag value\'with quote"}','{...
Any clue ?
Thanks
You can insert the whole array as a string:
CREATE TABLE arr (
id integer PRIMARY KEY,
tag_list character varying(128)[]
);
INSERT INTO arr (id, tag_list) VALUES
(1,
'{normal_string,string with spaces,"string,with,comma","string\"with\"quote","string''with''apostrophe"}'
);
This will result in entries like:
SELECT u.* FROM arr CROSS JOIN unnest(tag_list) AS u;
u
------------------------
normal_string
string with spaces
string,with,comma
string"with"quote
string'with'apostrophe
(5 rows)
An alternative is to insert each array entry individually:
INSERT INTO arr (
id,
tag_list[1],
tag_list[2],
tag_list[3],
tag_list[4],
tag_list[5]
) VALUES (
2,
'normal_string',
'string with spaces',
'string,with,comma',
'string"with"quote',
'string''with''apostrophe'
);
Use bound parameters. (Always use parameter binding to send data into a database.) DBD::Pg supports array parameters by default.
use strict;
use warnings;
my $query = 'INSERT INTO "table-name" ("Date","Time"...,"Tag_List"[1],"Tag_List"[2],"Tag_List"[3],"Tag_List"[4],"Tag_List"[5]) (?,?,...,?,?,?,?,?)';
$dbh->insert($query, undef, $date, $time, ... ['TAG1',"tag value'with quote"], ...);

how to remove double quotes from an Array

I had created a table in hive with ORC format and loaded data into the table. I have use collect_set to eliminate the duplicates as follows to insert the data. However, i see double quotes in the array. Is there anyway to remove those double quotes?
This is an sample data iim getting from table a and inserting into the table b using:
insert into table b
select a.name as name, collect_set(b.sub) as subjects from a group by a.name;
my table be would be like this:
name | subjects
john | ["Eng", "Math", "Phy"]
Sarah | ["Math", "Chem"]
I want to get ride of the double quote in the array to look like this:
name | subjects
john | [Eng, Math, Phy]
Sarah | [Math, Chem]
Is there anyway to do this using hql?
Array is an object and to be displayed it needs to be transformed to string.
When you select array, it is being transformed(serialized) to string. Hive displays array as comma separated values, double quoted, in square brackets.
Consider this example:
select array('Eng', 'Math', 'Phy');
Returns:
["Eng","Math","Phy"]
What I'm trying to say is that there is no double-quotes " in the initial data most probably, it is being serialized to String with double-quotes when you select it directly without explicit conversion to string.
If this is the real reason of double quotes in the select result, then the solution is to transform array to string explicitly:
select concat('[',concat_ws(',',array('Eng', 'Math', 'Phy')),']');
Returns:
[Eng,Math,Phy]
Is it what you expected?
If not and you really need to remove double-quotes from column value, then regexp_replace will do.
Example of array containing double quotes in the values:
select concat('[',concat_ws(',',array('"Eng"', '"Math"', '"Phy"')),']');
Returns:
["Eng","Math","Phy"]
In such case you can apply regexp_replace when loading your table
regexp_replace(string, '["]', '') --this will remove double-qutes
Your insert statement will look like this:
insert into table b select a.name as name, collect_set(regexp_replace(sub, '["]', '')) as subjects from a group by a.name;

PostgreSQL - counting the elements in the JSON

I have a JSON type column called "log_data" and the data stored in it is in the format [{"key":"test123123","identity":"user#test.it","identity_type":"email"}].
I want to count how many records for a given value for a given key in json:
Doesn't work
SELECT count (distinct esas_logs.log_id) AS "count" FROM "esas_logs" WHERE log_data->0->>'identity' = 'user#test.it'
[2016-06-30 13:59:18] [42883] ERROR: operator does not exist: json = unknown
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
use json_array_length()
test=# select json_array_length('[{"key":"test123123","identity":"user#test.it","identity_type":"email"}]');
json_array_length
-------------------
1
(1 row)
According to the doc, you should use the ? operator.
If your column type is JSON:
SELECT COUNT(esas_logs ? 'log_id') FROM esas_logs WHERE ...
If you column is a TEXT or VARCHAR:
SELECT COUNT(esas_logs::jsonb ? 'log_id') FROM esas_logs WHERE ...

Postgres: select array element based on value in other column

I have two column in a postgres table, one containing an array of variable length the other the array position of the elements I want to select.
How do I query these?
testtable
testarray arraypos
a,b,c 3
a NULL
NULL
b,c,d,e 3
a,c 1
What I want is something like:
SELECT testarray[arraypos] FROM testtable;
i.e. getting c, d, a.

Resources