?
Solved

Query to find top attributes for each product

Posted on 2014-10-10
1
Medium Priority
?
119 Views
Last Modified: 2015-01-08
Experts,
I have a product database.  It contains hundreds of thousands of transaction history records for thousands of products over time. There 20-30 columns that store various attributes for each record e.g.  productid, productname etc. and also columns for e.g. product size and various other product features, etc. as well as columns for customer data pertaining to the purchase.

What I would like to do is find the TOP n number of some given product feature for each product.
e.g. top 5 sizes for each product or top 5 zipcodes of customers who purchased the product.

Ideally I would like the result set to look like this:
e.g. Top 5 zipcodes per product - each zipcode in a column:

Product ID   Zipcode  Zipcode  Zipcode Zipcode Zipcode

or
Top 5 zipcodes all in the same column:

ProductID Zipcode, Zipcode, Zipcode, Zipcode

What is the best approach to this sort of problem?

Thanks in advance.
0
Comment
Question by:bobinorlando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40374405
From a SQL perspective the best approach is neither of those output options.

But if I had to choose from only those 2 options I would go with the second; because you can "union all" the individual outputs

select TOP (5) productid, comma_list, 'zipcodes' as [what]
from ...
group by
order by ... DESC

UNION ALL

select TOP (5) productid, comma_list, 'style/colour' as [what]
from ...
group by
order by ... DESC

...

If doing a temp table or similar, I would just insert using a table structure like this

productid | topnvalue (varchar) | what (varchar)

then you can fiddle with the output of this to meet the need.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question