1

I am very new to postgresql. I have this table.

enter image description here

From this table I want the data like

region                country          count
---------------------------------------------------
UttarPradesh           India              16
California            United State        2

How to achieve this task. I did something but not properly end with the solution here is my queries

CREATE TEMP TABLE temp1 AS
    (SELECT distinct region_name, country_name,  null::integer as "count"
     from opens where track_id=42);

After this I read this result then in a loop run update query with select statement.

So it is a right way ? Or any another way to achieve this task ?

1

2 Answers 2

5

Why a subquery or temp tables, why not just

SELECT region_name, country_name, COUNT(*) 
FROM opens WHERE track_id = 42 GROUP by region_name, country_name

It's extremely rare in postgresql to have to use a temporary table. If you want to update this or another table with this data, you can easily do so with a well thought out query. Looping is equally undesirable.

Sign up to request clarification or add additional context in comments.

Comments

1
SELECT region_name, country_name, COUNT(*)
FROM t GROUP BY region_name, country_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.