pg-promise search for VARCHAR value in an array - pg-promise

I have a table that has a column tagging_contexts with a type of VARCHAR[]
If I query the column in the command line I get the following output:
select tagging_contexts FROM my_table where id=92;
tagging_contexts
--------------------
{"User Interface"}
(1 row)
I would like to select all from any row that has the value "User Interface" in the tagging_contexts column array. My query however, is not working:
let context = "User Interface";
return db.any('SELECT * FROM my_table WHERE $1 = ANY ($/tagging_contexts:csv/)', context)
...
My expected output would be to get the row with the ID of 92, any help is appreciated!

Related

Query to search over array elements inside jsonb PSQL

I have a JSON node on which I have to write a PSQL query, My table schema name(String),tagValues(jsonb). Example tagValue data is given below
Name_TagsTable
uid | name(String)| tagValues(jsonb)
-----+-------------------+-----------------------------
1 | myName | { "tags": ["xyz","pqr","xyp"]}
I need a query that returns all rows for a search "pq" made on the tagValues of the table
select * from Name_TagsTable where tagValues->tags contains %pq%
You can use LIKE operator along with casting JSONB value to a string type such as
SELECT *
FROM Name_TagsTable
WHERE (tagValues->'tags')::TEXT LIKE '%pq%'
You need to unnest the elements, then you can use it in a WHERE condition that applies a LIKE condition.
select nt.*
from name_tagstable nt
where exists (select *
from jsonb_array_elements_text(tagvalue -> 'tags') as a(tag)
where a.tag like '%pg%');

PSQL - UPDATE tableA off tableB where Column1 Match

I have a broken column with null values, however I have managed to import the data off a csv into TempTable
MediaRecords - localpath column is null
TempTable - localpath column is correct
UPDATE mediarecords
SET localpath = TempTable.localpath
FROM TempTable
WHERE recordid = TempTable.recordid;
I keep getting ERROR: relation "temptable" does not exist
LINE 3: FROM TempTable
however I can browse the table and see the data.
I tried following this How to update selected rows with values from a CSV file in Postgres? and here we are
Hu Bucky, can you check how the table is actually called because I see you referring at it as TempTable with camelcase and the error states temptable all lowercase.
PostgreSQL could be case sensitive. As example if you do the following
create table "TempTableABC" (id int);
Trying to select from temptableABC will fail
defaultdb=> select * from temptableABC;
ERROR: relation "temptableabc" does not exist
LINE 1: select * from temptableABC;
^
You'll need to use the same quoted syntax to make it work
defaultdb=> select * from "TempTableABC";
id
----
(0 rows)
UPDATE mediarecords
SET localpath = "TempTable".localpath
FROM public."TempTable"
WHERE "mediarecords".recordid = "TempTable".recordid;
Worked

return table as function value

I'm trying to return table as function value.
My code, but gives syntax error:
drop function if EXISTS fn_must_edukas;
CREATE FUNCTION fn_must_edukas()
returns Table as
return(
Select Top 1 v_mangijad.isik_nimi as mängijaNimi,(SUM(punkt)) as punktid_mustad from v_punkti join
v_mangijad on v_mangijad.isik_id=v_punkti.mangija
where varv='M'
GROUP BY mängijaNimi
order by punktid_mustad desc);
select fn_must_edukas()
Thanks!
You have one problem in the last line:
select fn_must_edukas()
It should be:
select * FROM fn_must_edukas()
Because you are returning a table.

Cast a PostgreSQL column to stored type

I am creating a viewer for PostgreSQL. My SQL needs to sort on the type that is normal for that column. Take for example:
Table:
CREATE TABLE contacts (id serial primary key, name varchar)
SQL:
SELECT id::text FROM contacts ORDER BY id;
Gives:
1
10
100
2
Ok, so I change the SQL to:
SELECT id::text FROM contacts ORDER BY id::regtype;
Which reults in:
1
2
10
100
Nice! But now I try:
SELECT name::text FROM contacts ORDER BY name::regtype;
Which results in:
invalid type name "my first string"
Google is no help. Any ideas? Thanks
Repeat: the error is not my problem. My problem is that I need to convert each column to text, but order by the normal type for that column.
regtype is a object identifier type and there is no reason to use it when you are not referring to system objects (types in this case).
You should cast the column to integer in the first query:
SELECT id::text
FROM contacts
ORDER BY id::integer;
You can use qualified column names in the order by clause. This will work with any sortable type of column.
SELECT id::text
FROM contacts
ORDER BY contacts.id;
So, I found two ways to accomplish this. The first is the solution #klin provided by querying the table and then constructing my own query based on the data. An untested psycopg2 example:
c = conn.cursor()
c.execute("SELECT * FROM contacts LIMIT 1")
select_sql = "SELECT "
for row in c.description:
if row.name == "my_sort_column":
if row.type_code == 23:
sort_by_sql = row.name + "::integer "
else:
sort_by_sql = row.name + "::text "
c.execute("SELECT * FROM contacts " + sort_by_sql)
A more elegant way would be like this:
SELECT id::text AS _id, name::text AS _name AS n FROM contacts ORDER BY id
This uses aliases so that ORDER BY still picks up the original data. The last option is more readable if nothing else.

postgresql return the first positive value from multiple columns queried

I have a table in a POstgreSQL database with multiple columns out of which only one will have a value entered.
SELECT "Garden_GUID", "Municipality_Amajuba", "Municipality_Ilembe", "Municipality_Sisonke" from forms_garden
WHERE "Garden_GUID" = 'testguid';
Garden_GUID | Municipality_Amajuba | Municipality_Ilembe | Municipality_Sisonke
-------------+----------------------+---------------------+----------------------
testguid | Dannhauser | |
(1 row)
I wish to create a view in which the entries from those columns are colated into a single column.
I have tried:
CREATE VIEW municipality (GUID,funder,municipality)
AS SELECT "Garden_GUID"GUID,"Funder"funder,"Municipality_Amajuba","Municipality_Ilembe","Municipality_Sisonke"municipality
FROM forms_garden;
but it returns an error:
ERROR: column "municipality" specified more than once
Is there any way to query the various municipality_* columns row by row and only return the first positive entry?
Many thanks in advance.
I think coalesce() is what you're looking for:
with forms_garden as (
select 'guid1' guid, 'Dannhauser' amajuba, null ilembe, null sisonke
union all select 'guid2', null, 'muni2', null
union all select 'guid3', null, null, 'muni3'
) select guid, coalesce(amajuba,ilembe,sisonke) municipality from forms_garden;