如何通过 Unit_ID 求和 account_ID 并提供 SUM 总金额和余额总计
How to SUM account_ID by Unit_ID and provide SUM Total Amount with Balance Total
想知道为什么我的查询没有汇总和总结所有“ID_Number”,与个别“Product_ID”,“Total_Receipts”老化?
使用 SQL Server Management Studio 创建了查询,我正确显示的输出有参数,我已经尝试了以下查询。
Totals:
count(ar.Product_ID) AS 'Total_Receipts'
sum(ar.Receipt_Amount) AS 'Total_Amount',
sum(chg.Balance) AS 'Total Balance'
目前我的 SQL SSMS 查询输出显示如下:
ID_Number Production_ID Total_Receipts Aging Month Year Total_Amount Total Balance
0017 105206 143 90 DAYS PLUS April 2021 7.00 7.00
0017 105206 143 90 DAYS PLUS April 2021 0.00 0.00
0017 105343 132 90 DAYS PLUS April 2021 0.00 0.00
0017 105343 132 90 DAYS PLUS April 2021 1.00 1.00
我真正想要的是有一个提供“Production_ID”的特定输出并按每个“ID_Number”合并,然后是“总计与 AGING”和 SUM 收据费用“Total_Amount" 如下所列:
ID_Number Production_ID Total_Receipts Aging Month Year Total_Amount Total Balance
0017 105206 143 90 DAYS PLUS April 2021 7.00 7.00
0017 105343 132 90 DAYS PLUS April 2021 1.00 1.00
[查询]
DECLARE @STARTDATE DATETIME2
DECLARE @ENDDATE DATETIME2
SET @STARTDATE = '2021-01-01 00:00:00'
SET @ENDDATE = '2022-02-10 00:00:00'
SELECT DISTINCT
te.ID_Number AS 'ID_Number',
chg.product_ID,
count(ar.Product_ID) AS 'Total_Receipts'
,CASE
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) < 30 THEN 'CURRENT'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 30 AND 59 THEN '30 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 60 AND 89 THEN '60 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) >= 90 THEN '90 DAYS PLUS'
END AS 'Aging',
DATENAME(MONTH, chg.Created_Date) AS 'Month',
DATEPART(YEAR, chg.Created_Date) AS 'Year',
sum(ar.Receipt_Amount) AS 'Total_Amount',
sum(chg.Balance) AS 'Total Balance'
FROM dbo.charges AS chg
LEFT JOIN dbo.ar_receipts AS ar
ON arcpts.production_ID = chg.production_ID
INNER JOIN dbo.entities AS te
ON buld.Name = te.Name
INNER JOIN dbo.Employee AS emp
ON ar.Charge_ID = emp.Charge_ID
WHERE
chg.Balance > 0 AND
chg.Created_Date >= @STARTDATE AND
chg.Created_Date <= @ENDDATE
GROUP BY
te.ID_Number,
chg.Created_Date,
chg.product_ID
ORDER BY
te.ID_Number,
Datename(month,chg.Created_Date)
例如,您可以使用 max
聚合列
SELECT
te.ID_Number AS ID_Number,
chg.product_ID,
count(ar.Product_ID) AS Total_Receipts
,max(CASE
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) < 30 THEN 'CURRENT'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 30 AND 59 THEN '30 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 60 AND 89 THEN '60 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) >= 90 THEN '90 DAYS PLUS'
END) AS max_Aging,
max(DATENAME(MONTH, chg.Created_Date)) AS max_Month,
max(DATEPART(YEAR, chg.Created_Date)) AS max_Year,
sum(ar.Receipt_Amount) AS Total_Amount,
sum(chg.Balance) AS Total_Balance
FROM dbo.charges AS chg
LEFT JOIN dbo.ar_receipts AS ar
ON arcpts.production_ID = chg.production_ID
INNER JOIN dbo.entities AS te
ON buld.Name = te.Name
INNER JOIN dbo.Employee AS emp
ON ar.Charge_ID = emp.Charge_ID
WHERE
chg.Balance > 0 AND
chg.Created_Date >= @STARTDATE AND
chg.Created_Date <= @ENDDATE
GROUP BY
te.ID_Number, chg.product_ID
ORDER BY
te.ID_Number, chg.product_ID
想知道为什么我的查询没有汇总和总结所有“ID_Number”,与个别“Product_ID”,“Total_Receipts”老化?
使用 SQL Server Management Studio 创建了查询,我正确显示的输出有参数,我已经尝试了以下查询。
Totals:
count(ar.Product_ID) AS 'Total_Receipts'
sum(ar.Receipt_Amount) AS 'Total_Amount',
sum(chg.Balance) AS 'Total Balance'
目前我的 SQL SSMS 查询输出显示如下:
ID_Number Production_ID Total_Receipts Aging Month Year Total_Amount Total Balance
0017 105206 143 90 DAYS PLUS April 2021 7.00 7.00
0017 105206 143 90 DAYS PLUS April 2021 0.00 0.00
0017 105343 132 90 DAYS PLUS April 2021 0.00 0.00
0017 105343 132 90 DAYS PLUS April 2021 1.00 1.00
我真正想要的是有一个提供“Production_ID”的特定输出并按每个“ID_Number”合并,然后是“总计与 AGING”和 SUM 收据费用“Total_Amount" 如下所列:
ID_Number Production_ID Total_Receipts Aging Month Year Total_Amount Total Balance
0017 105206 143 90 DAYS PLUS April 2021 7.00 7.00
0017 105343 132 90 DAYS PLUS April 2021 1.00 1.00
[查询]
DECLARE @STARTDATE DATETIME2
DECLARE @ENDDATE DATETIME2
SET @STARTDATE = '2021-01-01 00:00:00'
SET @ENDDATE = '2022-02-10 00:00:00'
SELECT DISTINCT
te.ID_Number AS 'ID_Number',
chg.product_ID,
count(ar.Product_ID) AS 'Total_Receipts'
,CASE
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) < 30 THEN 'CURRENT'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 30 AND 59 THEN '30 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 60 AND 89 THEN '60 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) >= 90 THEN '90 DAYS PLUS'
END AS 'Aging',
DATENAME(MONTH, chg.Created_Date) AS 'Month',
DATEPART(YEAR, chg.Created_Date) AS 'Year',
sum(ar.Receipt_Amount) AS 'Total_Amount',
sum(chg.Balance) AS 'Total Balance'
FROM dbo.charges AS chg
LEFT JOIN dbo.ar_receipts AS ar
ON arcpts.production_ID = chg.production_ID
INNER JOIN dbo.entities AS te
ON buld.Name = te.Name
INNER JOIN dbo.Employee AS emp
ON ar.Charge_ID = emp.Charge_ID
WHERE
chg.Balance > 0 AND
chg.Created_Date >= @STARTDATE AND
chg.Created_Date <= @ENDDATE
GROUP BY
te.ID_Number,
chg.Created_Date,
chg.product_ID
ORDER BY
te.ID_Number,
Datename(month,chg.Created_Date)
例如,您可以使用 max
聚合列SELECT
te.ID_Number AS ID_Number,
chg.product_ID,
count(ar.Product_ID) AS Total_Receipts
,max(CASE
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) < 30 THEN 'CURRENT'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 30 AND 59 THEN '30 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) BETWEEN 60 AND 89 THEN '60 DAYS'
WHEN DATEDIFF(day, chg.Created_Date, @EndDate) >= 90 THEN '90 DAYS PLUS'
END) AS max_Aging,
max(DATENAME(MONTH, chg.Created_Date)) AS max_Month,
max(DATEPART(YEAR, chg.Created_Date)) AS max_Year,
sum(ar.Receipt_Amount) AS Total_Amount,
sum(chg.Balance) AS Total_Balance
FROM dbo.charges AS chg
LEFT JOIN dbo.ar_receipts AS ar
ON arcpts.production_ID = chg.production_ID
INNER JOIN dbo.entities AS te
ON buld.Name = te.Name
INNER JOIN dbo.Employee AS emp
ON ar.Charge_ID = emp.Charge_ID
WHERE
chg.Balance > 0 AND
chg.Created_Date >= @STARTDATE AND
chg.Created_Date <= @ENDDATE
GROUP BY
te.ID_Number, chg.product_ID
ORDER BY
te.ID_Number, chg.product_ID