February 9, 2017 at 10:40 am
Select Invoice_Exceptions.DateTime, Invoice_Exceptions.Invoice_Number, Invoice_Exceptions.ItemNum, Inventory.ItemName, Invoice_Exceptions.Reason_Code, Invoice_Exceptions.LineNum, Invoice_Exceptions.Amount, Invoice_Exceptions.Quantity, SUM((Invoice_Exceptions.Amount) * (Invoice_Exceptions.Quantity)) AS Voided_Total, Invoice_Totals.Payment_Method
From Invoice_Exceptions
Inner join Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
Inner Join Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
where Invoice_Exceptions.DateTime between '2017-01-01 04:50:08.000' and '2017-02-08 03:50:08.000' and Invoice_Totals.Payment_Method = 'CA'
GROUP BY Invoice_Exceptions.DateTime, Invoice_Totals.Payment_Method, Invoice_Exceptions.Invoice_Number, Invoice_Exceptions.ItemNum, Inventory.ItemName, Invoice_Exceptions.Reason_Code, Invoice_Exceptions.LineNum, Invoice_Exceptions.Amount, Invoice_Exceptions.Quantity, day(Invoice_Exceptions.DateTime)
Order by Invoice_Exceptions.DateTime
The SQL code above gives me the data that I need...these are invoices in a POS system. But i need to group by individual days, ala, Jan 1, Jan 2 etc..and get the Total Amount for that day [Invoice_Exceptions.Amount]
Any help appreciated.
Chris
February 9, 2017 at 11:29 am
i think this will do it.
i'm using datemath to round the dates down to midnight of each date, and grouping by that
select DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
SUM(Invoice_Exceptions.Amount) As TotalAmount
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDatee
Lowell
February 9, 2017 at 11:56 am
Lowell - Thursday, February 9, 2017 11:29 AMi think this will do it.
i'm using datemath to round the dates down to midnight of each date, and grouping by that
select DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
SUM(Invoice_Exceptions.Amount) As TotalAmount
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDatee
Thank you for the response, but that omits my other fields...Id love to have the totals all on the same table. Is that possible?
February 9, 2017 at 11:59 am
chef423 - Thursday, February 9, 2017 11:56 AMLowell - Thursday, February 9, 2017 11:29 AMi think this will do it.
i'm using datemath to round the dates down to midnight of each date, and grouping by that
select DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
SUM(Invoice_Exceptions.Amount) As TotalAmount
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDateeThank you for the response, but that omits my other fields...Id love to have the totals all on the same table. Is that possible?
Id love to have the TotalAmount, TotalQuantity, VoidedTotal on a separate line below each day (1st, 2nd, 3rd, etc)
February 9, 2017 at 12:04 pm
Lowell - Thursday, February 9, 2017 11:29 AMi think this will do it.
i'm using datemath to round the dates down to midnight of each date, and grouping by that
select DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
SUM(Invoice_Exceptions.Amount) As TotalAmount
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDatee
Good idea. You don't need the DATEADD in the GROUP BY, though. It's just using up clock cycles.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 9, 2017 at 12:13 pm
chef423 - Thursday, February 9, 2017 11:59 AMchef423 - Thursday, February 9, 2017 11:56 AMLowell - Thursday, February 9, 2017 11:29 AMi think this will do it.
i'm using datemath to round the dates down to midnight of each date, and grouping by that
select DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
SUM(Invoice_Exceptions.Amount) As TotalAmount
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDateeThank you for the response, but that omits my other fields...Id love to have the totals all on the same table. Is that possible?
Id love to have the TotalAmount, TotalQuantity, VoidedTotal on a separate line below each day (1st, 2nd, 3rd, etc)
you would just add num SUM() or MIN() or MAX() or COUNT() for each of the columns you are talking about, then...the group by would still be the same.
I'm pretty sure the group by would be manditory in this case.
[SELECT
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0) As ExceptionDate,
MAX(Invoice_Exceptions.Invoice_Number) AS MaxInvoiceNumber,
MIN(Invoice_Exceptions.ItemNum) AS MinItemNum,
COUNT(Inventory.ItemName) AS CountItemName,
COUNT(Invoice_Exceptions.Reason_Code) AS CountReason_Code,
COUNT(Invoice_Exceptions.LineNum) AS CountLineNum,
SUM(Invoice_Exceptions.Amount) As TotalAmount,
SUM(Invoice_Exceptions.Quantity) AS TotalQuantity,
SUM(( Invoice_Exceptions.Amount ) * ( Invoice_Exceptions.Quantity )) AS Voided_Total,
Invoice_Totals.Payment_Method
FROM
Invoice_Exceptions
INNER JOIN Inventory
ON Invoice_Exceptions.ItemNum = Inventory.ItemNum
INNER JOIN Invoice_Totals
ON Invoice_Exceptions.Invoice_Number = Invoice_Totals.Invoice_Number
WHERE
Invoice_Exceptions.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND Invoice_Totals.Payment_Method = 'CA'
GROUP BY
DATEADD(dd, DATEDIFF(dd,0,Invoice_Exceptions.DateTime), 0)
ORDER BY
ExceptionDate
Lowell
February 10, 2017 at 8:57 am
How about this:
SELECT CAST(IX.[DateTime] AS date) AS [Date], IX.Invoice_Number, IX.ItemNum, I.ItemName, IX.Reason_Code, IX.LineNum,
IX.Amount, IX.Quantity, SUM(IX.Amount * IX.Quantity) AS Voided_Total, IT.Payment_Method
FROM Invoice_Exceptions AS IX
INNER JOIN Inventory AS I
ON IX.ItemNum = I.ItemNum
INNER JOIN Invoice_Totals AS IT
ON IX.Invoice_Number = IT.Invoice_Number
WHERE IX.DateTime BETWEEN '2017-01-01 04:50:08.000' AND '2017-02-08 03:50:08.000'
AND IT.Payment_Method = 'CA'
GROUP BY CAST(IX.[DateTime] AS date), IT.Payment_Method, IX.Invoice_Number, IX.ItemNum, I.ItemName, IX.Reason_Code,
IX.LineNum, IX.Amount, IX.Quantity
ORDER BY IX.[DateTime];
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
February 10, 2017 at 9:21 am
Lots of the code posted is great. The problem that I see with most of it is the omission of "zero" days that will occur. You need a reference that contains all days if you want to report on "zero" days.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply