在SQLServer中组合视图中的多个联合

sc4hvdpw  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(346)

我想创建一个视图,如下所示。

Create view SomeView
AS
SELECT DISTINCT ISNULL(ID,'ID') as Id, 
ISNULL(ID,'unknown') + ':' + 'ID' AS label,
ISNULL(ID,'unknown') AS [value]
FROM [test].[emp].[OrgView]
  UNION
SELECT DISTINCT ISNULL(EMP_ID,'ID') as Id, 
ISNULL(EMP_ID,'unknown') + ':' + 'EMP_ID' AS label,
ISNULL(EMP_ID,'unknown') AS [value]
FROM [test].[emp].[OrgView]
  UNION
SELECT DISTINCT ISNULL(LICENSE_NO,'LICENSE_NO') as Id, 
ISNULL(LICENSE_NO,'unknown') + ':' + 'LICENSE_NO' AS label,
ISNULL(LICENSE_NO,'unknown') AS [value]
FROM [test].[emp].[OrgView]
.
.
.
.
.
10 such selects

所以在这种情况下,是为每个select块调用一个,还是只调用一个?我不希望这使10个不同的电话,为每个选择。如果你知道的话,请告诉我一些在线资源。

mm9b1k5b

mm9b1k5b1#

取消打印使用 APPLY :

SELECT DISTINCT COALESCE(v.ID, v.id_default) as Id, 
       COALESCE(o.FIRST_NAME, 'unknown') + ':' + v.name_suffix AS label,
       COALESCE(o.VALUE, 'unknown') AS [value]
FROM [test].[emp].[OrgView] o CROSS APPLY
     (VALUES (o.ID, 'ID', 'Unknown'),
             (o.EMP_ID, 'ID', 'EMP'),
             (o.LICENSE, 'LICENSE_NO', 'LICENSE_NO'), . . . 
     ) v(ID, id_default, name_suffix);

相关问题