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
时间: 2024-02-14 15:33:44 浏览: 24
这是一个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、共同产品数量以及它们的排名值。
相关问题
select *, row_number()over(partition by id order by salary desc) ranking
这是一个使用`row_number()`函数和`over()`子句的SQL查询语句,用于在每个id分组中按照salary降序排列,并为每个分组中的行分配一个排名。下面是两个例子:
引用的例子:
```sql
with t as (
select *, row_number() over(partition by id order by salary desc) ranking
from tmp_learning_mary
)
select * from t where ranking = 1;
```
这个例子使用了一个公共表表达式(CTE)`t`,在其中使用`row_number()`函数为每个id分组中的行分配了一个排名。然后,查询选择了排名为1的行,即每个分组中的最高薪水行。
引用的例子:
```sql
select * from (
select *, row_number() over(partition by id order by rand()) ranking
from tmp_learning_mary
) t where t.ranking = 1;
```
这个例子与引用的例子类似,只是在`order by`子句中使用了`rand()`函数,它会随机排序每个分组中的行。然后,查询选择了排名为1的行,即每个分组中的随机行。
select *,dense_rank() over (partiton by id order by salary desc) rank from Employee;代码找错
在你提供的 SQL 语句中,关键字 `partiton` 应该是 `partition` 的错误拼写。因此,正确的 SQL 代码应该是:
```
SELECT *, DENSE_RANK() OVER (PARTITION BY id ORDER BY salary DESC) rank FROM Employee;
```
注意到 `DENSE_RANK()` 函数用于计算每个员工的薪资排名,而 `PARTITION BY id` 子句指定了按照 `id` 字段进行分组,这意味着每个员工的排名是在其所在的组内计算的。另外,`ORDER BY salary DESC` 指定了按照薪资降序排序,也就是薪资最高的员工排名最靠前。