Вот возможное решение.В этом случае ROW_NUMBER, RANK и DENSE_RANK будут работать.Однако ROW_COUNT не является допустимой оконной функцией на сервере sql.
DECLARE @A TABLE(ID INT, Value INT)
DECLARE @B TABLE(ID INT,Value INT)
INSERT INTO @A VALUES (1,1),(2,1),(3,2),(4,3),(5,2),(6,1),(7,3)
INSERT INTO @B VALUES (1,1),(2,1),(3,1),(4,2),(5,3),(6,2),(7,1),(8,3)
;WITH D AS
(
SELECT ID,Value FROM @A WHERE Value IN(1,2)
UNION ALL
SELECT ID,Value FROM @B WHERE Value IN (3)
)
SELECT * FROM
(
SELECT
ID, Value,
ValueRankInSet = DENSE_RANK() OVER(PARTITION BY VALUE ORDER BY ID) -- <-- If you do not have an ID field you can subst ID with NEWID() as order is not important
FROM D
)AS X
WHERE ValueRankInSet = 1