I am attempting to count the number of each category and merge it back onto the table by overwriting the table in postgresql.
This is the main table I have (Named Titanic, containing the columns in question):
PassengerId | Group |
---|---|
0001_01 | 1 |
0002_01 | 2 |
0003_01 | 3 |
0003_02 | 3 |
I've altered the table by adding a new numeric column "GroupSize" which I want to contain the frequency counts of each group category. So record 1, would be a count of 1, record 2 would be a count of 1 and record 3 and 4 would both be a count of 2. And I want my main "Titanic" table to be retained as opposed to creating a new table or view so ideally using an "Update" statement to impute values into "GroupSize";
I have created a view to contain group the corresponding frequency counts from this code:
CREATE OR REPLACE VIEW "GroupSize"("Group", "GroupSize") AS
select "Group", count("Group") from "Titanic" GROUP BY "Group";
which outputs this:
Group | GroupSize |
---|---|
1 | 1 |
2 | 1 |
3 | 2 |
And I've tried an Update statement to use this view to add data into my "GroupSize" column from "Titanic" like such:
UPDATE "Titanic"
SET "GroupSize" = (SELECT "GroupSize" from "GroupSize")
WHERE "Group" IN (SELECT "Group" from "GroupSize");
I have been unsuccessful in getting this UPDATE statement to work mainly because I get an error: "more than one row returned by a subquery used as an expression". I am pretty new to SQL so ny help would be appreciated.