列的T-SQL条件移位

时间:2015-07-09 21:19:32

标签: sql-server tsql

我目前有一个两个SQL查询的联合,可以生成我公司的整个组织结构图。查询如下:

SELECT [Boss].[dbo].[Persons].[PersonId]
  ,[FirstName]
  ,[MiddleName]
  ,[LastName]
  ,[Prefix]
  ,[Suffix]
  ,[Title]
  ,[CreateDate]
  ,[Boss].[dbo].[OrganizationPersons].[OrganizationId]
  ,[Boss].[dbo].[Organizations].[Name] AS [Region]
  ,Org2.Name AS [Division]
  ,Org3.Name AS [National]
FROM [Boss].[dbo].[Persons]
INNER JOIN [Boss].[dbo].[OrganizationPersons]
ON [Boss].[dbo].[Persons].[PersonId]=[Boss].[dbo].[OrganizationPersons].[PersonId]
INNER JOIN [Boss].[dbo].[Organizations]
ON [Boss].[dbo].[OrganizationPersons].[OrganizationId]=[Boss].[dbo].[Organizations].[OrganizationId]
INNER JOIN [Boss].[dbo].[OrganizationVersions]
ON [Boss].[dbo].[Organizations].[OrganizationVersionId]=[Boss].[dbo].[OrganizationVersions].[OrganizationVersionId]
LEFT OUTER JOIN [Boss].[dbo].[Organizations] Org2
ON Org2.OrganizationId=[Boss].[dbo].[Organizations].[ParentOrganizationId]
LEFT OUTER JOIN [Boss].[dbo].[Organizations] Org3
ON Org3.OrganizationId=Org2.ParentOrganizationId
WHERE [Boss].[dbo].[OrganizationVersions].[EndEffectiveDate] is NULL
UNION
SELECT [Boss].[dbo].[Persons].[PersonId]
  ,[FirstName]
  ,[MiddleName]
  ,[LastName]
  ,[Prefix]
  ,[Suffix]
  ,[Title]
  ,[CreateDate]
  ,[Boss].[dbo].[OrganizationManagers].[OrganizationId]
  ,[Boss].[dbo].[Organizations].[Name] AS [Region]
  ,Org2.Name AS [Division]
  ,Org3.Name AS [National]
FROM [Boss].[dbo].[Persons]
INNER JOIN [Boss].[dbo].[OrganizationManagers]
ON [Boss].[dbo].[Persons].[PersonId]=[Boss].[dbo].[OrganizationManagers].[PersonId]
INNER JOIN [Boss].[dbo].[Organizations]
ON [Boss].[dbo].[OrganizationManagers].[OrganizationId]=[Boss].[dbo].[Organizations].[OrganizationId]
INNER JOIN [Boss].[dbo].[OrganizationVersions]
ON [Boss].[dbo].[Organizations].[OrganizationVersionId]=[Boss].[dbo].[OrganizationVersions].[OrganizationVersionId]
LEFT OUTER JOIN [Boss].[dbo].[Organizations] Org2
ON Org2.OrganizationId=[Boss].[dbo].[Organizations].[ParentOrganizationId]
LEFT OUTER JOIN [Boss].[dbo].[Organizations] Org3
ON Org3.OrganizationId=Org2.ParentOrganizationId
WHERE [Boss].[dbo].[OrganizationVersions].[EndEffectiveDate] is NULL

......而且效果很好。除了担任监督职位的人员。这是什么输出(伺服报告罗宾逊,在这个例子中报告给Forrester):

PersonID  FirstName  LastName  ... Region    Division  National
1         Tom        Servo     ... SOL       Deep-13   National
2         Joel       Robinson  ... Deep-13   National  NULL
3         Clayton    Forrester ... National  NULL      NULL

有没有办法强迫Joel的Deep-13和National像Servo一样排队(即,NULL / Deep-13 / National)和Forrester排队,就像Servo& #39; s也是(即NULL / NULL / National)?

2 个答案:

答案 0 :(得分:1)

首先,在[Boss].[dbo].[Organizations] Org1上添加一个别名,因为你将会需要它。

然后将您最近的3个选项更改为:

CASE WHEN Org2.Name IS NULL THEN NULL ELSE Org1.Name END AS Region
CASE WHEN Org2.Name IS NULL THEN NULL
     WHEN Org3.Name IS NULL THEN Org1.Name
     ELSE Org2.Name
END AS Division,
COALESCE (Org3.Name, Org2.Name, Org1.Name) AS National

答案 1 :(得分:0)

case
    when Org2.Name is not null and Org3.Name is not null then Org1.Name
    else null
end as Region,
case
    when Org3.Name is not null then Org2.Name
    when Org2.Name is not null then Organizations.Name
    else null
end as Division,
coalesce(Org3.Name, Org2.Name, Organizations.Name) as National