2017-07-03 1 views
0

私は部署を持っています。詳細テーブルEmployeesにはDepartmentsテーブルを指す外部キーがあります。単純結合クエリがPATHオプションを使用してJSONとして返されると、Departmentの複数の行がリストされます。一方、AUTOオプションを使用すると、ユニークな部門が返されますが、私はスキーマの制御を失います。どのようにしてPATHオプションを使用しても、AUTOオプションと同じようにユニークな部門を返すことができます。コードは次のとおりです。SQLの "FOR 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], E.EmployeeName [Employee.Name], E.Salary [Employee.Salary] 
from 
    @Departments D 
    left join @Employees E on E.DeptName = D.DeptName 
for JSON Auto 

自動モードは次の結果を返します。各部門が1回だけ表示されていることに注意してください。

[{ 
     "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オプションは次の結果を返します。各部門の複数の発生に注意してください:

[{ 
     "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モードを使用した場合の部門の複数の発生を防ぐことができますか?

答えて

0

気にしないでください。 EmployeesテーブルをJSON化してから元のクエリを変更してから、Departmentsテーブルを適用してから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" 
     } 
    } 
] 
関連する問題