Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

return join table row values as comma-delim list in one column

Posted on 2012-03-26
2
Medium Priority
?
410 Views
Last Modified: 2012-04-02
I have the below query which returns a dataset such as this:

1      2      2      10      2012-03-26 14:12:10.000
1      2      2      40      2012-03-26 14:12:10.000
1      2      2      60      2012-03-26 14:12:10.000

This is because the m table has one row that matches @clientmedicationid, and the b table contains three rows that relate to the m table record foreign key. This is correct.

I want to instead return one row representing the m table, and show the b table 10,40 and 60 values as a comma delimited list in one column of that row. There could be up to 9 possible values/rows in b table for each unique row in m.

Thanks.


SELECT m.new_currentmedication, m.new_takingasindicated, m.new_doctordiscontinued, b.new_reasonfornoncompliance, m.createdon
FROM new_medicationcompliance m WITH (NOLOCK)
JOIN new_medcompliancereasonformednoncompliance b WITH (NOLOCK)
ON b.new_medicationcompliancereviewid = m.new_medicationcomplianceid
WHERE m.new_clientmedicationid = @clientmedicationid
AND m.deletionstatecode <> 2
AND m.statecode = 0

Open in new window

0
Comment
Question by:sailing_12
[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
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 700 total points
ID: 37766931
Create a function to collate the values:
CREATE FUNCTION [dbo].ItemsForKey
(
    @clientmedicationid INT,
    @new_currentmedication INT,
    @new_takingasindicated INT,
    @new_doctordiscontinued INT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
	DECLARE @ItemList VARCHAR(200)
	SELECT @ItemList = coalesce(@ItemList + ',', '') + cast(b.new_reasonfornoncompliance AS VARCHAR(10))
	FROM new_medicationcompliance m WITH (NOLOCK)
		JOIN new_medcompliancereasonformednoncompliance b WITH (NOLOCK) ON b.new_medicationcompliancereviewid = m.new_medicationcomplianceid
	WHERE m.new_clientmedicationid = @clientmedicationid
		AND m.deletionstatecode <> 2
		AND m.statecode = 0
		AND m.new_currentmedication = @new_currentmedication
		AND m.new_takingasindicated = @new_takingasindicated
		AND m.new_doctordiscontinued = @new_doctordiscontinued
	RETURN (@ItemList)
END  

Open in new window


Then call it in your query:
SELECT m.new_currentmedication,
	   m.new_takingasindicated,
	   m.new_doctordiscontinued,
	   [dbo].ItemsForKey(@clientmedicationid, m.new_currentmedication, m.new_takingasindicated, m.new_doctordiscontinued) AS ItemList,
	   m.createdon
FROM new_medicationcompliance m WITH (NOLOCK)
	JOIN new_medcompliancereasonformednoncompliance b WITH (NOLOCK) ON b.new_medicationcompliancereviewid = m.new_medicationcomplianceid
WHERE m.new_clientmedicationid = @clientmedicationid
	AND m.deletionstatecode <> 2
	AND m.statecode = 0

Open in new window

0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 700 total points
ID: 37767972
SELECT m.new_currentmedication, m.new_takingasindicated, m.new_doctordiscontinued,
    (SELECT TOP 100 PERCENT CAST(b.new_reasonfornoncompliance AS varchar(10)) +
         CASE WHEN b.new_reasonfornoncompliance < (SELECT MAX(bMax.new_reasonfornoncompliance)
             FROM new_medcompliancereasonformednoncompliance bMax
             WHERE bMax.new_medicationcompliancereviewid = b.new_medicationcompliancereviewid)
         THEN ', ' ELSE '' END
     FROM new_medcompliancereasonformednoncompliance b WITH (NOLOCK)
     WHERE b.new_medicationcompliancereviewid = m.new_medicationcomplianceid
     ORDER BY b.new_reasonfornoncompliance
     FOR XML PATH('')
    ) AS ReasonsForNoncompliance,
    m.createdon
FROM new_medicationcompliance m WITH (NOLOCK)
WHERE m.new_clientmedicationid = @clientmedicationid
AND m.deletionstatecode <> 2
AND m.statecode = 0
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

610 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