TRY THIS: It's really tedious but unique requirement and I think to accomplish this, we have to use function
1-Function returns distinct count of service_id
2-Function to split comma separated value and return in table format
--Function returns distinct count of service_id
CREATE FUNCTION [dbo].[getCount](@service_id varchar(500))
RETURNS INT
AS
BEGIN
DECLARE @count int
SELECT @count = COUNT(DISTINCT(t.service_id))
FROM tmptos t
INNER JOIN [dbo].[SplitValue](@service_id, ',') tt on t.service_id = tt.items
RETURN @count
END;
--Function to split comma separated value and return in table format
--Function copied from
--separate comma separated values and store in table in sql server
CREATE FUNCTION [dbo].[SplitValue](@String varchar(MAX), @Delimiter char(1))
RETURNS @temptable TABLE (items VARCHAR(MAX))
AS
BEGIN
DECLARE @idx int
DECLARE @slice varchar(8000)
SELECT @idx = 1
if len(@String)<1 or @String is null return
WHILE @idx!= 0
BEGIN
set @idx = charindex(@Delimiter,@String)
IF @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
IF(LEN(@slice)>0)
INSERT INTO @temptable(Items) values(@slice)
SET @String = right(@String,len(@String) - @idx)
IF LEN(@String) = 0 break
END
RETURN
END;
--Table with Sample Data
create table tmptos(id int, Service_id int, bawe_id int)
insert into tmptos values
(1, 2, 2),
(2, 3, 3),
(3, 2, 3)
declare @service_id varchar(50) = '2,3'
select *
from tmptos t
inner join [dbo].[SplitValue](@service_id, ',') tt on t.Service_id = tt.items
where [dbo].[getCount](@service_id) = (select count(distinct(items)) from [dbo].[SplitValue](@service_id, ','))
OUTPUT:
id Service_id bawe_id items
1 2 2 2
2 3 3 3
3 2 3 2
It's bit lengthy but works perfectly.