这应该会为您解决。
-- ============================================================================
-- BEGIN: SETUP TEST DATA
-- ============================================================================
CREATE TABLE UserProfileFields (
UserProfileID int
,[Key] varchar(5)
,Value varchar(12)
);
INSERT UserProfileFields (UserProfileID, [Key], Value)
SELECT A.*
FROM (
SELECT * FROM UserProfileFields WHERE 1=2
UNION ALL SELECT 123, 'food', 'Pizza'
UNION ALL SELECT 123, 'food', 'Indian'
UNION ALL SELECT 4453, 'drink', 'Coke'
UNION ALL SELECT 44850, 'drink', 'Orange Juice'
UNION ALL SELECT 88493, 'food', 'Pizza'
UNION ALL SELECT 448382, 'food', 'Chinese'
UNION ALL SELECT 88493, 'drink', 'Coke'
UNION ALL SELECT 88493, 'drink', 'Orange Juice'
) A;
--/*
-- Turn 8 records into 1,048,576
DECLARE @Count int; SELECT @Count = 0;
WHILE @Count < 17
BEGIN
INSERT UserProfileFields
SELECT * FROM UserProfileFields
SELECT @Count = (@Count + 1)
END
--*/
-- SELECT COUNT(*) FROM UserProfileFields WITH (NOLOCK)
-- ============================================================================
-- END: SETUP TEST DATA
-- ============================================================================
-- ============================================================================
-- BEGIN: Solution if Key, Value, and UserProfileID do NOT make up a unique key
-- ============================================================================
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#DistinctValues', 'U') IS NOT NULL DROP TABLE #DistinctValues;
IF OBJECT_ID('tempdb..#Matches', 'U') IS NOT NULL DROP TABLE #Matches;
SELECT [Key], UserProfileID, Value
INTO #DistinctValues
FROM UserProfileFields WITH (NOLOCK)
GROUP BY [Key], UserProfileID, Value;
SELECT A.[Key], A.Value, A.UserProfileID
INTO #Matches
FROM #DistinctValues A
JOIN #DistinctValues B
ON A.[Key] = B.[Key]
AND A.Value = B.Value
AND A.UserProfileID <> B.UserProfileID;
SELECT DENSE_RANK() OVER(ORDER BY A.[Key], A.Value) [MatchID]
,A.UserProfileID
,A.[Key]
,A.Value
FROM #Matches A;
-- ============================================================================
-- END: Solution if Key, Value, and UserProfileID do NOT make up a unique key
-- ============================================================================
-- ============================================================================
-- BEGIN: Solution if Key, Value, and UserProfileID make up a unique key
-- ============================================================================
IF OBJECT_ID('tempdb..#Matches', 'U') IS NOT NULL DROP TABLE #Matches;
SELECT A.[Key], A.Value, A.UserProfileID
INTO #Matches
FROM UserProfileFields A WITH (NOLOCK)
JOIN UserProfileFields B WITH (NOLOCK)
ON A.[Key] = B.[Key]
AND A.Value = B.Value
AND A.UserProfileID <> B.UserProfileID;
SELECT DENSE_RANK() OVER(ORDER BY A.[Key], A.Value) [MatchID]
,A.UserProfileID
,A.[Key]
,A.Value
FROM #Matches A;
-- ============================================================================
-- END: Solution if Key, Value, and UserProfileID make up a unique key
-- ============================================================================