The Column headers in a UNION query are named as the select statement of the first query, how can I change the column headers to Entities
and 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'
OUTPUT IS:
**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
As you can see the column headers refer to the select statement of the first query. How can I change the name of these headers while using a UNION
statement?
Change the alias in the first 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