Skip to content
Advertisement

How can I pull out the second highest product usage from a SQL Server table?

We have a product usage table for software. It has 4 fields, [product name], [usage month], [users] and [Country]. We must report the data by Country and Product Name for licensing purposes. Our rule is to report the second highest number of users per country for each product. The same products can be used in all countries. It based on monthly usage numbers, so second peak usage for fy 2020. Since all of the data is in one table I am having trouble figuring out the SQL to get the information I need from the table.

I am thinking I need to do multiple selects (inner select? ) and group the data in a way to pull out the product name, peak usage and country. But that is where I am getting confused as to the best approach.

Example Data looks like this:

I had originally thought about breaking this out into multiple tables and then trying sql against each product table, but since this is something I will need to do monthly, I didn’t want to redesign the ETL that loads the data because 1) I don’t control that ETL and 2) I felt like that would be a move backwards for a repetitive task. We were also looking into Power BI to do this for us, but haven’t foound the right approach, and I would honestly rather have this in SQL.

Advertisement

Answer

If I follow you correctly:

This generates one row per product and country, that corresponds to the second highest number of users.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement