Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PostgreSQL中创建高效查询并合并语句_Sql_Postgresql - Fatal编程技术网

在PostgreSQL中创建高效查询并合并语句

在PostgreSQL中创建高效查询并合并语句,sql,postgresql,Sql,Postgresql,请原谅,如果这是琐碎的-我是新使用PostGRESQL。给我两个 表: 我现在要回答一个问题: 不同组中的用户之间的交互百分比是多少, 组中的用户与不在任何组中的用户以及同一组中的用户 团体 这是我的查询-但是,我必须将其合并到一个查询中 我的问题 它们是: 对于下面的查询,在PostGRESQL中是否有更有效的方法 进行这一查询最有效的方法是什么 -使用交互信息将用户\u ID映射到组\u ID CREATE TEMPORARY TABLE group_interaction_informat

请原谅,如果这是琐碎的-我是新使用PostGRESQL。给我两个 表:

我现在要回答一个问题:

不同组中的用户之间的交互百分比是多少, 组中的用户与不在任何组中的用户以及同一组中的用户 团体

这是我的查询-但是,我必须将其合并到一个查询中

我的问题 它们是:

对于下面的查询,在PostGRESQL中是否有更有效的方法

进行这一查询最有效的方法是什么

-使用交互信息将用户\u ID映射到组\u ID

CREATE TEMPORARY TABLE group_interaction_information AS
SELECT 
a.interactionid,
a.user_id, 
b.group_id, 
a.target_user_id, 
c.group_id AS target_group_id
FROM interactions a
LEFT JOIN
group_users b
on a.user_id = b.user_id
LEFT JOIN
group_users c
ON a.target_user_id = c.user_id;
-不同组中用户交互的百分比

select 
(select count(*) from group_interaction_information
where group_id != target_group_id and group_id is not null 
and target_group_id is not null)/(select count(*) from group_interaction_information)::float;
-组中用户交互的百分比

select 
(select count(*) from group_interaction_information
where group_id is not null)/
(select count(*) from group_interaction_information)::float;
-来自不在组中的用户的交互的百分比

select
(select count(*) from group_interaction_information where group_id is null)/
(select count(*) from group_interaction_information)::float;
-来自同一组中用户的交互百分比

select 
(select count(*) from group_interaction_information
where group_id = target_group_id and group_id is not null 
and target_group_id is not null)/(select count(*) from group_interaction_information)::float;

组合查询的一种方法是使用条件聚合:

select 
    count(case when group_id <> target_group_id then 1 end) / count(*)::float,
    count(case when group_id is not null then 1 end) / count(*)::float,
    count(case when group_id is null then 1 end) / count(*)::float,
    count(case when group_id = target_group_id then 1 end) / count(*)::float
from group_interaction_information

顺便说一句,如果一列或两列都为null,则a=b永远不会为true,因此,如果您具有诸如group\u id=target\u group\u id之类的条件,则无需指定,target\u group\u id也不为null。这同样适用于b。要测试运行,请选择null=null,null-null,'a'null-这些都将导致null。

@RDizzl3很高兴我能提供帮助:
select 
    count(case when group_id <> target_group_id then 1 end) / count(*)::float,
    count(case when group_id is not null then 1 end) / count(*)::float,
    count(case when group_id is null then 1 end) / count(*)::float,
    count(case when group_id = target_group_id then 1 end) / count(*)::float
from group_interaction_information