I am performing the query below, it is a simple query that checks for records in each month of the companies financial year, by branch but it is returning results that are in excess of the total for the year overall. Its the first time I have tried a query like this and I didn't want to use BETWEEN two dates because I wanted to ensure (no matter how unlikely) that I didn't miss anything by missing off a minute/second etc
Query is below, can anyone see anything wrong?
SELECT Branches.BraID, Branches.Branch, (SELECT SUM(Quote) AS QuoteCount FROM MainTable WHERE Branches.BraID = MainTable.BraID AND (DATEPART(Mm, MainTable.CreatedDate) = 6 OR DATEPART(Mm, MainTable.CreatedDate) = 7 OR DATEPART(Mm, MainTable.CreatedDate) = 8 OR DATEPART(Mm, MainTable.CreatedDate) = 9 OR DATEPART(Mm, MainTable.CreatedDate) = 10 OR DATEPART(Mm, MainTable.CreatedDate) = 11 OR DATEPART(Mm, MainTable.CreatedDate) = 12 AND DATEPART(Yy, MainTable.CreatedDate) = 2012) OR (DATEPART(Mm, MainTable.CreatedDate) = 1 OR DATEPART(Mm, MainTable.CreatedDate) = 2 OR DATEPART(Mm, MainTable.CreatedDate) = 3 OR DATEPART(Mm, MainTable.CreatedDate) = 4 OR DATEPART(Mm, MainTable.CreatedDate) = 5 AND DATEPART(Yy, MainTable.CreatedDate) = 2013)) AS QuotedLastFY FROM Branches ORDER BY QuotedLastFY DESC, Branch