SQL查询过滤特定记录计数的记录

时间:2012-09-25 06:28:49

标签: sql sql-server sql-server-2008 tsql sql-server-2008-r2

我有一个包含Identity,RecordId,Type,Reading和IsDeleted列的表。标识是自动递增的主键,RecordId是可以具有重复值的整数,类型是可以是“一”或“平均”的读取类型,读取是包含任何整数值的整数,IsDeleted是位可以是0或1,即假或真。 现在,我希望查询包含表的所有记录,如果每个RecordId的COUNT(Id)大于2,则显示该RecordId的所有记录。

如果该特定RecordId的COUNT(Id)== 2且两者的读数值即“一”或“平均”类型的记录相同,则仅显示平均记录。

如果COUNT(Id)== 1,则只显示该记录。

例如:

Id          RecordId          Type          Reading       IsDeleted 
1           1                 one             4              0
2           1                 one             5              0
3           1                 one             6              0
4           1                 average         5              0
5           2                 one             1              0
6           2                 one             3              0
7           2                 average         2              0
8           3                 one             2              0
9           3                 average         2              0
10          4                 one             5              0
11          4                 average         6              0
12          5                 one             7              0

Ans结果可以是

Id          RecordId          Type          Reading       IsDeleted 
1           1                 one             4              0
2           1                 one             5              0
3           1                 one             6              0
4           1                 average         5              0
5           2                 one             1              0
6           2                 one             3              0
7           2                 average         2              0
9           3                 average         2              0
10          4                 one             5              0
11          4                 average         6              0
12          5                 one             7              0

简而言之,我想跳过'one'类型的读数,它的平均读数具有相同的值,并且“一”类型读数的计数不超过一个。

3 个答案:

答案 0 :(得分:2)

查看此计划

DECLARE @t TABLE(ID INT IDENTITY,RecordId INT,[Type] VARCHAR(10),Reading INT,IsDeleted BIT)
INSERT INTO @t VALUES
(1,'one',4,0),(1,'one',5,0),(1,'one',6,0),(1,'average',5,0),(2,'one',1,0),(2,'one',3,0),
(2,'average',2,0),(3,'one',2,0),(3,'average',2,0),(4,'one',5,0),(4,'average',6,0),(5,'one',7,0),
(6,'average',6,0),(6,'average',6,0),(7,'one',6,0),(7,'one',6,0)
--SELECT * FROM @t

;WITH GetAllRecordsCount AS                              
(   
    SELECT *,Cnt = COUNT(RecordId) OVER(PARTITION BY RecordId ORDER BY RecordId)
    FROM @t
)
-- Condition 1 : When COUNT(RecordId) for each RecordId is greater than 2 
 --               then display all the records of that RecordId. 
, GetRecordsWithCountMoreThan2 AS
(
    SELECT * FROM GetAllRecordsCount WHERE Cnt > 2
)
-- Get all records where count = 2
, GetRecordsWithCountEquals2 AS
(
    SELECT * FROM GetAllRecordsCount WHERE Cnt = 2
)
-- Condition 3 : When COUNT(RecordId) == 1 then display only that record.
, GetRecordsWithCountEquals1 AS
(
    SELECT * FROM GetAllRecordsCount WHERE Cnt = 1
)

-- Condition 1: When COUNT(RecordId) > 2
SELECT * FROM GetRecordsWithCountMoreThan2  UNION ALL

-- Condition 2 : When  COUNT(RecordId) == 2 for that specific RecordId and Reading value of 
--               both i.e. 'one' or 'average' type of the records are same then display only 
--               average record. 
SELECT t1.* FROM GetRecordsWithCountEquals2 t1
JOIN (Select RecordId From GetRecordsWithCountEquals2 Where [Type] = ('one') )X
ON t1.RecordId = X.RecordId
AND t1.Type = 'average'     UNION ALL   

-- Condition 2: When COUNT(RecordId) = 1
SELECT * FROM GetRecordsWithCountEquals1    

<强>结果

ID  RecordId    Type    Reading IsDeleted   Cnt
1   1            one    4             0     4
2   1            one    5             0     4
3   1            one    6             0     4
4   1            average5             0     4
5   2            one    1             0     3
6   2            one    3             0     3
7   2            average2             0     3
9   3            average2             0     2
11  4            average6             0     2
12  5            one    7             0     1

答案 1 :(得分:1)

假设您的表名为the_table,请执行以下操作:

select main.*
from the_table as main
inner join (
  select recordId, count(Id) as num, count(distinct Reading) as reading_num
  from the_table
  group by recordId
) as counter on counter.recordId=main.recordId
where num=1 or num>2 or reading_num=2 or main.type='average';

未经测试,但应该是其中的一些变体。

编辑 TEST HERE ON FIDDLE

简短摘要是我们想要使用o =本身的聚合版本加入表格,然后根据您提到的计数条件对其进行过滤(num = 1,然后显示它; num = 2,仅显示平均记录如果读数相同则显示两者; num> 2,显示所有记录。

答案 2 :(得分:1)

;with a as
(
select Id,RecordId,Type,Reading,IsDeleted, count(*) over (partition by RecordId, Reading) cnt, 
row_number() over (partition by RecordId, Reading order by Type, RecordId) rn
from table
)
select Id,RecordId,Type,Reading,IsDeleted
from a where cnt <> 2 or rn = 1