0

UNION 查询中的列标题被命名为第一个查询的选择语句,如何将列标题更改为Entitiesand Errors

The Query is:

SELECT DISTINCT '1001account' AS [1001account]
    ,[1001account] AS '1001account'
FROM XXX
WHERE StresstestaccountEnabled LIKE '%Yes%'
    AND BalancesheetAmount <> 0
    AND PATINDEX('%[^0-9]%', [1001account]) > 0
    OR [1001account] IS NULL

UNION

SELECT DISTINCT '[MAX(InterestRate)]' AS InterestType
    ,MAX(InterestRate) AS 'MAXInterestRate'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

UNION

SELECT DISTINCT '[MIN(InterestRate)]' AS InterestType
    ,MIN(InterestRate) AS 'MINInterestRate'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

UNION

SELECT DISTINCT '[MAX(SwapRate)]' AS [SwapRate]
    ,MAX(SwapRate) AS 'MAXSwapRate'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

UNION

SELECT DISTINCT '[MIN(SwapRate)]' AS [SwapRate]
    ,MIN(SwapRate) AS 'MINSwapRate'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

UNION

SELECT DISTINCT '[MAX([Margin])]' AS [Margin]
    ,MAX([Margin]) AS 'MAXS[Margin]'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

UNION

SELECT DISTINCT '[MIN([Margin])]' AS [Margin]
    ,MIN([Margin]) AS 'MIN[Margin]'
FROM XXX
WHERE BalanceSheetAmount <> 0
    AND StressTestAccountEnabled LIKE 'Yes'

             

输出是:

**1001account**         **1001account**
[MAX([Margin])]         170.8372200000
[MAX(InterestRate)]         172.7691400000
[MAX(SwapRate)]         70.8750000000
[MIN([Margin])]         -70.6084500000
[MIN(InterestRate)]         -19.4163150000
[MIN(SwapRate)]         -1.0392500000
1001account                 NULL
5028account                 NULL

如您所见,列标题指的是第一个查询的选择语句。如何在使用UNION语句时更改这些标题的名称?

4

1 回答 1

0

在第一个选择中更改别名

SELECT DISTINCT '1001account' AS Entities
    ,[1001account] AS Errors
FROM XXX
WHERE StresstestaccountEnabled LIKE '%Yes%'
    AND BalancesheetAmount <> 0
    AND PATINDEX('%[^0-9]%', [1001account]) > 0
    OR [1001account] IS NULL
于 2019-04-23T13:27:40.053 回答