gpt4 book ai didi

sql - 修改交叉连接查询

转载 作者:行者123 更新时间:2023-12-03 02:29:11 25 4
gpt4 key购买 nike

我发现我想要修改这个查询,以获得额外的列并对最近 3 个月找到的金额求和。我想为此制作一份 Crystal 报告。查询如下。

SELECT 
dbo.[@EIM_PROCESS_DATA].U_Tax_year,
dbo.[@EIM_PROCESS_DATA].U_Employee_ID,
SUM(dbo.[@EIM_PROCESS_DATA].U_Amount) AS PAYE,
dbo.OADM.CompnyName,
dbo.OADM.CompnyAddr,
dbo.OADM.TaxIdNum,
dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + ' ' +
ISNULL(dbo.OHEM.firstName, '') AS EmployeeName, dbo.OHEM.govID

FROM dbo.[@EIM_PROCESS_DATA]
INNER JOIN dbo.OHEM ON dbo.[@EIM_PROCESS_DATA].U_Employee_ID
= dbo.OHEM.empID CROSS JOIN dbo.OADM
WHERE (dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS033')

GROUP BY
dbo.[@EIM_PROCESS_DATA].U_Tax_year,
dbo.[@EIM_PROCESS_DATA].U_Employee_ID,
dbo.OADM.CompnyName,
dbo.OADM.CompnyAddr,
dbo.OADM.TaxIdNum,
dbo.OHEM.lastName,
dbo.OHEM.firstName,
dbo.OHEM.middleName,
dbo.OHEM.govID

OHEM 包含一个名为U_Process_month 的字母数字字段,其中包含从一月到十二月的字符。正如上面的查询,SUM(dbo.[@EIM_PROCESS_DATA].U_Amount) 给出了所有 PAYE 金额的总计,ie。 U_PD_code = 'SYS033'

我想要一个查询,根据所选的年份和月份将过去 3 个月 (PAYE) 相加。

我还想检索额外的列,SUM(dbo.[@EIM_PROCESS_DATA].U_Amount) as TAXABLEPAY where (dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS034')

我该如何实现这一目标?任何帮助表示赞赏。

最佳答案

我不确定 U_Tax_year 是什么数据类型,所以我将其保留为 INT。但是,此查询应返回您设置的月份之前的 3 个月。

DECLARE @start_month DATETIME;
DECLARE @start_year INT;

SET @start_month = '2013-04-01';
SET @start_year = 2013;

SELECT dbo.[@EIM_PROCESS_DATA].U_Tax_year
, dbo.[@EIM_PROCESS_DATA].U_Employee_ID
, SUM(CASE WHEN dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS033' THEN dbo.[@EIM_PROCESS_DATA].U_Amount ELSE 0 END) AS PAYE
, SUM(CASE WHEN dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS034' THEN dbo.[@EIM_PROCESS_DATA].U_Amount ELSE 0 END) AS TAXABLEPAY
, dbo.OADM.CompnyName
, dbo.OADM.CompnyAddr
, dbo.OADM.TaxIdNum
, dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + ' ' + ISNULL(dbo.OHEM.firstName, '') AS EmployeeName
, dbo.OHEM.govID
FROM dbo.[@EIM_PROCESS_DATA]INNER JOIN dbo.OHEM ON dbo.[@EIM_PROCESS_DATA].U_Employee_ID = dbo.OHEM.empID CROSS JOIN dbo.OADM
WHERE dbo.[@EIM_PROCESS_DATA].U_PD_code IN ('SYS033', 'SYS034')
AND dbo.OHEM.U_Process_month IN (DATENAME(MONTH, DATEADD(MONTH,-3, @start_month)), DATENAME(MONTH, DATEADD(MONTH,-2, @start_month)), DATENAME(MONTH, DATEADD(MONTH,-1, @start_month)))
AND dbo.[@EIM_PROCESS_DATA].U_Tax_year = @start_year
GROUP BY dbo.[@EIM_PROCESS_DATA].U_Tax_year
, dbo.[@EIM_PROCESS_DATA].U_Employee_ID
, dbo.OADM.CompnyName
, dbo.OADM.CompnyAddr
, dbo.OADM.TaxIdNum
, dbo.OHEM.lastName
, dbo.OHEM.firstName
, dbo.OHEM.middleName
, dbo.OHEM.govID;

关于sql - 修改交叉连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com