如何在SQL中使用联合声明获取不同的列名

时间:2019-04-19 11:02:42

标签: mysql sql

我正在寻找数据错误;表格中不同的列。我正在使用UNION语句一次运行多个查询,以查找表中的错误。在表中找到的所有错误都将在发现错误的第一个查询的列名称下的一栏中。有没有办法以这种方式拆分列,即通过UNION语句中的单独查询发现的每个错误也都显示为单独的列/实体?

这是我正在使用的代码:

readtable

这是输出示例:

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

UNION

SELECT DISTINCT
[5028account] 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE
StresstestaccountEnabled LIKE '%Yes%' AND BalancesheetAmount <> 0
AND
PATINDEX('%[^0-9]%', [5028account]) > 0 OR [5028account] IS NULL

UNION

SELECT DISTINCT
[BalanceSheetType] 
FROM
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE
StressTestAccountEnabled LIKE '%Yes%' AND BalanceSheetAmount <> 0
AND
[BalanceSheetType] NOT LIKE '%Assets%'
AND
BalanceSheetType NOT LIKE '%Liabilities%'

UNION

SELECT DISTINCT 
CorepRRR 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE '%Yes%' AND BalanceSheetAmount<>0
AND
CorepRRR NOT LIKE '%D%'
AND
CorepRRR NOT LIKE '%R%'
AND
CorepRRR NOT LIKE '%P%'
AND
CorepRRR NOT LIKE '%Not Applicable%'

UNION

SELECT 
[CoreGroupId] 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE '%Yes%' AND BalanceSheetAmount<>0
AND
[CoreGroupId] NOT IN (2, 3, 4, 5, 6, 7, 8, 9, 11 ,14, 15, 99)

UNION

SELECT 
CoreGroupDescription 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE '%Yes%' AND BalanceSheetAmount<>0
AND
[CoreGroupDescription] NOT IN 
( 'DLL', 'ABB_Assets', 'ABB_Liab', 'LRDW', 'OBV', 'VRN', 'Force', 'ROB', 'HedgeAccounting', 'LRDW_US', 'CorepDefaultsCorrections', 'Corrections'  )

UNION

SELECT DISTINCT 
[Country] 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE 'Yes'
AND
BalanceSheetAmount <> 0 
AND
Country NOT LIKE '%[^a-z]%'
AND
(
LEN(Country)>2 OR LEN(Country)<2 
)

UNION

SELECT DISTINCT 
CountryOfRisk 
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE 'Yes'
AND
BalanceSheetAmount != 0
AND
Country NOT LIKE '%[^a-z]%'
AND
(
LEN(Country)>2 OR LEN(Country)<2
)

UNION


SELECT DISTINCT 
InterestType
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE 'Yes'
AND
BalanceSheetAmount <> 0
AND
InterestType NOT LIKE 'Fixed'
AND
InterestType NOT LIKE 'Floating'

UNION

SELECT DISTINCT
InterestRate
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE
BalanceSheetAmount <> 0
AND
StressTestAccountEnabled LIKE 'Yes'
AND
(
InterestRate > 20
OR
InterestRate < -2
)

UNION

SELECT DISTINCT 
LocalBalance
FROM 
[Cris_Ocean_tmp].[dbo].[NiiStressTest2018_v2_PUBLISHED_201812_CC_0326]
WHERE 
StressTestAccountEnabled LIKE 'Yes'
AND
BalanceSheetAmount <> 0
AND
LocalBalance IS NULL

NULL在我的查询中被归类为错误,可以在[1001account]列中找到,但第2、3和4行中的其他值属于另一列:互换和利率。

我该如何以可视化的方式使用户看到第2、3和4行属于不同的列?

1 个答案:

答案 0 :(得分:2)

只需在SELECT列表中添加第二列,即可告诉您值的来源:

SELECT DISTINCT
[1001account] AS [value], '1001account' AS [column]

其他列也是如此:

SELECT DISTINCT
[5028account] AS [value], '5028account' AS [column]