Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 仅选择完整集_Sql_Sql Server 2008 - Fatal编程技术网

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?