Skip to content

Tag: postgresql

Postgresql reference a variable within statement

I come across this scenario frequently in sql and I have seen it done, but would like to summarize the case when/when not to reference a new variable within a statement. This doesn’t work – but this does – How can I implement scenario 1? Answer You can use a subquery.

PostgreSQL create index on JSONB[]

Consider a table defined as follows: How can I create an index on json key type and how can I query on it? Edit: Previously, there were no indexes on json keys and select queries used an unnest operation as shown below: The problem is, if the table has a large number of rows, the above query will not only

How to import a JSON file into postgresql databse?

I just tried to read a .JSON file in my PostgreSQL database but it is not able to read it. I did it with a .csv file, but with a .JSON file, I am not able to do it. This is how the .JSON file looks like: This is the code that I tried, I created the table first and

How to grouping postgresql when id is same into new add value

I have table like this: And I would like to bring value item in one row when user_input_id and question_id is duplicate. The result that I wish is this: Can anyone tell me how to querying it? Thank You Answer You can easily do taht with string_agg(value,’,’) in postgresql. Output: You can also hav…

Finding top 10 products sold in a year

I have these tables below along with the definition. I want to find top 10 products sold in a year after finding counts and without using aggregation and in an optimized way. I want to know if aggregation is still needed or I can accomplish it without using aggregation. Below is the query. Can anyone suggest …