Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Connect to SQL 2008 r2 server over the Internet 4 23
SQL Syntax 6 32
SQL 2012 Instance Problem 3 62
T-SQL: problem comparing datetime 4 51
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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