2012-09-25 81 views
2

我有一个表有Identity,RecordId,Type,Reading和IsDeleted列。 Identity是自动增量的主键,RecordId是可以有重复值的整数,Type是可以是'one'或'average'的读数类型,Reading是包含任何整数值的整数,IsDeleted是可以是0或1,即错误或真实。 现在,我希望包含所有记录的查询以这种方式,如果每个RecordId的COUNT(Id)大于2,那么显示该RecordId的所有记录。SQL查询过滤记录的计数记录

如果该特定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 

答案结果可以是

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 

总之我想跳过其具有相同的值,其为“计数的平均读数的“一”型读取一个“类型的阅读不超过一个。任何人都可以有想法?

+5

人都标记下来,但是不准备,为什么(很烦人)评论 - 问题不太清楚了,但还有很多更糟 - 你有没有试图写一个查询呢?你能告诉我们你的尝试吗? – whytheq

+1

更糟糕的是,收到upvote 8 :( – codingbiz

回答

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 
+0

Id 2已经过去了:[FIDDLE](http://sqlfiddle.com/#!3/cadae/20) – whytheq

+0

谢谢。它做了我的工作.. – Dev

+1

@DevendraGohil - 如果这_did工作_那么为什么是原始帖子中的记录id = 2,但不是由这个答案返回?!你原来的帖子是错的吗? – whytheq

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

简短的总结是,我们要加入表与邻者的聚集版本=本身,然后筛选它基于你所提到的数量标准(NUM = 1,则表明它; num = 2,只显示平均记录,如果读数是相同的,否则显示; num> 2,显示所有记录)。

+0

谢谢。但这里没有必要所有记录的平均值必须存在。我希望如果一个'1'类型的记录只是单个记录,并且它的记录Id是存在的请记住'one'类型的记录,然后丢弃'one'类型的记录 – Dev

+0

请记住,只有'num = 2'的记录才会触发'main.type ='average''的添加,因为'num = 1或num> 2将覆盖其他基地,where子句可以被重写为'(num = 1或num> 2或(num = 2和main.type ='average'))',但这样做会是多余的,因此我不需要,所以我把它关掉了。 – chops

+1

似乎工作:[** SQL FIDDLE **](http://sqlfiddle.com/#!3/cadae/2)...实际上Id 10已被排除因为 – whytheq

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