?
Solved

SQL syntax

Posted on 2006-11-14
3
Medium Priority
?
322 Views
Last Modified: 2008-02-01
Hello,

I need to transpose the information in a table and substitue words for letter abreviations, basically taking sevral rows of data and putting it into one row/column.  

Here is a sample of the information in my DB currently...

[Bird ID]            [Defects]
-----------------------------------
365965544         Slight Legs
365965544         Toes
365965545         Slight Legs
365965545         Toes
365965549         Severe Legs
365965549         Severe Toes
365965551         Slight Legs
365965553         Slight Legs

Here is what i want to turn it into...

[Bird ID]            [Defects]
-----------------------------------
365965544         SL, T
365965545         SL, T
365965549         SL, ST
365965551         SL
365965553         SL

Thanks!

-Navicerts
0
Comment
Question by:Navicerts
[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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17942026
what are the exact rules for the transformation fo  [Defects] into the short version?


in short:

create function dbo.concate_itemid ( @birdid )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + defects
  from yourtable where [bird id]= @birdid
  return (@res)
end


and use it like this

select [bird id], dbo.concate_itemid([bird id])
from yourtable
group by [bird id]
0
 
LVL 7

Author Comment

by:Navicerts
ID: 17942160
I think this will work great.  I can add a case statement to replace the string with letter abbreviations, unless you know a better way?

Here is what I got it is working good for me, Thank You!! (I need to add the rest of the letter replacements in)


create function dbo.concate_itemid ( @birdid int )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + case when defect = 'Slight Legs' then 'SL' else defect end
  from Defects where [bird id]= @birdid
  return (@res)
end

GO

select [bird id], dbo.concate_itemid([bird id])
from defects
group by [bird id]


-Navicerts
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17942208
I would, instead of doing it in the code, have a "translation" table of the values and the replacements...

you might also try to do it automatically, by extracting all the characters after a space...

create function dbo.GetShortValue ( @d varchar(100) )
returns varchar(100)
as
begin
  declare @res varchar(100)
  declare @x int

  set @res = substring(@d, 1,1)
  set @x = 2
  while (@x > 0)
  begin
    set @x = charindex(@d, ' ' , @x)
    if (@x>0)
    begin
      set @res = @res + substring(@d, @x+1, 1)
      set @x = @x +2
    end      
  end      

  return (@res)
end

and use it like this:


create function dbo.concate_itemid ( @birdid int )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + dbo.GetShortValue (defect )
  from Defects where [bird id]= @birdid
  return (@res)
end

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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