Let’s say I have a table (table1) with 100 unique ID numbers, each belonging to one of five categories. On another table (table2) I have a whole lot of info for each of these ID numbers. Table2 may have more than one record for each of the ID numbers?each record having a different date and cost.
I need to create a cross-tabulation with the categories from table1 grouped along the vertical axis, and the costs from table2 grouped along the horizontal axis, with the number of ID numbers totaled to fill in the data area. The trick is that I need to eliminate all data fields except for the one with latest date.
I would love to use the cross-tab feature in the query designer, but to use it I must have only three fields in the selected output. I could do this if I could have several fields and just select the three I want to be involved in the cross-tab.
The simplest way to achieve what you need is to run a query that will result in a cursor/table that has the the three columns you need, and then run the CrossTab against this intermediate result set. To eliminate all except for the latest date, you may want to use the MAX() SQL funcion.
Something that is very important when doing this is to make sure that you do what is called an “outer join.” An outer join makes sure that even if a particular category did not have any cost records in table 2 for the date range you have chosen, that there is at least one record in the intermediate table where the category is used. If not, then the crosstab will only give you information on the categories that were in the intermediate table, not all of the categories that exist in table 1.