简体   繁体   中英

How to roll up Batch Invoices

I have data Invoices, I only need to see total receipts processed from a given time frame from each associated individual ID_Number.

ID_Number   Month    Total_Invoices     Total_Amount_Processed
NULL        1        200                6853.72
20                   1                  5.61
20                   1                  5.97
20                   1                  7.05

I'm looking for this output:

ID_Number   Month   Total_Invoices   Total_Amount_Processed
20          12      203              6872.35         

[Query]

SELECT DISTINCT
       ge.ID_Number,
       MONTH(arr.Receipt_Date) AS Month,
       COUNT(arr.Receipt_ID) AS Total_Invoices,
       SUM(Receipt_Amount) AS Total_Amount_Processed
FROM dbo.ar_receipts arr
     INNER JOIN dbo.ar_batch_receipts arbr ON arr.Batch_Receipt_ID = arbr.Batch_Receipt_ID
     INNER JOIN dbo.ar_receipt_types art ON arr.Receipt_Type_ID = art.Receipt_Type_ID
     INNER JOIN dbo.users usr ON arbr.User_ID = usr.UserID
     INNER JOIN dbo.gl_entities ge ON arbr.Entity_ID = ge.Entity_ID
WHERE usr.Active = 1
  AND arbr.Batch_Date >= DATEADD(MONTH, -1, GETDATE())
GROUP BY ge.Entity_Number,
         arr.Receipt_Date,
         arr.Receipt_ID WITH ROLLUP;

As commented you really do not need "select distinct" if you are using "group by"

SELECT DISTINCT -- "distinct" is not needed
...
GROUP BY        -- if you are using group by

A simple way to design a group by query is to copy all "non-aggregating" columns mentioned in the select clause into the group by clause- these should be the only columns you need in the group by.

SELECT
       ge.ID_Number,                       -- this is "non-aggregating"
       MONTH(arr.Receipt_Date) AS Month,   -- this is "non-aggregating"
       COUNT(arr.Receipt_ID) AS Total_Invoices,       -- COUNT() is an aggregation
       SUM(Receipt_Amount) AS Total_Amount_Processed  -- SUM() is an aggregation

so copy/paste those into the group by clause but remove any column aliases as they will cause errors if you forget to remove them:

GROUP BY
       ge.ID_Number,             -- this is "non-aggregating"
       MONTH(arr.Receipt_Date)   -- but do you want a new row for each "Month"?

Because you are grouping by "Month" if you data spans November and December you would get 2 rows. So the way you filter for dates arbr.Batch_Date >= DATEADD(MONTH, -1, GETDATE()) can result in some dates of November and some dates of December if getdate() was 12 Dec 2021. Perhaps you need to alter the way you select dates?

If you actually wanted all invoices for the previous month, and only that month, then a better way to filter for the dates would be as follows:

WHERE usr.Active = 1
  AND arbr.Batch_Date >= DATEADD(MM,-1,DATEADD(dd, - (DAY(getdate()) - 1), cast(getdate() as date))) 
  AND arbr.Batch_Date < DATEADD(dd, - (DAY(getdate()) - 1), cast(getdate() as date))

These seemingly complex set of function calls simply calculates the first date of the previous month (eg if today was 12 Dec 2021 then 2021-11-01) and the first day of the current month (eg if today was 12 Dec 2021 then 2021-12-01) and so you would be searching for arbr.Batch_Date >= 2021-11-01 AND arbr.Batch_Date < 2021-12-01

Now your group by clause would only get 1 "Month" row because all the data is in the same month.

So overall I suggest the following:

SELECT
       ge.ID_Number, 
       MONTH(arr.Receipt_Date) AS Month,
       COUNT(arr.Receipt_ID) AS Total_Invoices,
       SUM(Receipt_Amount) AS Total_Amount_Processed 
FROM dbo.ar_receipts arr
     INNER JOIN dbo.ar_batch_receipts arbr ON arr.Batch_Receipt_ID = arbr.Batch_Receipt_ID
     INNER JOIN dbo.ar_receipt_types art ON arr.Receipt_Type_ID = art.Receipt_Type_ID
     INNER JOIN dbo.users usr ON arbr.User_ID = usr.UserID
     INNER JOIN dbo.gl_entities ge ON arbr.Entity_ID = ge.Entity_ID
WHERE usr.Active = 1
  AND arbr.Batch_Date >= DATEADD(MM,-1,DATEADD(dd, - (DAY(getdate()) - 1), cast(getdate() as date))) 
  AND arbr.Batch_Date < DATEADD(dd, - (DAY(getdate()) - 1), cast(getdate() as date))
GROUP BY
       ge.ID_Number, 
       MONTH(arr.Receipt_Date)

btw you do not need with rollup to achieve the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM