英文:
Perform SUM() Calc with Where Statement then Insert SUM() Into another SELECT Statement with different FILTERS
问题
我试图将12个列的数据进行SUM,并使用特定的WHERE语句过滤数据,以确保数字正确,然后我必须尝试将SUM的结果注入到我的报表的主表中。
以下是我正在处理和测试的一些代码(它仍然很粗糙,我正在一步一步地构建它),但我尝试使用CREATE VIEW来获得我的SUM结果,然后尝试将其放入报表的主SELECT语句中,但它根本没有正常工作,所以我放弃了那个并注释掉了Create View块。但希望这给出了我脑海中整体计划的一个好主意。
我可能还需要以相同的方式计算其他列,所以我正试图找出以简单而有效的方式将所有这些链接在一起的最佳方法。
最终目标是在SSRS中生成401k信息的报告。
英文:
I'm trying to SUM 12 columns together with a specific WHERE Statement to filter the data so the numbers are correct, and then I have to try to inject the results of that SUM into my main table for the report.
Below is some code I am working with and testing (its still very rough and I am building it one step at a time), but I attempted to use CREATE VIEW in order to get my SUM Results and I attempted to get that into my main SELECT Statement for the report but it wasn't working correctly at all so I abandoned that and just commented out the Create View block. But hopefully this gives a good idea of my overall plan is in my head.
There are other columns I will likely have to calculate the same way, so I'm trying to figure out the best way I can chain this all together in a simple but effective way.
End goal is a report in SSRS to compile 401k info.
/*
GO
CREATE VIEW [TOTAL CONTRIBUTIONS] AS
SELECT SUM(MTDWAGES_1 + MTDWAGES_2 + MTDWAGES_3 + MTDWAGES_4 + MTDWAGES_5 + MTDWAGES_6 + MTDWAGES_7 + MTDWAGES_8 + MTDWAGES_9 + MTDWAGES_10 + MTDWAGES_11 + MTDWAGES_12) AS 'TOT CONTRI'
FROM [DB1].[dbo].[UPR30301]
WHERE PYRLRTYP LIKE '3' AND PAYROLCD LIKE '401K';
GO*/
SELECT A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM,BRTHDATE,STRTDATE,DEMPINAC AS 'Term Date', B.YEAR1,
FROM [DB1].[dbo].[UPR00100] A
INNER JOIN [DB1].[dbo].[UPR30301] B ON A.EMPLOYID = B.EMPLOYID
INNER JOIN [DB1].[dbo].[UPR30300] c ON A.EMPLOYID = C.EMPLOYID
--WHERE
GROUP BY A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM, BRTHDATE, STRTDATE, DEMPINAC, B.YEAR1, B.PYRLRTYP, B.PAYROLCD
答案1
得分: 1
你的 MTDWages 数据是非标准化的,但我猜你已经知道这一点,而且没有改变的可能性。然而,你可以使用 CROSS APPLY(SELECT VALUES ...)
来使它看起来标准化,之后可以使用内置的聚合函数。
类似以下的方式:
SELECT A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM, BRTHDATE,
STRTDATE, DEMPINAC AS 'Term Date', B.YEAR1,
SUM(W.MTDWAGES) AS 'TOT CONTRI'
FROM [DB1].[dbo].[UPR00100] A
INNER JOIN [DB1].[dbo].[UPR30301] B ON A.EMPLOYID = B.EMPLOYID
INNER JOIN [DB1].[dbo].[UPR30300] c ON A.EMPLOYID = C.EMPLOYID
CROSS APPLY (
VALUES
(B.MTDWAGES_1), (B.MTDWAGES_2), (B.MTDWAGES_3), (B.MTDWAGES_4),
(B.MTDWAGES_5), (B.MTDWAGES_6), (B.MTDWAGES_7), (B.MTDWAGES_8),
(B.MTDWAGES_9), (B.MTDWAGES_10), (B.MTDWAGES_11), (B.MTDWAGES_12)
) W(MTDWAGES)
--WHERE
GROUP BY A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM, BRTHDATE, STRTDATE, DEMPINAC, B.YEAR1, B.PYRLRTYP, B.PAYROLCD
如果需要的话,你还可以添加一个键:
CROSS APPLY (
VALUES
(1, B.MTDWAGES_1), (2, B.MTDWAGES_2), (3, B.MTDWAGES_3), (4, B.MTDWAGES_4),
(5, B.MTDWAGES_5), (6, B.MTDWAGES_6), (7, B.MTDWAGES_7), (8, B.MTDWAGES_8),
(9, B.MTDWAGES_9), (10, B.MTDWAGES_10), (11, B.MTDWAGES_11), (12, B.MTDWAGES_12)
) W(Month, MTDWAGES)
英文:
Your MTDWages data is unnormlized, but I'm guessing that you know that and are not in a position to change that. However, you can use a CROSS APPLY(SELECT VALUES ...)
to make it look normalized after which you can use built-in aggregation functions.
Something like:
SELECT A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM, BRTHDATE,
STRTDATE, DEMPINAC AS 'Term Date', B.YEAR1,
SUM(W.MTDWAGES) AS [TOT CONTRI]
FROM [DB1].[dbo].[UPR00100] A
INNER JOIN [DB1].[dbo].[UPR30301] B ON A.EMPLOYID = B.EMPLOYID
INNER JOIN [DB1].[dbo].[UPR30300] c ON A.EMPLOYID = C.EMPLOYID
CROSS APPLY (
VALUES
(B.MTDWAGES_1), (B.MTDWAGES_2), (B.MTDWAGES_3), (B.MTDWAGES_4),
(B.MTDWAGES_5), (B.MTDWAGES_6), (B.MTDWAGES_7), (B.MTDWAGES_8),
(B.MTDWAGES_9), (B.MTDWAGES_10), (B.MTDWAGES_11), (B.MTDWAGES_12)
) W(MTDWAGES)
--WHERE
GROUP BY A.EMPLOYID, LASTNAME, FRSTNAME, SOCSCNUM, BRTHDATE, STRTDATE, DEMPINAC, B.YEAR1, B.PYRLRTYP, B.PAYROLCD
If needed, you can also add a key:
CROSS APPLY (
VALUES
(1, B.MTDWAGES_1), (2, B.MTDWAGES_2), (3, B.MTDWAGES_3), (4, B.MTDWAGES_4),
(5, B.MTDWAGES_5), (6, B.MTDWAGES_6), (7, B.MTDWAGES_7), (8, B.MTDWAGES_8),
(9, B.MTDWAGES_9), (10, B.MTDWAGES_10), (11, B.MTDWAGES_11), (12, B.MTDWAGES_12)
) W(Month, MTDWAGES)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论