Solved

Grouping Codes In Sql

Posted on 2011-02-16
7
320 Views
Last Modified: 2012-05-11
Hi All

I have a table in Sql which consists of 293 Analysis Codes ie:

aa12345
aa123456
bb123
cc3456

What i need to be able to do is to group them in to certain products so for example

aa12345 and aa123456 need to be grouped and bb123 and cc3456 need to be grouped

so then i could create a bar graph for products showing which type of product has been sold.

so diodes for instance would consist of the aa12345 and the aa123456 anaylysis codes.

I am Using SQL Server 2008 Standard Edition

Any help on how to do this would be great.

Thanks

John
0
Comment
Question by:pepps11976
7 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 34906025
hi

if i got you currect you want to order the values in groups not do a simple aggragate but any way.

1.
select analisyscodes from (
                                              select analisyscodes,
                                              case substring(analisyscodes,1,2) when 'aa' then 1
                                                                                                       when 'bb' then 2
                                                                                                       ....
                                                                                                       ....
                                                                                                       when 'zz' then 24 end ord ) as Tab
order by ord ASC

2.
select count(*) , substring(analisyscodes,1,2)
from table
group by substring(analisyscodes,1,2)
order by count(*) desc

                                                   
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 34906062
Groping codes sounds much more interesting then Grouping codes.....:)

However....
Do you have a table that contains the relationship between the codes and the groups you want to create?

You stated that :
aa12345 and aa123456 need to be grouped and bb123 and cc3456 need to be grouped
If this is correct and not a typo, then ashilo's response does not quite work for you since it will not group bb... and cc.... codes together.

What is it that determines that bb123 and cc3456 should be grouped together?
0
 

Author Comment

by:pepps11976
ID: 34906085
So at the moment i have this

SELECT DISTINCT it_anal
FROM         dbo.itran

which gives me this

AAMCSAMP
AERFERRI
AEROWEIN
CONYYREW
HERTUY
OUTGYU

for the above i would need AAMCSAMP,AEROWEIN grouped, And HERTUY and OUTGYU grouped

itried your example but it failed i probably have the syntax incorrect
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 34906086
Perhaps you should create another table called something like product type:

id   name
1   resistors
2   diodes

and then create a new column in you product table called product_type, putting your products in to their type:
id name                product_type
1   aa12345          1
2  aa123456         1
3  bb123                2
4  cc3456              2

then the query would be

select * from  analisyscodes ac
left join product_type pt
on ac. product_type = pt.id
where pt.id = 1
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 34906141
hi

i would have to agree with @darren-w-
if there is no simple vivible corraletion between the values then you will have to create a refference table.
0
 

Author Comment

by:pepps11976
ID: 34906610
Ok Guys i will follow the instructions of Darren-W and see how i get on.

Sorry about the Title.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 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

22 Experts available now in Live!

Get 1:1 Help Now