2012-04-06 54 views
4

從下面的表中爲每個IssID FieldID組,我想要選擇包含最低ChgGrpID值的行,如果行中有一行將該字段分組爲空,然後仍然選擇最低的非空值。如果只有一行存在NULL,那麼爲該組選擇該行。根據可以有值或爲空的字段在組內選擇行

create table #Projects 
(ProjectID int, IssID int, PtID int, PTY varchar(10), TypeID int, TypeName varchar(20), FieldID int, FieldName varchar(20),   STRINGVALUE varchar(50), NUMBERVALUE int,ChgGrpID int,ChgGrpIssID int,ChgItemID int,ChgItemGrpID int,FIELD varchar(20),   NEWVALUE varchar(20), NEWSTRING varchar(20)) 
insert into #Projects values 
(10879,107930,3,'Super',22,'A',10648,'ADH',NULL,666,501040,107930,852895,501040,'ADH',NULL,'666') 
,(10879,107930,3,'Super',22,'A',10571,'DLV','No',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10541,'CMPLX','Large',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10542,'EWF','Orange',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10654,'WKFL','UAT',NULL,501034,107930,852889,501034,'WKFL','DVP','CRV') 
,(10879,107930,3,'Super',22,'A',10654,'WKFL','UAT',NULL,501037,107930,852892,501037,'WKFL','CRV','UAT') 
,(10879,107930,3,'Super',22,'A',10654,'WKFL','UAT',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10648,'ADH',NULL,999,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10571,'DLV','No',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10541,'CMPLX','Large',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10542,'EWF','Orange',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10654,'WKFL','UAT',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 


select * from #Projects 
order by IssID, FieldID 

這是我想看到的結果是:

insert into #Projects values 
(10879,107930,3,'Super',22,'A',10648,'ADH',NULL,666,501040,107930,852895,501040,'ADH',NULL,'666') 
,(10879,107930,3,'Super',22,'A',10571,'DLV','No',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10541,'CMPLX','Large',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10542,'EWF','Orange',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107930,3,'Super',22,'A',10654,'WKFL','UAT',NULL,501034,107930,852889,501034,'WKFL','DVP','CRV') 
,(10879,107971,3,'Super',103,'B',10648,'ADH',NULL,999,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10571,'DLV','No',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10541,'CMPLX','Large',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10542,'EWF','Orange',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
,(10879,107971,3,'Super',103,'B',10654,'WKFL','UAT',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) 
+0

我討厭像這樣的問題。沒有進攻,但這些在這裏是我爲我做功課的。你可以嘗試分解一下,嘗試一下,併發回你真正需要的東西嗎? – JonH 2012-04-06 17:06:20

+0

這不是作業,我只是要求一些建議。本質上,我需要選擇一個組中的行,其中一個字段可以有一個值或NULL,當有多個行,並且一個在字段中有NULL時,選擇具有最小NON-NULL值的行。如果字段中只有一行帶有NULL,則選擇該行。 – 2012-04-06 17:09:53

+0

這裏的答案:'從 選擇*( \t選擇*,ROW_NUMBER()OVER(由IssID,CustomFieldID爲了分區由ISNULL(ChgGrpID,2147483647))爲RN \t從#工程 \t )p 其中RN = 1' – 2012-04-06 18:47:16

回答

0

第一招是空值是由min的速度抽取的最後的值 - 他們比任何數量分鐘()大,小於max()的任何數字。這意味着你想要的行爲是默認的。

第二個技巧是你不能加入空值,所以你需要使用isnull()。 (如果可以有一個-1的ID,你必須選擇一個不同的重置值。)

所以我們找到了我們的最小值和鍵中的其他項,然後用它作爲子查詢來選擇我們想要的行:

Select p.* from #Projects p 
join 
(Select isnull(MIN(ChgGrpID),-1) as ChgGrpID, IssID, FieldID 
from #Projects 
group by IssID, FieldID) X 
on isnull(P.ChgGrpID,-1)=X.ChgGrpID and P.IssID=X.IssID and P.FieldID=X.FieldID 
相關問題