Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual FoxPro
Expertise: Beginner
Apr 8, 1999

Slecting the maximum value of a group of records

Question:
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.

Answer:
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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date