I am trying to write a query to get this result:
[{
"server_name": "windows",
"app_names": ["firebase", "news api", "video api"]
}, {
"server_name": "linux",
"app_names": ["game"]
}]
However this is the result I am getting:
[{
"server_name": "windows",
"app_names": ["firebase", "news api", "video api", "game"]
}, {
"server_name": "linux",
"app_names": ["firebase", "news api", "video api", "game"]
}]
Database tables:
A
server_id | server_name
-----------------------
1 windows
2 Linux
B
app_id | app_name
-----------------------
1 firebase
2 news api
3 video api
4 game
C
status_id | status | server_id | app_id
----------------------------------------
1 UP 1 1
2 DOWN 1 2
3 DOWN 1 3
4 DOWN 2 4
Query that I have:
SET @output = (
SELECT DISTINCT A.server_name,
JSON_QUERY(REPLACE(REPLACE(( SELECT
B.app_name
FROM B
INNER JOIN C ON
B.app_id = C.app_id
INNER JOIN A ON
C.server_id = A.server_id
FOR
JSON AUTO
), N'{"app_name":', N''),'"}', '"'
)) AS [app_names]
FROM
A
FOR JSON PATH
)
I am close to the result that I want, but don't know what I am missing out on. All the app_names appear no matter what I do.