select t1.pro_c_id from ( ( select pro_c_id, count(distinct(pro_pif_id)) as cnt1 from property, finances_product where pro_pif_id = p_id and pro_type = 1 group by pro_c_id ) as t1 join ( select pro_c_id, count(distinct(pro_pif_id)) as cnt2 from property, fund where pro_pif_id = f_id and pro_type = 3 group by pro_c_id ) as t2 on t1.pro_c_id = t2.pro_c_id )
时间: 2024-04-23 09:23:20 浏览: 11
这是一个SQL语句,根据两个子查询的结果,对两个表进行join操作,最终返回pro_c_id,即具有某些属性和基金的公司的ID。具体的查询条件是:
在属性表和finances_product表中,如果属性的p_id等于产品的pro_pif_id,并且属性类型为1,则计算每个公司拥有的不同产品数量,并将结果作为t1表。
在属性表和fund表中,如果属性的f_id等于基金的pro_pif_id,并且属性类型为3,则计算每个公司拥有的不同基金数量,并将结果作为t2表。
最后将t1表和t2表按照pro_c_id进行join操作,并返回pro_c_id。
相关问题
select * from ( select *, rank() over(partition by pac order by common desc,pbc asc) as crank from ( select distinct t1.pro_c_id as pac, t2.pro_c_id as pbc, count(t2.pro_c_id) as common from property as t1 join ( select distinct pro_c_id, pro_pif_id from property where pro_type = 1 )as t2 on t2.pro_pif_id = t1.pro_pif_id where pro_type = 1 and t2.pro_c_id <> t1.pro_c_id group by t1.pro_c_id, t2.pro_c_id ) as t3 ) as t4 where crank <3
这是一个SQL语句,用于查询拥有相同产品的公司,并找出其中拥有相同产品数量前两名的公司组合。具体的查询条件是:
首先从属性表中选择属性类型为1的记录,并根据产品ID(pro_pif_id)和公司ID(pro_c_id)进行去重,将结果作为t1表。
然后从t1表中选择公司ID和产品ID,并根据产品ID进行去重,将结果作为t2表。
接下来将t1表和t2表进行join操作,条件是t2表中的产品ID等于t1表中的产品ID,并且t2表中的公司ID不等于t1表中的公司ID。然后根据t1表中的公司ID和t2表中的公司ID进行分组,并计算它们拥有的相同产品数量,将结果作为t3表。
在t3表中,按照拥有相同产品数量从大到小排序,并且如果数量相同,则按照公司ID从小到大排序,最终计算每个公司排名值,将结果作为t4表。
最后在t4表中,选择排名值小于2的记录,并返回公司ID、共同产品数量以及它们的排名值。
ith pro(c_id, f_id) as ( select pro_c_id c_id, group_concat(distinct pro_pif_id order by pro_pif_id) f_id from property where pro_type = 3 group by pro_c_id ) select t1.c_id c_id1, t2.c_id c_id2 from pro t1, pro t2 where t1.c_id < t2.c_id and t1.f_id = t2.f_id;
这是一个SQL查询语句,它使用了子查询和自连接来查找拥有共同f_id的不同c_id的组合。
首先,在with子句中定义了一个名为pro的子查询,它从property表中选择所有pro_type为3的记录,然后按pro_c_id进行分组,并使用group_concat函数将每个组中的pro_pif_id连接成一个字符串,保存在f_id列中。
然后,在主查询中,使用自连接将pro表与自身连接起来,找出所有拥有相同f_id但不同c_id的组合。最后,使用where子句过滤掉重复的组合。查询结果包括两个列:c_id1和c_id2,分别表示两个拥有共同f_id的不同c_id。
总的来说,这个查询是用来查找投资基金(pro_type=3)的投资者之间是否存在共同的投资项目(f_id相同),如果存在,则返回这些投资者的组合。