Alright, that's better, now my main question is: The table in your database you are representing with 'Database Table' I assume has data that has at least one record for every FY IN TOTAL. In other words if it's orders for a customer, I don't really care who the customer is, but there is at least one order for every year from 2010 through 2013. Is that correct?
In other words let's say that your GROUPCODE is actually a representation of your customer if you add one record for group 2 and put the Year as 2012 we would have all years represented across all customers.
I put in item number 1, quantity 5, group code of Group 2, year of 2012 for the purposes of my example.
I also removed all spaces from field/table names (they cause more problems than they are worth and you should try to stay away from the practice of having anything but text and underscores(_) in your object names.
So the first thing is to create a list of years:
Code:
SELECT DatabaseTable.Year
FROM DatabaseTable
GROUP BY DatabaseTable.Year;
One other thing to mention here is that YEAR is a reserved word and you probably shouldn't use it.
Name this query qryYears
next a summary of the crosstab items:
Code:
SELECT DatabaseTable.ItemNo, Sum(DatabaseTable.Qty) AS Qty, DatabaseTable.Year
FROM DatabaseTable
GROUP BY DatabaseTable.ItemNo, DatabaseTable.Year, DatabaseTable.GroupCode
HAVING (((DatabaseTable.GroupCode)="GROUP 1"));
Name this query qryOrders
Now the crosstab itself:
Code:
TRANSFORM Sum(qryOrders.Qty) AS SumOfQty
SELECT qryOrders.ItemNo
FROM qryYears LEFT JOIN qryOrders ON qryYears.Year = qryOrders.Year
GROUP BY qryOrders.ItemNo
PIVOT qryYears.Year;
you're always going to have a 'blank' line in this query but you can remove it by building another query based on the crosstab query and put is not null in the itemno column.
There are ways to economize on the number of queries to build this but I've always prefered having readily editable sub queries you can change easily as demand necessitates.