Link to home
Start Free TrialLog in
Avatar of terminator_vn
terminator_vnFlag for United States of America

asked on

SQL Statement for Group By and Sum

Hello Experts,

Can you help to fix this SQL Statement:  I can select statement to show all the SONumber, But Can't get it to work for a total amount in Month.  Please view a script and picture  below

select
tblServiceOrders.ARCustomerNumber, tblServiceOrders.SONumber, tblServiceOrders.Status AS ServiceStatus, tblInvoices.status AS InvoiceStatus, tblInvoices.InvoiceDate, SUM(tblInvoiceDetail.TotalPostedCost) AS DetailTotalPostedCost, SUM(tblInvoiceDetail.TotalSellingPrice) AS DetailTotalSellingPrice,
SUM(tblInvoiceDetail.TotalSellingPrice) - SUM(tblInvoiceDetail.TotalPostedCost) as Margin
FROM tblInvoices
 INNER JOIN tblAccounts
            ON  tblInvoices.AccountNumber = tblAccounts.AccountNumber
       LEFT OUTER JOIN tblReps
            ON  tblInvoices.SalesRep = tblReps.RepNumber
       INNER JOIN tblInvoiceDetail
            ON  tblInvoices.InvoiceNumber = tblInvoiceDetail.InvoiceNumber
       LEFT OUTER JOIN tblInvoiceAssemblyDetail
            ON  tblInvoiceDetail.InvoiceDetailKeyID = tblInvoiceAssemblyDetail.FKInvoiceDetail
		INNER JOIN tblServiceOrders
			ON tblInvoices.AccountNumber = tblServiceOrders.AccountNumber
			
						
			WHERE tblInvoices.status = 'Posted' AND tblInvoiceDetail.TotalSellingPrice > 0 
			GROUP BY tblServiceOrders.ARCustomerNumber, tblServiceOrders.SONumber, tblServiceOrders.Status,tblInvoices.Status, tblInvoices.InvoiceDate

			ORDER By tblServiceOrders.SONumber ASC;

Open in new window


I want to be able to get a total amount for all items in that particular MONTH associated with SONumber.  We may have the same SONumber with different month because the Invoice is posted on different time. How do I get a total for all DetailTotalPostedCost and DetailTotalSellingPrice in July, June, and August...ect.  base on ID 30003

For example:

SONumber                            Date                                          DetailTotalPriceCosted                          DetailTotalSellingPrice
30003                                     2014-June                                  1111111111111
30003                                     2014-July                                    34444322
30003                                     2014-August                              443333

I would like to thank you for your help in advanced
sqlstatement.jpg
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of terminator_vn

ASKER

Thank you for all yours help.