|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
Quote:
may be this solution can help you. Regards, Tonny Soeroso |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
You would have to get the list of names first.
Then for each name get the list of hobbies and concatenate them. |
|
#5
|
|||
|
|||
|
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 ![]() |
|
#6
|
|||
|
|||
|
hmm the table doesn't looks rite, here's what it looks like:
http://img187.exs.cx/my.php?loc=img187&image=expert8kt.jpg |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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()
|
|
#9
|
|||
|
|||
|
anyone can help me on this one? Thanks.
|
|
#10
|
|||
|
|||
|
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 |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Displaying multiple results in one row |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|