Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old February 16th, 2005, 08:08 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
Displaying multiple results in one row

Here's what my table looks like

(1st Table)
ID | Name
-- |------
1 | violato
2 | Steve

(2nd Table)
ID | Hobby
---|------
1 | Basket
1 | Pool
1 | Soccer
2 | Tennis
2 | Golf

And I want it to be displayed as:
ID | Name | Hobby
---|------|--------------------
1 |Violato|Basket, Pool, Soccer
---|------|--------------------
2 |Steve |Tennis, Golf
-------------------------------

So basically put all my hobbies in one column separated by , instead of one row on every hobby and duplicate all my name over and over again.

I DO understand this can be done with MySQL by using GROUP_CONCAT() BUT i'm using MS SQL and I've been trying couple hours to fix it. This is an ASP.NET application is built on VB.NET using dataset and datatable if that matters. Any help will be appreciated. Thanks.

Reply With Quote
  #2  
Old February 16th, 2005, 09:44 PM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
Thumbs up

Quote:
use stored procedure

create proc sp_getHobbyInOneRow as

declare @temp table(id int, name char(30), hobby char(50))
declare @result table(id int, name char(30), xHobby char(200))

insert into @temp
select a.id, a.name, b.hobby
from table1 a left join table2 b on a.id = b.id

/*
the result : (@temp)
===============
id, name, hobby
===============
1 | violato | Basket
1 | violato | Pool
1 | violato | Soccer
2 | Steve | Tennis
2 | Steve | Golf
*/
declare @strid int, @prevId int
declare @strname char(30), @prevName char(30)
declare @strhobby char(50), @prevHobby char(50)
declare @dummy char(200)

declare toncursor cursor for select * from @temp order by id
open toncursor
fetch next from toncursor into @strid, @strname, @strHobby
set @prevId = @strid
set @prevName = @strname
set @prevhobby= @strhobby

@dummy = ''
while @@fetch_status = 0
begin
if @prevId = @strid
begin
@dummy = @dummy + @strHobby + ','
end
else
begin
insert into @result
select @previd, @prevName, @dummy

set @dummy = ''
end

set @previd = @strid
set @prevName = @strname
set @prevHobby = @strHobby
fetch next from toncursor into @strid, @strname, @strHobby
end


close toncursor
deallocate toncursor

select * from @result

go


may be this solution can help you.


Regards,


Tonny Soeroso

Reply With Quote
  #3  
Old February 17th, 2005, 03:17 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
Thank you for your help, but is there any easy way to do it like in SQL query or in VB.NET without using stored procedure, because the application I develop is for in-house tools and i don't want to mess around with the database nor editing anything.

I believe there are another way to do it. Thanks.

Reply With Quote
  #4  
Old February 17th, 2005, 03:45 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
You would have to get the list of names first.
Then for each name get the list of hobbies and concatenate them.

Reply With Quote
  #5  
Old February 17th, 2005, 04:19 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
and actually the actual table is much more complicated than that, here's the actual table looks like:

Expert IDCustomer IDProfession TitleRateService DescriptionCategoriesKeywords1227Computer Tech0.5000Will train you to maintain your own computers at home.Computer AdviceComputer Train Network1429whack job1.0000howdy do?Insurancecooking1429whack job1.0000howdy do?Drugs & Alchoholcooking1429whack job1.0000howdy do?Mathcooking1530sir15.0000peter piper picked a peck of pickled peppersLife Coacha peck of pickled peppers1530sir15.0000peter piper picked a peck of pickled peppersOthera peck of pickled peppers1530sir15.0000peter piper picked a peck of pickled peppersSpeecha peck of pickled peppers

and here's the SQL query I use:
AQry = "SELECT Expert.ExpertID, Expert.CustomerID, Expert.ProfessionTitle, Expert.RatePerMin, " & _
"Expert.Service, Expert.keywords, vm_categories.CategoryName FROM Expert LEFT JOIN " & _
"vm_providers_to_categories ON Expert.ExpertID = vm_providers_to_categories.ExpertID " & _
"LEFT JOIN vm_categories ON vm_providers_to_categories.CategoryID = vm_categories.CategoryID ORDER BY Expert.ExpertID"

How do I make the whack job only have 1 row and the category separated by , (comma) like Insurance, Drug & Alcohol, Math. The stored procedure seems to complicated to accomplish this. Any help will be appreciated. Thanks

Reply With Quote
  #6  
Old February 17th, 2005, 04:38 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
hmm the table doesn't looks rite, here's what it looks like:

http://img187.exs.cx/my.php?loc=img187&image=expert8kt.jpg

Reply With Quote
  #7  
Old February 17th, 2005, 05:49 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
You can't return a comma separated list like that from a query.
You would have to do as I mentioned earlier, which is get the persons name, then get the hobbies associated with that person, then just concatenate the hobbies together, separated by commas.

Reply With Quote
  #8  
Old February 17th, 2005, 06:03 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
any idea on how to do that? right now I only use datagrid and in my code, I use: sqldataadapter, dataset, fill, datatable, and databind. Here's my code looks like:

Code:
  
Dim Acon As New SqlConnection(QryStr)
	Dim AQry As String
	AQry = "SELECT Expert.ExpertID, Expert.CustomerID, Expert.ProfessionTitle, Expert.RatePerMin, " & _
	  "Expert.Service, Expert.keywords, vm_categories.CategoryName FROM Expert LEFT JOIN " & _
	  "vm_providers_to_categories ON Expert.ExpertID = vm_providers_to_categories.ExpertID " & _
	  "LEFT JOIN vm_categories ON vm_providers_to_categories.CategoryID = vm_categories.CategoryID ORDER BY Expert.ExpertID"
	
	Acon.Open()
	
	Dim ADScmd As New SqlDataAdapter(AQry, Acon)
	
	Dim ADS As New DataSet()
	
	ADScmd.Fill(ADS, "SearchResult")
   
	Dim AgentTable As DataTable = ADS.Tables.Item("SearchResult")
	
	'Set a DataGrid's DataSource to the DataView, then Bind
	AgentGrid.DataSource = AgentTable 
	AgentGrid.DataBind()
	
	'Close Agent SQL Connection
	Acon.Close()

Reply With Quote
  #9  
Old February 22nd, 2005, 03:12 PM
violato violato is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 36 violato User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 52 m 17 sec
Reputation Power: 4
anyone can help me on this one? Thanks.

Reply With Quote
  #10  
Old March 14th, 2005, 10:33 AM
asmoran asmoran is offline
ASPFree Know-It-All
ASP Free Novice (500 - 999 posts)
 
Join Date: Aug 2004
Posts: 930 asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level)asmoran User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 15 h 30 m 7 sec
Reputation Power: 325
Try something like this:

Code:
 
Dim conn1 as SqlConnection = New SqlConnection(QryStr)
Dim conn2 as SqlConnection = New SqlConnection(QryStr)
Dim cmd1 as SqlCommand = new SqlCommand (<Select all persons>)
Dim cmd2 as SqlCommand 
Dim reader1, reader2 as SqlDataReader
 
conn1.open
reader1=cmd.ExecuteReader
if reader1.HasRows then
	 while reader1.Read 
		  cmd2 = new SqlCommand("Select Hobby From Table2 Where ID = " & reader1("ID"))
		  conn2.Open
		  reader2 = cmd2.ExecuteReader
		  if reader2.HasRows then
			   Response.Write(reader1("Name") & " - ")
			   while reader2.read
					Response.Write(reader2("Hobby") & ", ")
			   end while <or loop, I forget the exact syntax>
		  else 
			   Response.Write("No hobbies for this person")
		  end if
		  reader2.close
		  conn2.close
	 end while <again, I forget the exact syntax>
else
	 Response.Write ("No people found")
end if
reader.close
conn.close


----------------------------
Please keep in mind I just typed all this straight into the reply box, so there may be simple syntax errors.

Last edited by asmoran : March 14th, 2005 at 10:34 AM. Reason: forgot code tags

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Displaying multiple results in one row


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT