Hi river1
Strange question! Here's some help:
DECLARE @People TABLE(
FirstName VARCHAR(10),
SomeKey TINYINT PRIMARY KEY,
SomeForeignKey CHAR(1)
)
INSERT @People
SELECT 'Pedro', 1, 'A' UNION ALL
SELECT 'Pedro', 2, 'C' UNION ALL
SELECT 'Pedro', 3, 'A' UNION ALL
SELECT 'Martha',7, 'A' UNION ALL
SELECT 'Martha',8, 'C' UNION ALL
SELECT 'Martha',9, 'C'
SELECT
A.SomeKey,
B.Nb
FROM
@People AS A
CROSS APPLY
(
SELECT
Nb = COUNT(*)
FROM
@People AS B
WHERE
A.FirstName = B.FirstName
AND
B.SomeForeignKey = 'A'
) AS B
WHERE
SomeForeignKey = 'C'
See how I posted the test data as a script, this will help people help you by making it easier to just copy paste your code and propose a solution.
Let me know how it goes!
Maxim