2012-09-25 77 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