Solved

Query syntax

Posted on 2016-09-14
7
41 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 65

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

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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