Solved

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

Posted on 2012-03-26
2
396 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
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 175 total points
Comment Utility
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 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 175 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now