Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Query syntax

Posted on 2016-09-14
7
Medium Priority
?
53 Views
Last Modified: 2016-09-14
Hi,

I have two tables,

Table A has users' info, name, address.etc
Table B has notes that created by users in Table A, each user can create multiple notes.

Table A
userid
username
firstname
lastname
address

Table B
noteid
userid
note
date

I need to create an Excel report that contain all users and their notes. Since each user can create multiple notes, I want to combine individual user's notes into one cell.  Is there a way to do this in SQL stored procedure?  thanks
0
Comment
Question by:mcrmg
7 Comments
 
LVL 7

Expert Comment

by:D Patel
ID: 41797811
Use This syntax:

select
  tableA.userid as `Id`,
  tableA.username as `User`,
  GROUP_CONCAT(tableB.note) as `Notes`
from
  tableA,
  tableB
where
  tableA.userid=tableB.userid
group by
  tableA.userid
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41797829
whuch databas edo you use? MS SQL? mySQL?
0
 

Author Comment

by:mcrmg
ID: 41797834
thanks for the quick reply, I am getting

'GROUP_CONCAT' is not a recognized built-in function name.


I have SQL 2008, too old?  thanks
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 35

Expert Comment

by:YZlat
ID: 41797847
GROUP_CONCAT is for mySQL
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1800 total points
ID: 41797857
        SELECT DISTINCT a.userid, notes = STUFF(( SELECT  ',' + note
	FROM TableB as b
	WHERE b.userid=a.userid	
	ORDER BY note
	FOR XML	PATH('')), 1, 1, '')
FROM TableA as a

Open in new window

0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 200 total points
ID: 41797892
YZlat's solution of XML PATH is correct.   For a demo of the XML path method of making comma-separated values as a single value check out T-SQL:  Normalized data to a single comma delineated string and back
0
 

Author Closing Comment

by:mcrmg
ID: 41797945
thank you very much. Learned something new today
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 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