上图是我使用 t-SQL 表和视图制作的报告输出的屏幕截图。每列有两个值,第一个是当前数字,然后正斜杠后面是每周的差值。这些数字在我的大多数报告中看起来都不错,但正如您所见,“总计”列似乎具有完全错误的差异值。
该表是使用以下代码创建的
SELECT
ISNULL(a.[LRPR Biospecimen Consent], 'Missing') AS Consent,
CONVERT(varchar, a.Oppong) + ' / ' + CONVERT(varchar, a.Oppong - ISNULL(b.Oppong, 0)) AS Oppong,
CONVERT(varchar, a.Tousimis) + ' / ' + CONVERT(varchar, a.Tousimis - ISNULL(b.Tousimis, 0)) AS Tousimis,
CONVERT(varchar, a.Willey) + ' / ' + CONVERT(varchar, a.Willey - ISNULL(b.Willey, 0)) AS Willey,
CONVERT(varchar, a.Anderson) + ' / ' + CONVERT(varchar, a.Anderson - ISNULL(b.Anderson, 0)) AS Anderson,
CONVERT(varchar, a.Bacarra) + ' / ' + CONVERT(varchar, a.Bacarra - ISNULL(b.Bacarra, 0)) AS Bacarra,
CONVERT(varchar, a.McMahon) + ' / ' + CONVERT(varchar, a.McMahon - ISNULL(b.McMahon, 0)) AS McMahon
FROM
dbo.vw_RPT_Ourisman_BiospecimenConsentWithProvider_Pivot a
LEFT OUTER JOIN dbo.tbl_vw_RPT_Ourisman_BiospecimenConsentWithProvider_Pivot b
ON ...
此代码的变体(仅更改路径名已为正斜杠两侧的数字提供了准确的总值。我是 SQL 新手,我似乎无法弄清楚为什么此代码停止工作。
非常感谢任何有用的建议
编辑 下面是用于创建 dbo.vw_RPT_Ourisman_BiospecimenConsentWithProvider_Pivot Total 值的代码。
With T as ( SELECT [LRPR Biospecimen Consent], [Oppong, Bridget A] AS Oppong, [Tousimis, Eleni] AS Tousimis, [Willey, Shawna] AS Willey, [Anderson, Lyndsay] AS Anderson,
[Bacarra, Minna Manalo] AS Bacarra, [McMahon, Ann] AS McMahon
FROM vw_RPT_Ourisman_BiospecimenConsentWithProvider_List c PIVOT (count([Breast Provider]) FOR [Breast Provider] IN ([Oppong, Bridget A], [Tousimis, Eleni],
[Willey, Shawna], [Anderson, Lyndsay], [Bacarra, Minna Manalo], [McMahon, Ann])) piv
WHERE [LRPR Biospecimen Consent] IS NOT NULL)
SELECT *
FROM T
UNION ALL
SELECT 'Total', SUM(Oppong), SUM(Tousimis), SUM(Willey), SUM(Anderson), SUM(Bacarra), SUM(McMahon)
FROM T