提供一個 subquery的方式
不知道你資料量多少 可能有效能問題 供你參考
DECLARE @T1 TABLE (A varchar(10), B varchar(10))
INSERT INTO @T1
SELECT '1', '123' UNION ALL
SELECT '2', '444' UNION ALL
SELECT '1', '123' UNION ALL
SELECT '2', '1235'
SELECT DISTINCT (
SELECT A, B
FROM @T1
WHERE A = t2.A
FOR JSON AUTO
) FROM @T1 t1
INNER JOIN (
SELECT A FROM @T1
GROUP BY A
) t2
ON t1.A = t2.A