从下表中为每个 IssID FieldID 组,我想选择包含最低 ChgGrpID 值的行,如果组中存在该字段为 NULL 的行,则仍然选择最低的非空值。如果仅存在 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)