防止SQL“FOR JSON”导致重复

时间:2017-07-03 00:20:52

标签: sql-server json path sql-server-2016

我有一个主表部门。详细信息表Employees具有指向Departments表的外键。当使用PATH选项将简单连接查询作为JSON返回时,它将列出Department的多个行。而当使用AUTO选项时,它会返回唯一的部门,但我放松了对架构的控制。如何使用PATH选项,仍然可以像AUTO选项一样返回唯一的部门。这是代码:

npm install -g eslint-config-airbnb

自动模式返回以下结果。请注意每个部门只出现一次:

Oops! Something went wrong! :(

ESLint couldn't find the plugin "eslint-plugin-jsx-a11y". This can happen for a couple different reasons:

1. If ESLint is installed globally, then make sure eslint-plugin-jsx-a11y is also installed globally. A globally-installed ESLint cannot find a locally-installed plugin.

2. If ESLint is installed locally, then it's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

    npm i eslint-plugin-jsx-a11y@latest --save-dev

If you still can't figure out the problem, please stop by https://gitter.im/eslint/eslint to chat with the team.

PATH选项返回以下结果。请注意每个部门的多次出现:

Declare @Departments as table (DeptName varchar(100), Location varchar(100) )
insert @Departments 
select 'IT', 'San Francisco'
union
select 'Sales', 'Miami'
union
select 'Finance', 'NYC'

Declare @Employees as table (DeptName varchar(100) , EmployeeName varchar(100), Salary Decimal(7,2))
insert @Employees 
select 'Finance', 'Ponzi', 1000
union
select 'Finance', 'Madoff', 10000
union
select 'IT' , 'Bill', 20000
union
select 'IT', 'Steve', 100

select D.DeptName [Department.Name], D.Location [Department.Location], E.EmployeeName [Employee.Name], E.Salary [Employee.Salary]
from
    @Departments D
    left join @Employees E on E.DeptName = D.DeptName
for JSON Auto

使用PATH模式时,如何防止多次出现部门?

1 个答案:

答案 0 :(得分:0)

没关系。必须首先通过JSONifying Employees表来修改源查询,然后交叉应用Departments表,然后在最后再次JSONified整个事情。

查询:

Declare @Departments as table (DeptName varchar(100), Location varchar(100) )
insert @Departments 
select 'IT', 'San Francisco'
union
select 'Sales', 'Miami'
union
select 'Finance', 'NYC'

Declare @Employees as table (DeptName varchar(100) , EmployeeName varchar(100), Salary Decimal(7,2))
insert @Employees 
select 'Finance', 'Ponzi', 1000
union
select 'Finance', 'Madoff', 10000
union
select 'IT' , 'Bill', 20000
union
select 'IT', 'Steve', 100

select D.DeptName [Department.Name], D.Location [Department.Location], jsonEmployees.Employees
from
    @Departments D
    cross apply (
        select EmployeeName [Employee.Name], Salary [Employee.Salary]
        from @Employees Employee 
        where Employee.DeptName = D.DeptName 
        For JSON path
    ) JsonEmployees(Employees)

for JSON path

结果:

[{
        "Department": {
            "Name": "Finance",
            "Location": "NYC"
        },
        "Employees": [{
                "Employee": {
                    "Name": "Madoff",
                    "Salary": 10000.00
                }
            }, {
                "Employee": {
                    "Name": "Ponzi",
                    "Salary": 1000.00
                }
            }
        ]
    }, {
        "Department": {
            "Name": "IT",
            "Location": "San Francisco"
        },
        "Employees": [{
                "Employee": {
                    "Name": "Bill",
                    "Salary": 20000.00
                }
            }, {
                "Employee": {
                    "Name": "Steve",
                    "Salary": 100.00
                }
            }
        ]
    }, {
        "Department": {
            "Name": "Sales",
            "Location": "Miami"
        }
    }
]