Build statistics for a tsvector column - postgresql

I want to build a table where each row contains a string and the number of rows where that string appears as a prefix
Basically I want
select count(*) from "myTable" where tsfield ## (p||':*')::tsquery
for each value of p in an array.
How can I write a query to do this?

Unnest the array and join:
SELECT arr.p, count(*)
FROM "myTable"
JOIN unnest('{...}') AS arr(p)
ON tsfield ## (arr.p||':*')::tsquery
GROUP BY arr.p;

Related

How to extract all entries from a JSON array?

Field name: groups
Value:
[{"GroupId": "abcd-41234", "GroupName": "testingrule"}]
How do to extract the GroupID and GroupName values as a separate fields using select statement?
These are my failed attempts:
select groups->>'GroupId' as id,
groups->>'GroupName' as name`
from table_name
select (groups::json->>'GroupId')::json->>'id' as id
from table_name
select groups::json->>'GroupId' as id
from table_name`
I assume that you are using the jsonb data type. If not, change your table definition.
If you want the values for all array elements for all rows in the table, you would use a lateral join like this:
SELECT exp.j ->> 'GroupId' AS groupid,
exp.j ->> 'GroupName' AS groupname
FROM table_name AS t
CROSS JOIN LATERAL jsonb_array_elements(t.groups) AS exp(j);

Search any subquery bigint is exist in bigint array posgreSQL

I need to query a table in an array column using a subquery that is returning multiple records.
If any one of the subquery ids match in the array_column. The row should be returned in the select query.
Eg:
SELECT * FROM table_a WHERE bigint_array_column IN (
SELECT id FROM table_b
)
bigint_array_column is '{1, 2, 3, 4, 5, 6}'
Eg:
subquery data is '10, 1'.
Expected output : All the record which has 10 and 1 has to be retured.
If any one of the subquery ids exists in the bigint_array_column. I want that record to be selected in the query of table_a
I know we can do it by UNNEST the column in the select query and search it with a subquery. Is there any simpler method to do this?
ANY is working if the subquery returns only one record. but not for multiple records.
Thanks in advance.

Split dynamic array row data into column names in hive

Problem statement: How to convert a row value, which has a dynamic array into column names in Hive.
Eg: table name: TAB1
Col1 Col2
{a,b,c}
{a,b,d,e}
{e,c,a,m,n}
Required output: I somehow need to split the row data dynamic array into column names based on the Col1 as the filter on TAB1.
The Final query needs to be something like below ( i know some sort of JOIN to TAB1 is required)
select 1,a,b,c from TAB2;
select 2,a,b,d,e from TAB2;
select 3,e,c,a,m,n from TAB2;
Try this -
SELECT CONCAT("SELECT ",concat_ws(',', t.*)," FROM TAB2;") FROM TAB1;

How to get a row for each occurrence of Id in IN clause?

Given that I have a list of repeating ids that I need to fetch some additional data to populate xls spreadsheet, how can I do that. "IN" clause returns only 1 match, but I need a row for each occurrence of an Id. I looked at PIVOT, thinking I could create a select list and then do inner join.
Select m.Id, m.LegalName, m.OtherId
from MyTable m
where m.OtherId in (1,2,1,1,3,1,4,4,2,1)
You can use VALUES clause :
SELECT t.id as OtherId, m.id, m.LegalName
FROM ( VALUES (1),(2),(1),(1),(3),(1),(4),(4),(2),(1)
) t(ID) INNER JOIN
MyTable m
ON m.OtherId = t.id;

PostgreSQL group by all fields

I have a query like this:
SELECT
table1.*,
sum(table2.amount) as totalamount
FROM table1
join table2 on table1.key = table2.key
GROUP BY table1.*;
I got the error: column "table1.key" must appear in the GROUP BY clause or be used in an aggregate function.
Are there any way to group "all" field?
There is no shortcut syntax for grouping by all columns, but it's probably not necessary in the described case. If the key column is a primary key, it's enough when you use it:
GROUP BY table1.key;
You have to specify all the column names in group by that are selected and are not part of aggregate function ( SUM/COUNT etc)
select c1,c2,c4,sum(c3) FROM totalamount
group by c1,c2,c4;
A shortcut to avoid writing the columns again in group by would be to specify them as numbers.
select c1,c2,c4,sum(c3) FROM t
group by 1,2,3;
I found another way to solve, not perfect but maybe it's useful:
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table
Then apply this select result to main query like this:
$columns = $result[0]["columns"];
SELECT
table1.*,
sum(table2.amount) as totalamount
FROM table1
join table2 on table1.key = table2.key
GROUP BY $columns;