I gotta believe that there is an simpler way of doing this but...declare @offices table (
Office varchar(10),
OfficeType varchar(10),
Employee varchar(50),
EmployeeType varchar(20)
)
insert into @offices
values
('East', 'Division', null, null),
('East', 'Division', 'Bill Smith', 'Full Time'),
('East', 'Division', 'Sally Smith', 'Part Time'),
('East', 'Division', 'Bill Smith', 'Maternity Leave'),
('West', 'Field', null, null),
('West', 'Field', 'John Smith', 'Part Time'),
('South', 'Division', null, null),
('South', 'Division', 'Sue Smith', 'Part Time'),
('South', 'Division', 'John Smith', 'Part Time'),
('South', 'Division', 'Jimmy Smith', 'Part Time'),
('North', 'Division', null, null)
;with Divisions
as (
select
Office,
OfficeType,
EmployeeType,
count(EmployeeType) over (partition by Office) CountTypes
from
@offices
where
OfficeType = 'Division'
),
DivSingleType
as (
select
Office,
count (distinct EmployeeType) CountTypes
from
divisions
group by
Office
having
count (distinct EmployeeType) = 1
)
select
Office,
OfficeType,
EmployeeType
from Divisions
where CountTypes = 0
union all
select distinct
a.Office,
a.OfficeType,
a.EmployeeType
from
Divisions a
inner join
DivSingleType b
on a.Office = b.Office
and a.EmployeeType is not null
=================================================
Hear the sledges with the bells - silver bells!
What a world of merriment their melody foretells!