Solved

Grouping Codes In Sql

Posted on 2011-02-16
7
319 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

708 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

10 Experts available now in Live!

Get 1:1 Help Now