Solved

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

Posted on 2012-03-26
2
400 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
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

831 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