Is there any way to build a JSON that looks like this using SQL Server 2019? - json

I have this SQL Query:
SELECT
c.customer_name as name,
c.customer_ssn as social_number,
c.telephone as telephone,
c.email as email,
u.user_name as name
FROM
Customer c
LEFT OUTER JOIN
User u ON c.id_customer = u.id_customer
WHERE
c.id_customer = 2
FOR JSON PATH
SELECT * FROM Customer WHERE ID_CUSTOMER = 2
SELECT * FROM User WHERE ID_Customer = 2
This query returns this JSON :
[
{
"name": "blablabla",
"social": "123123123",
"telephone": "91123123",
"email": "blabla#gmail.com",
"name":"blabla"
}
]
Is there any way to get a JSON that looks like this:
{
"name": "blabla",
"social_number": "123123123",
"emails": [
{
"email": "blabla#gmail.com"
},
{
"email": "blablabla#gmail.com"
}
],
"telephones": [
{
"number": "(604)6622141"
},
{
"number": "(652) 2123223"
}
],
Thank you for the help!
I'm still new to coding and StackOverflow... sorry for any mistakes!

You can use correlated subqueries, with FOR JSON in each one
SELECT
c.customer_name as name,
c.customer_ssn as social_number,
telephones = (
SELECT telephone
FROM telephone t
WHERE t.id_customer = c.id_customer
FOR JSON PATH
),
emails = (
SELECT email
FROM email e
WHERE e.id_customer = c.id_customer
FOR JSON PATH
),
u.user_name as name
FROM
Customer c
LEFT OUTER JOIN
User u ON c.id_customer = u.id_customer
WHERE
c.id_customer = 2
FOR JSON PATH;
You can also do this as an APPLY
SELECT
c.customer_name as name,
c.customer_ssn as social_number,
t.telephones,
e.emails,
u.user_name as name
FROM
Customer c
LEFT OUTER JOIN
User u ON c.id_customer = u.id_customer
OUTER APPLY (
SELECT telephone
FROM telephone t
WHERE t.id_customer = c.id_customer
FOR JSON PATH
) t(telephones)
OUTER APPLY (
SELECT email
FROM email e
WHERE e.id_customer = c.id_customer
FOR JSON PATH
) e(emails)
WHERE
c.id_customer = 2
FOR JSON PATH;

Related

SQL Query is returning NULL for the inner join fields

SELECT GROUP_CONCAT(sindidaskalies.sindidaskalia_name SEPARATOR ', ') AS sindidaskalia_names, classes.name AS class_name, classes.*, users.*, teachers.name as teacher_name
FROM users
INNER JOIN classes ON classes.id = users.class_id
INNER JOIN teachers ON teachers.id = classes.ipefthinos OR classes.voithos = teachers.id
INNER JOIN sindidaskalies ON sindidaskalies.user_id = users.id
WHERE users.id = ?
This is my query code ^
{
"sindidaskalia_names": null,
"class_name": null,
"id": 4475,
"name": "Marios",
"voithos": null,
"ipefthinos": null,
"sex": "Male",
"class_id": 1,
"password": "$2b$12$Hwr3n72Swd4vwX/bMxwGGu9XbaZoShxCmCxLF8vaaDBEB4quQfzvu",
"surname": "Kyriacou",
"teams_mail": "email",
"teacher_name": null
}
Output ^
If I request the data one by one without the inner join then I get them just fine but when I try to use it, then it's just NULL. Below are pictures of the tables I have
You have the problem with your teacher table, maybe instead of using teachers.id you need to use teachers.idikotita_id
SELECT GROUP_CONCAT(sindidaskalies.sindidaskalia_name SEPARATOR ', ') AS sindidaskalia_names,
classes.name AS class_name, classes.*, users.*,
teachers.name as teacher_name
FROM users
INNER JOIN classes ON classes.id = users.class_id
INNER JOIN teachers ON teachers.idikotita_id = classes.ipefthinos OR classes.voithos = teachers.idikotita_id
INNER JOIN sindidaskalies ON sindidaskalies.user_id = users.id
WHERE users.id = 4475;

How can we access nested object using n1ql by applying filter to object name

Sample JSON:
{
"id":"idValue",
"rank":{
"1":{
"city":"Mumbai"
},
"2":{
"city":"Delhi"
}
},
"KEY":"RANK"
}
Expected result: get the document max rank city where "KEY"="RANK"
[
{
"id":"idValue",
"city":"Delhi"
}
]
Query:
SELECT
b.id,
(SELECT p.val
FROM OBJECT_PAIRS(b.rank) p
WHERE `p`.`name` = (SELECT to_string(MAX(TONUMBER(v)))
FROM OBJECT_NAMES(b.rank) v))
FROM
`rating` b
WHERE
b.KEY = 'RANK'
SELECT b.id,
(SELECT RAW MAX([TONUMBER(op.name) , op.val])[1]
FROM OBJECT_PAIRS(b.`rank`) AS op)[0].*
FROM mybucket AS b
WHERE b.`KEY` = "RANK";
OR
SELECT b.id, b.`rank`.[name].city
FROM mybucket AS b
LET name = TO_STR(ARRAY_MAX(ARRAY TONUMBER(n) FOR n:v IN b.`rank` END))
WHERE b.`KEY` = "RANK";

How can i get data in one array using JSON_ARRAYAGG and JSON_OBJECT in SQL query?

This is my query, i'm using JSON_ARRAYAGG and JSON_OBJECT for hierarchy relations, but my concern is that i want data in following formated data. Don't be confuse with joins just help me get the data i want.
SELECT
CU.contact_group_id,
JSON_ARRAYAGG(
JSON_OBJECT('contact_user', users.user)
) AS `contact_users`
FROM
contact_user as CU
INNER JOIN (
SELECT
U.id,
JSON_ARRAYAGG(
JSON_OBJECT('id', U.id, 'user', U.first_name)
) as `user`
FROM
users as U
) as `users` on CU.user_id = users.id
Data i get:
"users": [
{
"contact_user": [
{
"id": 1,
"user": "dash"
},
{
"id": 3,
"user": "dash1"
}
]
}
]
Data i want:
"users": [
{
"id": 1,
"user": "dash"
},
{
"id": 3,
"user": "dash1"
}
]
Test this:
SELECT JSON_OBJECT('users', JSON_ARRAYAGG(JSON_OBJECT('id', users.id, 'user', users.first_name))) AS `users`
FROM users
-- check if the following line is really needed
-- INNER JOIN contact_user on contact_user.user_id = users.id
SELECT
CU.contact_group_id,
users.user AS `contact_users`
FROM
contact_user as CU
INNER JOIN (
SELECT
U.id,
JSON_ARRAYAGG(
JSON_OBJECT('id', U.id, 'user', U.first_name)
) as `user`
FROM
users as U
) as `users` on CU.user_id = users.id

Getting mysql query issue with WHERE key work. Help me to sort it out

SELECT c.name as "Client Name"
, c.email as "Email"
, c.phone as "Phone"
, cnt.name as "Country"
, c.street as "Street"
, c.city_id as "City/Suburb"
, st.name as "State"
, c.zip_code as "Postcode"
, "" as "Total Paid"
, "" as "Total Owed"
, CASE WHEN c.delete_status = 1 THEN "Not Active" ELSE "Active" END as "Status"
FROM customer c
LEFT
JOIN shipping_address s
ON c.id = s.customer_id
LEFT
JOIN states st
ON st.id = c.state_id
LEFT
JOIN countries cnt
ON cnt.id = c.country_id
WHEN c.delete_status = 0
Above is my query. And I am having below error message in phpmyadmin where i am not finding any issue in query.
Change WHEN to WHERE in the query
SELECT c.name as "Client Name", c.email as "Email", c.phone as "Phone", cnt.name as "Country", c.street as "Street", c.city_id as "City/Suburb", st.name as "State", c.zip_code as "Postcode", "" as "Total Paid", "" as "Total Owed", ( CASE WHEN c.delete_status = 1 THEN "Not Active" ELSE "Active" END ) as "Status" FROM customer c LEFT JOIN shipping_address s ON c.id = s.customer_id LEFT JOIN states st ON st.id = c.state_id LEFT JOIN countries cnt ON cnt.id = c.country_id WHERE c.delete_status = 0
You have some issue the error message is related to wrong clause word
USE where (and not when ) for where condition
but you have also a wrong use of quote
don't use double quote for column name alias use backtics when needed
SELECT c.name as `Client Name`
, c.email as Email
, c.phone as Phone
, cnt.name as Country
, c.street as Street
, c.city_id as `City/Suburb`
, st.name as State
, c.zip_code as Postcode
, "" as `Total Paid`, "" as `Total Owed`
, ( CASE WHEN c.delete_status = 1 THEN "Not Active" ELSE "Active" END ) as Status
FROM customer c
LEFT JOIN shipping_address s ON c.id = s.customer_id
LEFT JOIN states st ON st.id = c.state_id
LEFT JOIN countries cnt ON cnt.id = c.country_id
WHERE c.delete_status = 0

Duplicated field when using alias

I wrote a query that extract the competitions available, and my table have a common field called name so I used the alias for recognize this field in the final result:
$query = "SELECT c.*,
c.name AS competition_name,
s.name AS season_name
FROM competition c
LEFT JOIN competition_seasons s ON c.id = s.competition_id
WHERE country_id = :country_id";
the problem's that the query will return the field name:
{
"id": "1093",
"country_id": "1",
"name": "Premier League",
"category": "1",
"competition_name": "Premier League",
"season_name": "2018"
}
that is a duplication of competition_name, what I did wrong?
You SELECT c.*, which includes c.name:
Your query expands to:
SELECT
c.id, c.country_id, c.name, c.category,
c.name AS competition_name, s.name AS season_name
FROM ...
so you SELECT the name field twice.