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 17:34:02 浏览: 21
这是一个 MySQL 查询语句,用于查找所有在 property 表中持有相同基金的用户对,并按照用户 ID 排序。查询结果包括两个用户 ID 和他们共同持有的基金数量。查询中使用了窗口函数和嵌套查询。首先,在子查询中,通过关联 property 表自身,找出所有持有相同基金的用户对,并统计他们共同持有的基金数量。然后,在外层查询中使用 rank() 窗口函数,按照共同持有基金数量和第二个用户 ID 升序排序,并为每个用户对分配排名。最后,在外层查询中筛选排名小于 3 的用户对,即共同持有基金数量前两名的用户对。
相关问题
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` 指定了按照薪资降序排序,也就是薪资最高的员工排名最靠前。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)