Solved

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

Posted on 2012-03-26
2
404 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 175 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 175 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

729 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