Slecting the maximum value of a group of records

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a