如何更改 UNION 语句中列标题的名称
How to change the name of the Columnheaders in a UNION statement
UNION查询中的列headers被命名为第一个查询的select语句,如何将列headers更改为Entities
和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
如您所见,headers 列引用了第一个查询的 select 语句。如何在使用 UNION
语句时更改这些 headers 的名称?
更改第一个别名select
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
UNION查询中的列headers被命名为第一个查询的select语句,如何将列headers更改为Entities
和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
如您所见,headers 列引用了第一个查询的 select 语句。如何在使用 UNION
语句时更改这些 headers 的名称?
更改第一个别名select
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