Solved

Query syntax

Posted on 2016-09-14
7
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
LVL 35

Expert Comment

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

Accepted Solution

by:
YZlat earned 450 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 50 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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