SQL Question

Thread Solved

Join Date: Mar 2007
Posts: 158
Reputation: vuyiswamb is an unknown quantity at this point 
Solved Threads: 5
vuyiswamb's Avatar
vuyiswamb vuyiswamb is offline Offline
Junior Poster

SQL Question

 
0
  #1
May 7th, 2009
Good Morning All

I have a table named “Final” with Values like this


  1. ID || DESCR || CYCLE
  2. ======================================
  3. 1 || Earl G || 20
  4. 2 || Earl G || 21
  5. 3 || Earl G || 22
  6. 4 || Davidson I Dr || 20
  7. 5 || Davidson I Dr || 21
  8. 6 || Davidson I Dr || 22
  9. 7 || Easton C || 20
  10. 8 || Easton C || 21
  11. 9 || Easton C || 22
  12. 10 || Edwards J Ms || 20
  13. 11 || Edwards J Ms || 21
  14. 12 || Edwards J Ms || 22


As you can see in the Description Field “Earl G” appears 3 times and it can be more than that. Now I want to append Cycles [cycle] strings to the corresponding description, I want to run a query so that this table may look like this

  1. ID || DESCR || CYCLE
  2. ==============================================
  3. 1 ||Earl G || 20 , 21, 22
  4. 4 ||Davidson I Dr || 20 , 21, 22
  5. 7 ||Easton C || 20 , 21, 22
  6. 10 ||Edwards J Ms || 20 , 21, 22
How can I achieve this in SQL


Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 196
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: SQL Question

 
0
  #2
May 7th, 2009
try this

  1. SELECT ( SELECT f.cycle as col1
  2. FROM final as f
  3. for xml raw, elements, type).query('for $col1 in (row/col1) return concat($col1, " ")')

I did not test this.

Regards.
Last edited by peter_budo; May 7th, 2009 at 3:29 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 158
Reputation: vuyiswamb is an unknown quantity at this point 
Solved Threads: 5
vuyiswamb's Avatar
vuyiswamb vuyiswamb is offline Offline
Junior Poster

Re: SQL Question

 
1
  #3
May 7th, 2009
i have Resolved the Problem. Thank you. i did the Following

i created a UDF like this
  1. CREATE FUNCTION GetCycle (@Descr Varchar(50))
  2. RETURNS Varchar(500)
  3. AS
  4. BEGIN
  5. Declare @RetStr AS varchar(500),@Cycle Int --<-- Assuming Cycle field is of Type Integer
  6.  
  7. Declare TmpCur Cursor FOR SELECT CyCle FROM YourTblName WHERE Descr=@Descr
  8. Open TmpCur
  9. SET @RetStr=''
  10. Fetch Next FROM TmpCur INTO @Cycle
  11. While @@Fetch_status=0
  12. Begin
  13. SET @RetStr = @RetStr + Case when @RetStr='' then '' else ' ' End + Cast(@Cycle AS varchar)
  14. Fetch Next FROM TmpCur INTO @Cycle
  15. End
  16. Close TmpCur
  17. Deallocate TmpCur
  18. RETURN (@RetStr)
  19. END

and Called the Function like this
  1. SELECT Descr,.dbo.GetCycle(Descr) AS Cycle FROM YourTableName WHERE Descr
  2. IN ( SELECT DISTINCT Descr FROM YourTableName)

Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum


Views: 666 | Replies: 2
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC