Solved

SQL syntax

Posted on 2006-11-14
3
285 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
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 142

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

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

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

929 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

12 Experts available now in Live!

Get 1:1 Help Now