Solved

Large SQL Table Need Group and SubGroup

Posted on 2009-04-09
3
759 Views
Last Modified: 2012-08-14
Hello,
I have a table that has 2.5 million records in it and I want to be able to display this data in "groups" on one specific field.  
Assume the field is "Tag" and it is a VARCHAR(50).  
What I want to be able to do is list all the rows grouped by the first character of the TAG.  

However, because there are so many records, I also want to display this data in "pages".

For Example, for all rows that begin with "A", I want to then group by the 2nd character:
AAxxxx- AByyyyy
AC- AD
AD - AE
AF-  AG
Etc.
xxxxxx and yyyyy woudl be the remainder of the FIRST and LAST TAG name in the group.
The result would be a 26 groups under A for each letter of the alphabet.  When someone selects the AA-AB group, I would then load all the Tags that have LEFT(Tag,2) in ('AA','AB')

The table is stored in a MS SQL 2000 database.  Any suggestions would be greatly appreciated.

Thanks
0
Comment
Question by:aspireworks
[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
  • 2
3 Comments
 
LVL 17

Expert Comment

by:k_murli_krishna
ID: 24113429
SELECT tag, <column-list> FROM YourTable WHERE ............. GROUP BY SUBSTRING(tag,1,2), SUBSTRING(tag,3,2), <column-list> ORDER BY .............;
0
 

Author Comment

by:aspireworks
ID: 24117946
Thanks, it seemed to get me going down the right path, but didn't quite give me the results I was looking for.
I was able to come up with this code, but it runs REALLY slow.
Here's my SQL:
SELECT DISTINCT code, 
   (SELECT TOP 1 tag FROM table WHERE  SUBSTRING(tag,1,2) = code and LEN(tag) > 2 ORDER BY tag ASC) -- return 1st TAG in group code
,(SELECT TOP1 tag FROM TABLE WHERE SUBSTRING(tag,1,2) = code and LEN(tag) > 2 ORDER BY tag desc) -- return LAST TAG in group code   
FROM TABLE bb (NOLOCK)
INNER JOIN  
(SELECT DISTINCT SUBSTRING(tag,1,2) AS code FROM TABLE WHERE LEN(tag) > 2) cc
ON SUBSTRING(tag,1,2) = cc.code 
ORDER BY code ASC

Open in new window

0
 
LVL 17

Accepted Solution

by:
k_murli_krishna earned 500 total points
ID: 24121522
Try a simpler version:
SELECT DISTINCT SUBSTRING(tag,1,2),
                             (SELECT TOP 1 tag FROM table WHERE  LEN(tag) > 2 ORDER BY tag ASC) ,
                             (SELECT TOP 1 tag FROM TABLE WHERE LEN(tag) > 2 ORDER BY tag DESC)
FROM TABLE bb
GROUP BY  SUBSTRING(tag,1,2);

You can remove the DISTINCT if it is not necessary.

 


   

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

729 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