Solved

SQL syntax

Posted on 2006-11-14
3
304 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 143

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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SELECT * FROM table WHERE keyword…  (3 SQL queries needed) 10 100
best datatype for oracle table email creation 8 55
Perl Versus AWK? 7 46
Database ERD 4 24
I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

808 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