简体   繁体   English

如何将频率计数合并回 postgresql 中的表?

[英]How do I merge a frequency count back onto a table in postgresql?

I am attempting to count the number of each category and merge it back onto the table by overwriting the table in postgresql.我试图计算每个类别的数量,并通过覆盖 postgresql 中的表将其合并回表中。

This is the main table I have (Named Titanic, containing the columns in question):这是我的主表(名为泰坦尼克号,包含有问题的列):

PassengerId乘客编号 Group团体
0001_01 0001_01 1 1个
0002_01 0002_01 2 2个
0003_01 0003_01 3 3个
0003_02 0003_02 3 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.我通过添加一个新的数字列“GroupSize”更改了表格,我想在其中包含每个组类别的频率计数。 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";所以记录 1 的计数为 1,记录 2 的计数为 1,记录 3 和 4 的计数均为 2。我希望保留我的主“泰坦尼克号”表,而不是创建一个新表表或视图非常理想地使用“更新”语句将值归因于“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个 1 1个
2 2个 1 1个
3 3个 2 2个

And I've tried an Update statement to use this view to add data into my "GroupSize" column from "Titanic" like such:我已经尝试使用更新语句使用此视图将数据从“泰坦尼克号”添加到我的“GroupSize”列中,如下所示:

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".我未能成功使此 UPDATE 语句起作用,主要是因为我收到错误消息:“用作表达式的子查询返回的行不止一行”。 I am pretty new to SQL so ny help would be appreciated.我是 SQL 的新手,所以非常感谢您的帮助。

You almost had it right.你几乎是对的。 The value used in SET is dynamic based off the row being modified. SET 中使用的值是基于正在修改的行的动态值。 All you have to do is add a WHERE clause to it to ensure it picks the right value from the view.您所要做的就是向它添加一个 WHERE 子句,以确保它从视图中选择正确的值。

UPDATE "Titanic"
    SET "GroupSize" = (
        SELECT "GroupSize" from "GroupSize"
        where "Titanic"."Group" = "GroupSize"."Group"
        -- (Pedantic safety limit, just in case)
        limit 1
    )

Beware, though, this will modify every row, setting NULL for values not found in the view.但是请注意,这将修改每一行,为视图中未找到的值设置 NULL。 To have it preserve "GroupSize" column for rows without a match in the view, tack on another WHERE clause:要让它为视图中没有匹配项的行保留“GroupSize”列,请添加另一个 WHERE 子句:

UPDATE "Titanic"
    SET "GroupSize" = (
        SELECT "GroupSize" from "GroupSize"
        where "Titanic"."Group" = "GroupSize"."Group"
        limit 1
    )
    WHERE "Group" IN (SELECT "Group" from "GroupSize");

Do not actually Update you main table, just create the view to hold the group size.实际上不要更新你的主表,只是创建视图来保存组大小。 This eliminates maintenance headaches when performing DML on the table, image what extra you need to transfer one group to another.这消除了在表上执行 DML 时令人头疼的维护问题,想象一下将一个组转移到另一个组需要什么额外的东西。 With the count only in the view, you do nothing extra.如果仅在视图中显示计数,您无需执行任何额外操作。 You get the count of id in each group with the window version of count.您可以使用 window 版本的计数获得每个组中的 id 计数。 (see demo ) (见演示

create or replace view titanic_vw as 
 select passengerid "Passenger Id" 
      , passenger_group "Group"
      , count(*) over (partition by passenger_group) "Group Size"
   from  titanic;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM