Solved

SQL syntax

Posted on 2006-11-14
3
276 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
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now