Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Grouping values of a column based on similar names

Author  Topic 

mrm23
Posting Yak Master

198 Posts

Posted - 2010-10-19 : 05:28:08
Hi All,

I have a table with a million rows.
There is an object_name column which has data like:

object_name

Microsoft SQL Server 2000
Microsoft SQL Server 2005
Microsoft SQL Server 2000
Microsoft SQL Server 2008
Microsoft SQL Server 2008
Microsoft SQL Server 2005
.
.
.
.
1 million rows

Now, I need to make 3 groups out of these million records as

2000||2005||2008
a || b || c

a,b,c being the data related to them.

How can i make this?
Currently we have a query for this which uses CASE statement.

But in the real scenario, we have 96 such groups!!!!!
So writing 96 Case statements has slowed down the query badly and i need to tune this now.

Is there a better approach for this?

Thanks in adv.

michael.appleton
Posting Yak Master

160 Posts

Posted - 2010-10-19 : 05:58:18
First you have to decide what makes them a group. In the example you gave, the last four characters define each group. Then just use the group syntax in SQL to make them groups in each row rather than columns. You can format or transpose the data to columns as groups afterwards in the front end.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-19 : 13:24:44
will number of groups be known before? or will it vary at runtime?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -