Select 嵌套 JSON 数组包含特定值的行
Select row where nested JSON array contains certain value
我在 Employee
table:
中有一些这样的数据库行
EmpId Name Information
1 Eric {“Experience”: [{“Title”: “Assistant Accountant”, “Company”: “ComA”, “YearsOfExperience”: 3}, {“Title”: “Accountant”, “Company”: “ComB”, “YearsOfExperience”: 2}], “EmployedYear”:2016}
2 John {“Experience”: [{“Title”: “Tech Engineer”, “Company”: “ComX”, “Years”: 5}, {“Title”: “Senior Tech Engineer”, “Company”: “ComY”, “YearsOfExperience”: 2}], “EmployedYear”:2012}
3 Leonard {“Experience”: [{“Title”: “Junior Engineer”, “Company”: “ComJ”, “Years”: 2}, {“Title”: “Tech Engineer”, “Company”: “ComB”, “YearsOfExperience”: “7”}], “EmployedYear”:2017}
如何 select 没有在 ComB 工作过的员工?
这个要查询什么?到目前为止,由于这个复杂的嵌套 JSON 数组,我一无所获。
我正在尝试:
SELECT Name, Id
FROM Employee
OUTER APPLY OPENJSON(Information, '$.Experience') WITH (
Title nvarchar(max) '$.Title',
Company nvarchar(max) '$.Company',
YearsOfExperience int '$.YearsOfExperience'
) AS [Info]
WHERE [Info].Company != 'ComB'
您可以尝试使用 EXISTS()
和 OPENJSON()
。
Table:
CREATE TABLE Employee (
EmpId int,
Name varchar(100),
Information varchar(1000)
)
INSERT INTO Employee (EmpId, Name, Information)
VALUES
(1, 'Eric', '{"Experience":[{"Title":"Assistant Accountant","Company":"ComA","YearsOfExperience":3},{"Title":"Accountant","Company":"ComB","YearsOfExperience":2}],"EmployedYear":2016}'),
(2, 'John', '{"Experience":[{"Title":"Tech Engineer","Company":"ComX","Years":5},{"Title":"Senior Tech Engineer","Company":"ComY","YearsOfExperience":2}],"EmployedYear":2012}'),
(3, 'Leonard', '{"Experience":[{"Title":"Junior Engineer","Company":"ComJ","Years":2},{"Title":"Tech Engineer","Company":"ComB","YearsOfExperience":"7"}],"EmployedYear":2017}')
声明:
SELECT Name, EmpId
FROM Employee
WHERE NOT EXISTS (
SELECT 1
FROM OPENJSON(Information, '$.Experience') WITH (
Title nvarchar(max) '$.Title',
Company nvarchar(max) '$.Company',
YearsOfExperience int '$.YearsOfExperience'
) AS [Info]
WHERE [Info].Company = 'ComB'
)
一种方法是在 HAVING
子句中使用一些条件聚合`:
SELECT V.EmpId,
V.Name
FROM (VALUES(1,'Eric',N'{"Experience": [{"Title": "Assistant Accountant", "Company": "ComA", "YearsOfExperience": 3}, {"Title": "Accountant", "Company": "ComB", "YearsOfExperience": 2}], "EmployedYear":2016}'),
(2,'John',N'{"Experience": [{"Title": "Tech Engineer", "Company": "ComX", "Years": 5}, {"Title": "Senior Tech Engineer", "Company": "ComY", "YearsOfExperience": 2}], "EmployedYear":2012}'),
(3,'Leonard',N'{"Experience": [{"Title": "Junior Engineer", "Company": "ComJ", "Years": 2}, {"Title": "Tech Engineer", "Company": "ComB", "YearsOfExperience": "7"}], "EmployedYear":2017}'))V(EmpId,[Name],Information)
CROSS APPLY OPENJSON(Information,'$.Experience')
WITH (Title nvarchar(50),
Company nvarchar(50),
Years int) OJ
GROUP BY V.EmpId, V.Name
HAVING COUNT(CASE OJ.Company WHEN 'ComB' THEN 1 END) = 0;
您可以使用 openjson()
和横向连接。您不需要从嵌套对象中提取所有属性,因为您只对公司名称感兴趣:
select e.empId, e.Name
from employee e
where exists (
select 1
from openjson(information, '$.Experience') with(company nvarchar(max) '$.Company')
where company = 'ComB'
)
empId | Name
----: | :------
1 | Eric
3 | Leonard
我在 Employee
table:
EmpId Name Information
1 Eric {“Experience”: [{“Title”: “Assistant Accountant”, “Company”: “ComA”, “YearsOfExperience”: 3}, {“Title”: “Accountant”, “Company”: “ComB”, “YearsOfExperience”: 2}], “EmployedYear”:2016}
2 John {“Experience”: [{“Title”: “Tech Engineer”, “Company”: “ComX”, “Years”: 5}, {“Title”: “Senior Tech Engineer”, “Company”: “ComY”, “YearsOfExperience”: 2}], “EmployedYear”:2012}
3 Leonard {“Experience”: [{“Title”: “Junior Engineer”, “Company”: “ComJ”, “Years”: 2}, {“Title”: “Tech Engineer”, “Company”: “ComB”, “YearsOfExperience”: “7”}], “EmployedYear”:2017}
如何 select 没有在 ComB 工作过的员工?
这个要查询什么?到目前为止,由于这个复杂的嵌套 JSON 数组,我一无所获。
我正在尝试:
SELECT Name, Id
FROM Employee
OUTER APPLY OPENJSON(Information, '$.Experience') WITH (
Title nvarchar(max) '$.Title',
Company nvarchar(max) '$.Company',
YearsOfExperience int '$.YearsOfExperience'
) AS [Info]
WHERE [Info].Company != 'ComB'
您可以尝试使用 EXISTS()
和 OPENJSON()
。
Table:
CREATE TABLE Employee (
EmpId int,
Name varchar(100),
Information varchar(1000)
)
INSERT INTO Employee (EmpId, Name, Information)
VALUES
(1, 'Eric', '{"Experience":[{"Title":"Assistant Accountant","Company":"ComA","YearsOfExperience":3},{"Title":"Accountant","Company":"ComB","YearsOfExperience":2}],"EmployedYear":2016}'),
(2, 'John', '{"Experience":[{"Title":"Tech Engineer","Company":"ComX","Years":5},{"Title":"Senior Tech Engineer","Company":"ComY","YearsOfExperience":2}],"EmployedYear":2012}'),
(3, 'Leonard', '{"Experience":[{"Title":"Junior Engineer","Company":"ComJ","Years":2},{"Title":"Tech Engineer","Company":"ComB","YearsOfExperience":"7"}],"EmployedYear":2017}')
声明:
SELECT Name, EmpId
FROM Employee
WHERE NOT EXISTS (
SELECT 1
FROM OPENJSON(Information, '$.Experience') WITH (
Title nvarchar(max) '$.Title',
Company nvarchar(max) '$.Company',
YearsOfExperience int '$.YearsOfExperience'
) AS [Info]
WHERE [Info].Company = 'ComB'
)
一种方法是在 HAVING
子句中使用一些条件聚合`:
SELECT V.EmpId,
V.Name
FROM (VALUES(1,'Eric',N'{"Experience": [{"Title": "Assistant Accountant", "Company": "ComA", "YearsOfExperience": 3}, {"Title": "Accountant", "Company": "ComB", "YearsOfExperience": 2}], "EmployedYear":2016}'),
(2,'John',N'{"Experience": [{"Title": "Tech Engineer", "Company": "ComX", "Years": 5}, {"Title": "Senior Tech Engineer", "Company": "ComY", "YearsOfExperience": 2}], "EmployedYear":2012}'),
(3,'Leonard',N'{"Experience": [{"Title": "Junior Engineer", "Company": "ComJ", "Years": 2}, {"Title": "Tech Engineer", "Company": "ComB", "YearsOfExperience": "7"}], "EmployedYear":2017}'))V(EmpId,[Name],Information)
CROSS APPLY OPENJSON(Information,'$.Experience')
WITH (Title nvarchar(50),
Company nvarchar(50),
Years int) OJ
GROUP BY V.EmpId, V.Name
HAVING COUNT(CASE OJ.Company WHEN 'ComB' THEN 1 END) = 0;
您可以使用 openjson()
和横向连接。您不需要从嵌套对象中提取所有属性,因为您只对公司名称感兴趣:
select e.empId, e.Name
from employee e
where exists (
select 1
from openjson(information, '$.Experience') with(company nvarchar(max) '$.Company')
where company = 'ComB'
)
empId | Name ----: | :------ 1 | Eric 3 | Leonard