database - 我如何为人群建模?

标签 database database-design data-modeling

我需要为一组人建模,但我找不到设计表格的方法来高效地完成它。

组可以被认为是集合,一个或多个人的无序集合,每个组都应该由其组成部分唯一标识。

编辑:一个人可以属于多个组。

我的第一次尝试看起来像这样。 包含系统管理的所有“人员”的表。

table Persons(
  id int,
  name varchar,
  (other data...)
)

包含组和所有组属性的表:

table Groups(
  group_id int,
  group_name varchar,
  (other data...)
)

还有一个人与组之间关联的表格

table gropus_persons (
  person_id int,
  group_id in
)

此设计不符合此要求,因为很难编写查询以从组件列表中检索组 ID。

我唯一能找到由人 (1, 2, 3) 组成的组的查询如下所示:

select *
from groups g
where 
  g.group_id in (select group_id from gropus_persons where person_id = 1) 
  and g.group_id in (select group_id from gropus_persons where person_id = 2) 
  and g.group_id in (select group_id from gropus_persons where person_id = 3)
  and not exists (select 1 from gropus_persons where group_id = g.group_id and person_id not in (1,2,3))

问题是组件的数量是可变的,所以我只能使用动态生成的查询并在每次需要查找新组时为每个组件添加一个子查询。

有没有更好的解决方案?

谢谢各位的指点帮助!

最佳答案

您需要按“组”分组并计算您收到的点击次数。为此,您只需要交集表:

select GroupID, count(*) as MemberCount
from   GroupsPersons
where  PersonID in( 1, 2, 3 )
group by GroupID
having count(*) = 3;

问题在于使此查询适用于不同的人员 ID 值列表。您似乎已经意识到这将需要动态 SQL,伪代码将如下所示:

stmt := 'select GroupID, count(*) as MemberCount '
     || 'from   GroupsPersons '
     || 'where  PersonID in( ' || CSVList || ' ) '
     || 'group by GroupID '
     || 'having count(*) = ' || length( CSVList );

您必须警惕的一个潜在错误是相同的 ID 是否在列表中重复出现。例如:CSVList := '1, 2, 3, 2';

这将生成正确的 count(*) 值 3,但 having 子句将查找 4。

关于database - 我如何为人群建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650856/

相关文章:

mysql - 我如何在mysql中编写一个触发器来在插入之前检查重复记录

arrays - PostgreSQL 跨多行计算 jsonb 数组中的结果

PHP - 从模型加载数据库对象但只有一个实例的最佳方法?

mysql - 很少使用mysql表中的字段会降低性能吗?

algorithm - 存储和比较多维向量

database-design - Redis 中的数据建模实践?

machine-learning - 我可以在 CRF 模型中使用数值特征吗

sql - Mysql:如何创建具有多个主键的表?

mysql - 如何在新安装的WAMP服务器上恢复mysql数据库?

design-patterns - 跟踪用户代币/硬币的策略