防止SQL"FOR JSON"造成重复

Prevent SQL "FOR JSON" from causing duplication

我有一个硕士table科室。详细信息 table 员工具有指向部门 table 的外键。当使用 PATH 选项将简单连接查询 return 编辑为 JSON 时,它会列出部门的多行。而当使用 AUTO 选项时,它是 returning 独特的部门,但我失去了对模式的控制。我怎样才能使用 PATH 选项并且仍然能够 return 独特的部门,就像 AUTO 选项一样。这是代码:

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

自动模式return结果如下。注意每个部门只出现一次:

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

PATH 选项return产生以下结果。注意每个部门的多次出现:

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

使用PATH方式如何防止部门重复出现?

没关系。必须首先通过对员工 table 进行 JSON 化来修改源查询,然后与部门 table 交叉应用,最后再对整个事情进行 JSON 化。

查询:

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"
        }
    }
]