I'm working on a SQL project where I have a stored procedure where I return the total invoices of each customer for a specific time period. I have written a stored procedure that returns the total amount for each customer however the total results are a little bit above the real amount.
- Customer 1 Total: 23 (finding him with query individually)
- Customer 1: Total 25 (using a stored procedure)
This is my stored procedure and I have the tables below. Thank you for your time.
CREATE PROCEDURE QUERY4
@p_StartDate DATE, @p_EndDate DATE
AS
BEGIN
SELECT ct.*, SUM(iv.Total) AS Total
FROM Customer AS ct, Invoice AS iv
WHERE ct.CustomerId = iv.CustomerId
AND iv.InvoiceDate BETWEEN @p_StartDate AND @p_EndDate
GROUP BY ct.CustomerId, ct.FirstName, ct.LastName, ct.Company, ct.Country, ct.State, ct.City,
ct.Address, ct.PostalCode, ct.Phone, ct.Fax, ct.Email, ct.SupportRepId
ORDER BY Total DESC
END;