我有这么长的SQL UNION查询。因此,我有两个表具有相同的列名,但可能具有不同的数据。我正在编写此查询来检查两个表上的列是否存在差异并显示差异。我的问题是,差异又回到了错误的栏目之下。所有结果都返回到第一个SELECT语句中各列的标题下。我希望不同的列返回正确的列名,以使区别清楚。这是我的代码和输出结果的图片。我在SELECT语句返回差异的旁边加上了注释。
SELECT DISTINCT --All Differences are coming under these headings
s.EEid,
CAST(s.Gross AS FLOAT) AS 'Star Gross',
CAST(pr.Gross AS FLOAT) AS 'PR Gross',
CAST(s.Gross AS FLOAT) - CAST(pr.Gross AS FLOAT) AS 'Gross Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Gross AS FLOAT) != CAST(pr.Gross AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.NetPay AS FLOAT) AS 'Star NetPay',
CAST(pr.NetPay AS FLOAT) AS 'PR NetPay',
CAST(s.NetPay AS FLOAT) - CAST(pr.NetPay AS FLOAT) AS 'NetPay Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.NetPay AS FLOAT) != CAST(pr.NetPay AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.StudentLoanDeductions AS FLOAT) AS 'Star StudentLoanDeductions',
CAST(pr.StudentLoanDeductions AS FLOAT) AS 'PR StudentLoanDeductions',
CAST(s.StudentLoanDeductions AS FLOAT) - CAST(pr.StudentLoanDeductions AS FLOAT) AS 'StudentLoanDeductions Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.StudentLoanDeductions AS FLOAT) != CAST(pr.StudentLoanDeductions AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Total AS FLOAT) AS 'Star Total',
CAST(pr.Total AS FLOAT) AS 'PR Total',
CAST(s.Total AS FLOAT) - CAST(pr.Total AS FLOAT) AS 'Total Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Total AS FLOAT) != CAST(pr.Total AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Earnings_To_LEL AS FLOAT) AS 'Star EarningsToLEL',
CAST(pr.Earnings_To_LEL AS FLOAT) AS 'PR EarningsToLEL',
CAST(s.Earnings_To_LEL AS FLOAT) - CAST(pr.Earnings_To_LEL AS FLOAT) AS 'EarningsToLEL Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_LEL AS FLOAT) != CAST(pr.Earnings_To_LEL AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Earnings_To_SET AS FLOAT) AS 'Star EarningsToSET',
CAST(pr.Earnings_To_SET AS FLOAT) AS 'PR EarningsToSET',
CAST(s.Earnings_To_SET AS FLOAT) - CAST(pr.Earnings_To_SET AS FLOAT) AS 'EarningsToSET Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_SET AS FLOAT) != CAST(pr.Earnings_To_SET AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Earnings_To_PET AS FLOAT) AS 'Star EarningsToPET',
CAST(pr.Earnings_To_PET AS FLOAT) AS 'PR EarningsToPET',
CAST(s.Earnings_To_PET AS FLOAT) - CAST(pr.Earnings_To_PET AS FLOAT) AS 'EarningsToPET Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_PET AS FLOAT) != CAST(pr.Earnings_To_PET AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Earnings_To_UST AS FLOAT) AS 'Star EarningsToUST',
CAST(pr.Earnings_To_UST AS FLOAT) AS 'PR EarningsToUST',
CAST(s.Earnings_To_UST AS FLOAT) - CAST(pr.Earnings_To_UST AS FLOAT) AS 'EarningsToUST Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_UST AS FLOAT) != CAST(pr.Earnings_To_UST AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Earnings_To_AUST AS FLOAT) AS 'Star EarningsToAUST',
CAST(pr.Earnings_To_AUST AS FLOAT) AS 'PR EarningsToAUST',
CAST(s.Earnings_To_AUST AS FLOAT) - CAST(pr.Earnings_To_AUST AS FLOAT) AS 'EarningsToAUST Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_AUST AS FLOAT) != CAST(pr.Earnings_To_AUST AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Earnings_To_UEL AS FLOAT) AS 'Star EarningsToUEL',
CAST(pr.Earnings_To_UEL AS FLOAT) AS 'PR EarningsToUEL',
CAST(s.Earnings_To_UEL AS FLOAT) - CAST(pr.Earnings_To_UEL AS FLOAT) AS 'EarningsToUEL Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_To_UEL AS FLOAT) != CAST(pr.Earnings_To_UEL AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Earnings_Above_UEL AS FLOAT) AS 'Star EarningsAboveUEL',
CAST(pr.Earnings_Above_UEL AS FLOAT) AS 'PR EarningsAboveUEL',
CAST(s.Earnings_Above_UEL AS FLOAT) - CAST(pr.Earnings_Above_UEL AS FLOAT) AS 'EarningsAboveUEL Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Earnings_Above_UEL AS FLOAT) != CAST(pr.Earnings_Above_UEL AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Ee_Contributions_Pt1 AS FLOAT) AS 'Star EeContributionsPT1',
CAST(pr.Ee_Contributions_Pt1 AS FLOAT) AS 'PR EeContributionsPT1',
CAST(s.Ee_Contributions_Pt1 AS FLOAT) - CAST(pr.Ee_Contributions_Pt1 AS FLOAT) AS 'EeContributionsPT1 Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Ee_Contributions_Pt1 AS FLOAT) != CAST(pr.Ee_Contributions_Pt1 AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Ee_Contributions_Pt2 AS FLOAT) AS 'Star EeContributionsPT2',
CAST(pr.Ee_Contributions_Pt2 AS FLOAT) AS 'PR EeContributionsPT2',
CAST(s.Ee_Contributions_Pt2 AS FLOAT) - CAST(pr.Ee_Contributions_Pt2 AS FLOAT) AS 'EeContributionsPT2 Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Ee_Contributions_Pt2 AS FLOAT) != CAST(pr.Ee_Contributions_Pt2 AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT --Different
s.EEid,
CAST(s.Er_Contributions AS FLOAT) AS 'Star ErContributions',
CAST(pr.Er_Contributions AS FLOAT) AS 'PR ErContributions',
CAST(s.Er_Contributions AS FLOAT) - CAST(pr.Er_Contributions AS FLOAT) AS 'ErContributions Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Er_Contributions AS FLOAT) != CAST(pr.Er_Contributions AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Ee_Rebate AS FLOAT) AS 'Star EeRebate',
CAST(pr.Ee_Rebate AS FLOAT) AS 'PR EeRebate',
CAST(s.Ee_Rebate AS FLOAT) - CAST(pr.Ee_Rebate AS FLOAT) AS 'EeRebate Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Ee_Rebate AS FLOAT) != CAST(pr.Ee_Rebate AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Er_Rebate AS FLOAT) AS 'Star ErRebate',
CAST(pr.Er_Rebate AS FLOAT) AS 'PR ErRebate',
CAST(s.Er_Rebate AS FLOAT) - CAST(pr.Er_Rebate AS FLOAT) AS 'ErRebate Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Er_Rebate AS FLOAT) != CAST(pr.Er_Rebate AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.Ee_Reduction AS FLOAT) AS 'Star EeReduction',
CAST(pr.Ee_Reduction AS FLOAT) AS 'PR EeReduction',
CAST(s.Ee_Reduction AS FLOAT) - CAST(pr.Ee_Reduction AS FLOAT) AS 'EeReduction Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.Ee_Reduction AS FLOAT) != CAST(pr.Ee_Reduction AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.TaxPreviousEmt AS FLOAT) AS 'Star TaxPreviousEmt',
CAST(pr.TaxPreviousEmt AS FLOAT) AS 'PR TaxPreviousEmt',
CAST(s.TaxPreviousEmt AS FLOAT) - CAST(pr.TaxPreviousEmt AS FLOAT) AS 'TaxPreviousEmt Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.TaxPreviousEmt AS FLOAT) != CAST(pr.TaxPreviousEmt AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.TaxablePayPreviousEmt AS FLOAT) AS 'Star TaxablePayPreviousEmt',
CAST(pr.TaxablePayPreviousEmt AS FLOAT) AS 'PR TaxablePayPreviousEmt',
CAST(s.TaxablePayPreviousEmt AS FLOAT) - CAST(pr.TaxablePayPreviousEmt AS FLOAT) AS 'TaxablePayPreviousEmt Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.TaxablePayPreviousEmt AS FLOAT) != CAST(pr.TaxablePayPreviousEmt AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.TaxThisEmt AS FLOAT) AS 'Star TaxThisEmt',
CAST(pr.TaxThisEmt AS FLOAT) AS 'PR TaxThisEmt',
CAST(s.TaxThisEmt AS FLOAT) - CAST(pr.TaxThisEmt AS FLOAT) AS 'TaxThisEmt Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.TaxThisEmt AS FLOAT) != CAST(pr.TaxThisEmt AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.TaxablePayThisEmt AS FLOAT) AS 'Star TaxablePayThisEmt',
CAST(pr.TaxablePayThisEmt AS FLOAT) AS 'PR TaxablePayThisEmt',
CAST(s.TaxablePayThisEmt AS FLOAT) - CAST(pr.TaxablePayThisEmt AS FLOAT) AS 'TaxablePayThisEmt Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.TaxablePayThisEmt AS FLOAT) != CAST(pr.TaxablePayThisEmt AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.AccYearBal AS FLOAT) AS 'Star AccYearBal',
CAST(pr.AccYearBal AS FLOAT) AS 'PR AccYearBal',
CAST(s.AccYearBal AS FLOAT) - CAST(pr.AccYearBal AS FLOAT) AS 'AccYearBal Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.AccYearBal AS FLOAT) != CAST(pr.AccYearBal AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.PAYEYearBal AS FLOAT) AS 'Star PAYEYearBal',
CAST(pr.PAYEYearBal AS FLOAT) AS 'PR PAYEYearBal',
CAST(s.PAYEYearBal AS FLOAT) - CAST(pr.PAYEYearBal AS FLOAT) AS 'PAYEYearBal Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.PAYEYearBal AS FLOAT) != CAST(pr.PAYEYearBal AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.ACCYearUnits AS FLOAT) AS 'Star ACCYearUnits',
CAST(pr.ACCYearUnits AS FLOAT) AS 'PR ACCYearUnits',
CAST(s.ACCYearUnits AS FLOAT) - CAST(pr.ACCYearUnits AS FLOAT) AS 'ACCYearUnits Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.ACCYearUnits AS FLOAT) != CAST(pr.ACCYearUnits AS FLOAT) AND s.PayCode = pr.PayCode
UNION
SELECT DISTINCT
s.EEid,
CAST(s.PAYEYearUnits AS FLOAT) AS 'Star PAYEYearUnits',
CAST(pr.PAYEYearUnits AS FLOAT) AS 'PR PAYEYearUnits',
CAST(s.PAYEYearUnits AS FLOAT) - CAST(pr.PAYEYearUnits AS FLOAT) AS 'PAYEYearUnits Difference'
FROM starPayHistory s
INNER JOIN payRunPayHistory pr ON pr.EEid = s.EEid
WHERE CAST(s.PAYEYearUnits AS FLOAT) != CAST(pr.PAYEYearUnits AS FLOAT) AND s.PayCode = pr.PayCode
这是结果的图片。如您所见,结果全都位于错误的标题名称下。Any recommendations are greatly appreciated
答案 0 :(得分:0)
我不明白你的意思,但这是基于Oracle RDBMS的一些建议:
答案 1 :(得分:0)
一个关系(表)由一个标题和一组元组组成。在示例查询中,您将所有CAST列都放在同一标题下。如果您希望它们在不同的列中,则可以添加虚拟列,并在最外层使用聚合来删除虚拟变量:
SELECT DISTINCT
EEid,
CAST(s.Gross AS FLOAT)作为“ Star Gross”,
CAST(现价浮动净值)作为“ PR毛重”,
CAST(s.Gross AS FLOAT)-CAST(pr.Gross AS FLOAT)AS'Gross Difference',
CAST(空值表示为FLOAT)作为“ Star NetPay”,
CAST(不包含FLOAT)作为“ PR NetPay”,
CAST(空值表示为FLOAT)作为“ NetPay差异”
来自starPayHistory的
加入payRunPayHistory公关
开启pr.EEid = s.EEid
放送(s.Gross AS FLOAT)!=放送(s.Gross AS FLOAT)-这看起来很危险
AND s.PayCode = pr.PayCode
联盟
选择地区
EEid
CAST(空值表示为FLOAT)AS ...,
CAST(空值表示为FLOAT)AS ...,
CAST(空值表示为FLOAT)AS ...,
CAST(s.NetPay AS FLOAT)作为“ Star NetPay”,
CAST(pr.NetPay AS FLOAT)作为“ PR NetPay”,
CAST(s.NetPay AS FLOAT)-CAST(pr.NetPay AS FLOAT)AS'NetPay差异'
来自starPayHistory的
加入payRunPayHistory公关
开启pr.EEid = s.EEid
CAST(s.NetPay AS FLOAT)!= CAST(pr.NetPay AS FLOAT)
AND s.PayCode = pr.PayCode
一些评论:
但是,看起来好像您正在使用相同的表进行连接,因此我认为您可以像这样简化查询:
SELECT DISTINCT
EEid,
CAST(s.Gross AS FLOAT)作为“ Star Gross”,
CAST(现价浮动净值)作为“ PR毛重”,
CAST(s.Gross AS FLOAT)-CAST(pr.Gross AS FLOAT)AS'Gross Difference',
CAST(s.NetPay AS FLOAT)作为“ Star NetPay”,
CAST(pr.NetPay AS FLOAT)作为“ PR NetPay”,
CAST(s.NetPay AS FLOAT)-CAST(pr.NetPay AS FLOAT)AS'NetPay差异'
来自starPayHistory的
加入payRunPayHistory公关
开启pr.EEid = s.EEid
在哪里(s.Gross <> pr.Gross或s.NetPay <> pr.NetPay)
AND s.PayCode = pr.PayCode
我不清楚在值相等的情况下您想做什么,因此我将继续进行介绍。当然,您必须扩展查询以包括其他列。