Использование CTE:
Create Table #tbl
(
ID Int,
IndividualID Int,
ValueString VarChar(10),
ValueFloat Float,
ValueDate Date,
StartDate Date,
Prop VarChar(15)
)
Insert Into #tbl Values
(108,2,'Outside',NULL,NULL,'2018-06-17','Out Status'),
(107,2,'Male',NULL,NULL,'2018-05-17','Sex'),
(106,1,'Dead',NULL,NULL,'2018-08-17','Out Status'),
(105,2,'Unk',NULL,NULL,'2018-04-03','Sex'),
(104,2,'Adult',NULL,NULL,'2018-04-03','Status'),
(103,1,'Femal',NULL,NULL,'2018-03-27','Sex'),
(102,1,'Adult',NULL,NULL,'2018-03-27','Status'),
(101,2,'Egg',NULL,NULL,'2018-03-25','Status'),
(100,1,'Egg',NULL,NULL,'2018-03-17','Status')
CTE:
;With cte As
(
Select IndividualID,
Max(Case When Prop = 'Out Status' And ValueString = 'Dead' Then 'Y' Else 'N' End) As Dead,
Max(Case When Prop = 'Status' And ValueString <> 'Adult' Then 'Y' Else 'N' End) As Adult,
Max(Case When Prop = 'Sex' And ValueString = 'Male' Then 'Y' Else 'N' End) As Male,
Min(StartDate) As StartDT,
Max(StartDate) As EndDT,
'Living male individuals' As [Group]
From #tbl
Group By IndividualID
)
Select * From cte Where Male = 'Y' And Adult = 'Y' And Dead = 'N'