Solved

Query to find top attributes for each product

Posted on 2014-10-10
1
98 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
1 Comment
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now