473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to group/list top 3 of each category w/o using Union?

Hello,

So my table contains say 100,000 records, and I need to group the
categories in fld1 by the highest count of subcategories. Say fld1
contains categories A, B, C, D, E.

All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA,
BB...BJ, CA, CB, CC...CJ, etc in fld2.

I am counting how many subcategories are listed for each category. Like
A may contain 5 of AA, 7 of AB, 3 of AC, 11 of AD...1 for the rest and
20 of AJ. B may contain 2 of BA, 11 of BB, 7 of BC, and 1 for the rest.
I want to pick up the top 3 subcategory counts for each category. Would
look like this:

Cat SubCat Count
A AJ 20
A AD 11
A AB 7
B BB 11
B BC 7
B BA 2

So event though each category contains 10 subcategories, I only want to
list the top 3 categories with the highest counts as above. If I just
do a group by and sort I can get this:

Cat SubCat Count
A ... ...
A
A
A
A
A
A
...
B ... ...
B
B
B
B
B
...

But I just want the top 3 of each category. The only way I can think of
to do this is to query each category individually and Select Top 3, and
then Union these guys into one query. The problem is that I have to
hardcode each category in the Union query. There may be new categoris
that I miss. Is there a way to achieve what I want without using Union?

Thanks,
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
3 4604
[posted and mailed, please reply in news]

Rich Protzel (rp*****@aol.co m) writes:
So my table contains say 100,000 records, and I need to group the
categories in fld1 by the highest count of subcategories. Say fld1
contains categories A, B, C, D, E.

All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA,
BB...BJ, CA, CB, CC...CJ, etc in fld2.

I am counting how many subcategories are listed for each category. Like
A may contain 5 of AA, 7 of AB, 3 of AC, 11 of AD...1 for the rest and
20 of AJ. B may contain 2 of BA, 11 of BB, 7 of BC, and 1 for the rest.
I want to pick up the top 3 subcategory counts for each category. Would
look like this:

Cat SubCat Count
A AJ 20
A AD 11
A AB 7
B BB 11
B BC 7
B BA 2

So event though each category contains 10 subcategories, I only want to
list the top 3 categories with the highest counts as above. If I just
do a group by and sort I can get this:


Here is a pragmatic solution:

CREATE TABLE #temp (id int IDENTITY,
fld1 char(2) NOT NULL,
fld2 int NOT NULL,
cnt int NOT NULL)

INSERT #temp (fld1, fld2, cnt)
SELECT xtype, id % 10, COUNT(*)
FROM sysobjects
GROUP BY xtype, id % 10
ORDER BY 3 DESC
OPTION (MAXDOP 1)

SELECT DISTINCT t.*
FROM #temp t
JOIN (SELECT fld1, id = MIN(id)
FROM #temp
GROUP BY fld1) AS s ON t.id BETWEEN s.id AND s.id + 2
ORDER BY t.fld1, t.cnt DESC
go
drop table #temp

Here I have replaced your table with a funky categorization of a system
tables. (Hint: post CREATE TABLE statement for your tables, INSERT
statements with sample data and the desired output and you get a
tested solution that fits your situation.)

The solution is not fool-proof. There is not really any guarantee that
the identity values are assigned in the order specified in the SELECT
statement. But it usually works, particularly with the query hint that
turns of parallelism.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
"Rich Protzel" <rp*****@aol.co m> wrote in message news:3f******** *************@n ews.frii.net...
Hello,

So my table contains say 100,000 records, and I need to group the
categories in fld1 by the highest count of subcategories. Say fld1
contains categories A, B, C, D, E.

All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA,
BB...BJ, CA, CB, CC...CJ, etc in fld2.

I am counting how many subcategories are listed for each category. Like
A may contain 5 of AA, 7 of AB, 3 of AC, 11 of AD...1 for the rest and
20 of AJ. B may contain 2 of BA, 11 of BB, 7 of BC, and 1 for the rest.
I want to pick up the top 3 subcategory counts for each category. Would
look like this:

Cat SubCat Count
A AJ 20
A AD 11
A AB 7
B BB 11
B BC 7
B BA 2

So event though each category contains 10 subcategories, I only want to
list the top 3 categories with the highest counts as above. If I just
do a group by and sort I can get this:

Cat SubCat Count
A ... ...
A
A
A
A
A
A
..
B ... ...
B
B
B
B
B
..

But I just want the top 3 of each category. The only way I can think of
to do this is to query each category individually and Select Top 3, and
then Union these guys into one query. The problem is that I have to
hardcode each category in the Union query. There may be new categoris
that I miss. Is there a way to achieve what I want without using Union?

Thanks,
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Here's a UDF that will return the top N for each category where
N is provided as an argument to the function.

CREATE TABLE CategoryCounts
(
category CHAR(1) NOT NULL,
subcategory CHAR(2) NOT NULL PRIMARY KEY,
cnt INT NOT NULL
)

-- Sample data
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('A', 'AA', 5)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('A', 'AB', 7)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('A', 'AC', 3)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('A', 'AD', 11)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('A', 'AE', 1)

INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('B', 'BA', 2)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('B', 'BB', 11)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('B', 'BC', 7)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('B', 'BD', 1)
INSERT INTO CategoryCounts (category, subcategory, cnt)
VALUES ('B', 'BE', 1)

CREATE FUNCTION TopNFromEachCat egory
(@n INT)
RETURNS TABLE
AS
RETURN(
SELECT C1.category, C1.subcategory, C1.cnt
FROM CategoryCounts AS C1
WHERE @n >= (SELECT COUNT(DISTINCT C2.cnt)
FROM CategoryCounts AS C2
WHERE C1.category = C2.category AND
C2.cnt >= C1.cnt)
)

SELECT *
FROM TopNFromEachCat egory(3)
ORDER BY category ASC, cnt DESC, subcategory ASC

category subcategory cnt
A AD 11
A AB 7
A AA 5
B BB 11
B BC 7
B BA 2

Regards,
jag
Jul 20 '05 #3
Just a quick update on what I tried with the given solutions:

I created a static temp table which included an ID field, a Category
field, subCategory, and cnt. The first select statement here produced
the desired results with my actual data of retrieving only the top 3
subcategories (with the highest cnt's) for each category in Query
Analyzer. Note that in the temp table I ordered the table by Category
ASC and for cnt DESC.

SELECT DISTINCT t.*
FROM Temp t
JOIN (SELECT Category, id = MIN(id)
FROM Temp
GROUP BY Category) AS s ON t.id BETWEEN s.id AND s.id + 2
ORDER BY t.Category, t.cnt DESC
I also created a temp table without the ID field but all the rest of the
fields as above. The following select statement did not limit the
subcategories to the top 3. It did remove a few of the subcategories,
but removed the subcategories with the highest count. Maybe I missed
something or maybe it needs an ID field. I would be interested to know
what it would take to make this second procedure work as desired - since
it is so similar to the first one, except doesn't use ID field, and
doesn't use Min function, but uses Count instead.

SELECT C1.category, C1.subcategory, C1.cnt
FROM Temp AS C1
WHERE 3 >= (SELECT COUNT(DISTINCT C2.cnt)
FROM Temp AS C2
WHERE C1.category = C2.category AND
C2.cnt >= C1.cnt)
Again, I thank you all for helping me out.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1523
by: Betty Harvey | last post by:
The next meeting of the XML Users Group will be held on Wednesday, February 18, 2004 at the American Geophysical Union (AGU) at 2000 Florida Avenue, N.W., Washington, DC 20009-1277. The meeting starts at 7:00 p.m. and usually last approximately 2 hours. If attending the meeting by Metro, get off the Dupont Circle stop and walk north to Florida Avenue...turn right. Michael Hahn will be chairing the February meeting. There is no cost...
6
2499
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new products). Tables: tblCategoryDetails CategoryID SpecID
3
2131
by: Abhi | last post by:
Hi! I am wondering if this query is possible somehow: I have a table with many fields that all can have a value from 1 to 5. if I wanna see the count of each value from one field, then this is easy: SELECT field1, count(field1) as cntnr FROM table group by field1
2
3032
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can be associated with multiple categories. I have an "Issue," "IssueCategory," and "Category" in the database (among other tables). The form has a subform in it which is tied to the "IssueCategory" table. The main form is tied to the "Issue"...
3
5542
by: Egbert | last post by:
I use a System.Web.UI.WebControls.CheckBoxList object to fill a list. I need to label groups of checkboxes in the list by Category (Determined dynamically from the database) at runtime. Is it possible to group checkboxes in 1 CheckboxList? Something like this: *************************
1
15529
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can. So far I have // list.cpp
17
3125
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note that I am using Allen Browne's multi-select list box for a report as a guide. I should also note that my access skills are not the best so I may need some explaining on certain things. First let me give some background on the database: I have a...
2
2727
by: benhaynes | last post by:
I am writing a query to create a tree menu, it pulls from a table of music "tracks". In this database there are four "sub_genre" fields for each track, and I need to create a list of all used sub_genres and how many tracks are in each. The list will look like this: Acid Jazz (50) Big Beat (1) Breakbeat (75) Chill (27) Dance (12) Drum & Bass/Jungle (12)
2
1711
by: gnawz | last post by:
I have a table that consists of 5 categories and each category has items called brands. I want to be able to list the Category of each set of brands on top of the list using PHP and MySQL in tabular form Some thing like this Fruits apples mangoes
0
8130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8541
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5510
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2531
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.