Hopefully this is an easy question. I have a MySQL database which houses sales data for my company. I need to generate a report for total sales for an accounts fiscal year which is July 1st - June 30th... The database breaks hold monthly totals SalesBal1 for Jan, SalesBal2 for Feb and so forth. But the data has to be fitered by the fiscal year in the database which is a calendar year.
In the query below I can generate a report for July - Dec of 08, but I do not know how to then generate the Jan - June sales in 09... I know I could run a seperate query but I figure there is a simple way to combine this in one... Please let me know if my explanation is difficult to understand..

SELECT SUM(T.SalesBal7) As July_2008, SUM(T.SalesBal8) As Aug_2008, SUM(T.SalesBal9) As Sept_2008, SUM(T.SalesBal10) As Oct_2008, SUM(T.SalesBal11) As Nov_2008, SUM(T.SalesBal12) As Dec_2008FROM arcpsale T Left Outer Join arcusts T1 On T.CustID = T1.CustIDWHERE T1.CustAcct ='MAINE130^' and T.FiscalYear =2008

SELECT SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal7 else 0 END) As July_2008, SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal8 else 0 END) As Aug_2008, SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal9 else 0 END) As Sept_2008, SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal10 else 0 END) As Oct_2008, SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal11 else 0 END) As Nov_2008, SUM(CASE WHEN T.FiscalYear = 2008 then T.SalesBal12 else 0 END) As Dec_2008, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal1 else 0 END) As Jan_2009, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal2 else 0 END) As Feb_2009, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal3 else 0 END) As Mar_2009, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal4 else 0 END) As Apr_2009, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal5 else 0 END) As May_2009, SUM(CASE WHEN T.FiscalYear = 2009 then T.SalesBal6 else 0 END) As Jun_2009FROM arcpsale T Left Outer Join arcusts T1 On T.CustID = T1.CustIDWHERE T1.CustAcct ='MAINE130^' and T.FiscalYear BETWEEN 2008 AND 2009

Thank you for the responce. I'm now getting a syntext error "Right parenthese expected after aggregate SUM function argument, instead found 'WHEN' in source column expression.

SELECT SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal7 else 0 END) As July_2008, SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal8 else 0 END) As Aug_2008, SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal9 else 0 END) As Sept_2008, SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal10 else 0 END) As Oct_2008, SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal11 else 0 END) As Nov_2008, SUM(CASE T.FiscalYear WHEN 2008 then T.SalesBal12 else 0 END) As Dec_2008, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal1 else 0 END) As Jan_2009, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal2 else 0 END) As Feb_2009, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal3 else 0 END) As Mar_2009, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal4 else 0 END) As Apr_2009, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal5 else 0 END) As May_2009, SUM(CASE T.FiscalYear WHEN 2009 then T.SalesBal6 else 0 END) As Jun_2009FROM arcpsale T Left Outer Join arcusts T1 On T.CustID = T1.CustIDWHERE T1.CustAcct ='MAINE130^' and T.FiscalYear BETWEEN 2008 AND 2009

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time!
Introduction:
By the end of this Article it is my intention to bring the meaning and value of the above quote to…

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …