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 20:33:54 浏览: 86
这段代码是一个 SQL 查询语句,用于从 property 表中查询具有相同属性值的不同 pro_c_id 的组合,并计算每个组合中的记录总数。具体来说,这个查询首先使用了子查询的方式将具有相同 pro_pif_id 的记录组合在一起,并计算出它们的共同属性数量。接着,查询使用了窗口函数 rank() over() 来对每个 pac 进行分组,并按照共同属性数量(common)和 pbc 的升序进行排序,从而为每个 pac 按照共同属性数量和 pbc 的排名(crank)进行编号。最后,查询使用了另一个子查询将 crank 小于 3 的记录筛选出来,并返回每个记录的所有字段。其中 crank 小于 3 的记录表示每个 pac 中与它共同具有属性最多的前两个 pbc。
相关问题
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` 指定了按照薪资降序排序,也就是薪资最高的员工排名最靠前。