我的查询返回4条记录(行),我想将所有这些记录求和成一条记录(行),但不确定如何实现。
这是我的代码
SELECT
Tax + StudentLoans AS 'Net Income Tax',
GrossNI,
CASE
WHEN ed.EA_Indicator_CY = 0
THEN 0
ELSE
(
CASE
WHEN (
SELECT SUM(Funding)
FROM PayrollRuns
WHERE PAYEMonth = 12 AND PYear = 2018) > 3000
)
THEN 0
ELSE 3000
END
)
END AS 'Employment Allowance',
SMP AS 'SMP Recovered',
NICompOnSMP AS 'SMP Comp',
SPPA + SPPB AS 'SPP Recovered',
SPPACompensation + SPPBCompensation AS 'SPP COMP',
SAP AS 'SAP Recovered',
SAPCompensation,
SHPP_A + SHPP_B AS 'Shpp Recovered',
SHPP_A_Compensation + SHPP_B_Compensation AS 'ShPP Comp',
Apprenticeship_Levy_Due AS 'App Levy'
FROM PayrollRuns pr
CROSS JOIN [Employer Details] ed
WHERE PAYEMonth = 12 AND pr.PYear = 2018 --RunID = '1058'
GROUP BY Tax, StudentLoans, GrossNI, SMP, NICompOnSMP,
ed.EA_Indicator_CY,
SPPACompensation, SPPBCompensation, SAPCompensation, SHPP_A_Compensation,
SHPP_B_Compensation, SPPA, SPPB, SAP, Apprenticeship_Levy_Due, SHPP_A,
SHPP_B, RunID
我已附上查询结果的图片。我希望他们的总和成一排。任何帮助将不胜感激。
答案 0 :(得分:0)
您必须删除组中所有需要汇总的列。尝试以下操作:将获取按RUNID分组并具有不同EA_Indicator_CY的记录。
SELECT SUM(Tax +StudentLoans) AS 'Net Income Tax'
,SUm(GrossNI)
,CASE
WHEN (ed.EA_Indicator_CY) = 0
THEN 0
ELSE (
CASE
WHEN (
SELECT SUM(Funding)
FROM PayrollRuns
WHERE PAYEMonth = 12
AND PYear = 2018
) > 3000
THEN 0
ELSE 3000
END
)
END AS 'Employment Allowance'
,SUM(SMP) AS 'SMP Recovered'
,SUM(NICompOnSMP) AS 'SMP Comp'
,SUM(SPPA + SPPB) AS 'SPP Recovered'
,SUM(SPPACompensation + SPPBCompensation) AS 'SPP COMP'
,SUM(SAP) AS 'SAP Recovered'
,SUM(SAPCompensation)
,SUM(SHPP_A + SHPP_B) AS 'Shpp Recovered'
,SUM(SHPP_A_Compensation) + SUM(SHPP_B_Compensation) AS 'ShPP Comp'
,Apprenticeship_Levy_Due AS 'App Levy'
FROM PayrollRuns pr
CROSS JOIN [Employer Details] ed
WHERE PAYEMonth = 12
AND pr.PYear = 2018 --RunID = '1058'
group by ed.EA_Indicator_CY,RUNID
答案 1 :(得分:0)
select sum([Net Income Tax]) as [Net Income Tax],sum([GrossNI') as [GrossNI]
,[Employment Allowance] as [Employment Allowance]
,sum([SMP Recovered]) as [SMP Recovered],sum([SMP Comp]) as [SMP Comp]
,sum([SPP Recovered]) as [SPP Recovered],sum([SPP COMP]) as [SPP COMP]
,sum([SAP Recovered]) as [SAP Recovered],sum([SAPCompensation]) as [SAPCompensation]
,sum([Shpp Recovered]) as [Shpp Recovered],sum([ShPP Comp]) as [ShPP Comp]
,sum([App Levy]) as [App Levy]
FROM ( SELECT
Tax + StudentLoans AS 'Net Income Tax',
GrossNI,
CASE WHEN ed.EA_Indicator_CY = 0 THEN 0 ELSE (CASE WHEN (SELECT
SUM(Funding) FROM PayrollRuns WHERE PAYEMonth = 12 AND PYear = 2018) >
3000 THEN 0 ELSE 3000 END) END AS 'Employment Allowance',
SMP AS 'SMP Recovered',
NICompOnSMP AS 'SMP Comp',
SPPA + SPPB AS 'SPP Recovered',
SPPACompensation + SPPBCompensation AS 'SPP COMP',
SAP AS 'SAP Recovered',
SAPCompensation,
SHPP_A + SHPP_B AS 'Shpp Recovered',
SHPP_A_Compensation + SHPP_B_Compensation AS 'ShPP Comp',
Apprenticeship_Levy_Due AS 'App Levy'
FROM PayrollRuns pr
CROSS JOIN [Employer Details] ed
WHERE PAYEMonth = 12 AND pr.PYear = 2018 --RunID = '1058'
GROUP BY Tax, StudentLoans, GrossNI, SMP, NICompOnSMP,
ed.EA_Indicator_CY,
SPPACompensation, SPPBCompensation, SAPCompensation, SHPP_A_Compensation,
SHPP_B_Compensation, SPPA, SPPB, SAP, Apprenticeship_Levy_Due, SHPP_A,
SHPP_B, RunID)
GROUP BY [Employment Allowance]
答案 2 :(得分:0)
按如下所述使用 Sum()聚合函数
-如果缺少一列,则可以添加它:put sum(column)[column_alias]
select
sum(mytab.[Net Income Tax]) [sum_Net Income Tax],
sum(mytab.[GrossNI]) [sum_GrossNI],
sum(mytab.[Employment Allowance])[sum_Employment Allowance],
sum(mytab.[SMP Recovered])[sum_SMP Recovered],
sum(mytab.[ShPP Comp]) [sum_ShPP Comp],
sum(mytab.[SPP COMP]) [sum_SPP COMP],
sum(mytab.[App Levy]) [sum_App Levy],
sum(mytab.[SAPCompensation])[sum_SAPCompensation]
from(
SELECT
Tax + StudentLoans AS 'Net Income Tax',
GrossNI,
CASE
WHEN ed.EA_Indicator_CY = 0 THEN 0 ELSE
(
CASE
WHEN (
SELECT SUM(Funding)
FROM PayrollRuns
WHERE PAYEMonth = 12 AND PYear = 2018) > 3000
)
THEN 0
ELSE 3000
END
) END AS 'Employment Allowance',
SMP AS 'SMP Recovered',
NICompOnSMP AS 'SMP Comp',
SPPA + SPPB AS 'SPP Recovered',
SPPACompensation + SPPBCompensation AS 'SPP COMP',
SAP AS 'SAP Recovered',
SAPCompensation,
SHPP_A + SHPP_B AS 'Shpp Recovered',
SHPP_A_Compensation + SHPP_B_Compensation AS 'ShPP Comp',
Apprenticeship_Levy_Due AS 'App Levy'
FROM PayrollRuns pr
CROSS JOIN [Employer Details] ed
WHERE PAYEMonth = 12 AND pr.PYear = 2018 --RunID = '1058'
GROUP BY Tax, StudentLoans, GrossNI, SMP, NICompOnSMP,
ed.EA_Indicator_CY,
SPPACompensation, SPPBCompensation, SAPCompensation, SHPP_A_Compensation,
SHPP_B_Compensation, SPPA, SPPB, SAP, Apprenticeship_Levy_Due, SHPP_A,
SHPP_B, RunID
) mytab