Sql 仅选择完整集
我只想从每种类型中选择至少有一行的值Sql 仅选择完整集,sql,sql-server-2008,Sql,Sql Server 2008,我只想从每种类型中选择至少有一行的值 File|Value |Type 001 |"Crektolap" |1 001 |"123.45" |2 001 |"0.00" |4 002 |"Cream floure" |1 002 |"56.89" |2 002 |"Supercat" |3 002 |"0.01" |4 有:1、2、3、4种类型 我想要的结果是: File|
File|Value |Type
001 |"Crektolap" |1
001 |"123.45" |2
001 |"0.00" |4
002 |"Cream floure" |1
002 |"56.89" |2
002 |"Supercat" |3
002 |"0.01" |4
有:1、2、3、4种类型
我想要的结果是:
File|Value |Type
002 |"Cream floure" |1
002 |"56.89" |2
002 |"Supercat" |3
002 |"0.01" |4
这甚至可以在一个查询中实现吗?您可以使用窗口函数实现这一点:
select file, value, type
from (select t.*, max(dr) over (partition by file) as numtypes
from (select t.*, dense_rank() over (partition by file order by type) as dr
from table t
) t
) t
where numtypes = 4;
稠密_秩统计每行上枚举的不同类型值的数量。max然后获得最大值,外部查询选择有四种类型的行
如果您不知道类型的数量:
select file, value, type
from (select t.*, max(dr) over (partition by file) as numtypes
from (select t.*, dense_rank() over (partition by file order by type) as dr
from table t
) t
) t
where numtypes = (select count(distinct type) from table t);
所以你不想要文件001,因为它缺少类型3?