需要 json 值而无需重新加入 table in SQL 服务器

Need json value without re-joining the table in SQL Server

我已经编写了如下所示的代码并且得到了预期的输出。但是,需要相同的输出而不在内联视图中使用相同的表(例如 UserPhoneDetail_JSON 的表连接)。提前致谢

代码:

BEGIN
DROP TABLE #USERMASTER;
DROP TABLE #USERPHONE;
CREATE TABLE #USERMASTER (ID INT, NAME VARCHAR(100));
CREATE TABLE #USERPHONE (ID INT, PHONENUMBER NUMERIC,PHONETYPE CHAR(1));
INSERT INTO #USERMASTER VALUES(1,'JOHN');
INSERT INTO #USERMASTER VALUES(2,'VICTOR');
INSERT INTO #USERPHONE VALUES(1,1356487965,'W');
INSERT INTO #USERPHONE VALUES(1,9841007493,'M');
INSERT INTO #USERPHONE VALUES(1,7255952105,'O');
INSERT INTO #USERPHONE VALUES(2,9874563212,'M');
WITH E AS (SELECT A.ID,A.NAME,B.PHONENUMBER,B.PHONETYPE,ROW_NUMBER() OVER(PARTITION BY A.ID ORDER BY A.ID) RN
FROM #USERMASTER A JOIN #USERPHONE B ON  A.ID=B.ID)
SELECT E.ID,
E.NAME,
E.PHONENUMBER,
E.PHONETYPE,
UserPhoneDetail_JSON = (
                   SELECT A.ID,B.PHONETYPE,PHONENUMBER,A.NAME FROM #USERMASTER A JOIN #USERPHONE B ON  A.ID=B.ID
                    FOR JSON PATH )
FROM E WHERE RN=1;
END

输出:

1   JOHN    1356487965  W   [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
2   VICTOR  9874563212  M   [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]

预期输出:

1   JOHN    1356487965  W   [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"}]

2   VICTOR  9874563212  M   [{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]

如果我的理解正确并且您需要为每个 ID 的每个第一行生成 JSON 内容,则可以选择以下语句:

SELECT 
   t.ID, t.NAME, t.PHONENUMBER, t.PHONETYPE,
   UserPhoneDetail_JSON = (
     SELECT t.ID, PHONETYPE, PHONENUMBER, t.NAME 
     FROM #USERPHONE 
     WHERE ID = t.ID
     FOR JSON PATH 
  )
FROM (
   SELECT 
      m.ID, m.NAME, p.PHONENUMBER, p.PHONETYPE,
      ROW_NUMBER() OVER (PARTITION BY m.ID ORDER BY p.ID) RN
   FROM #USERMASTER m 
   JOIN #USERPHONE p ON m.ID = p.ID
) t
WHERE t.RN = 1