Solved

LEFT OUTER JOIN Merge Two Row Fields as One Field

Posted on 2008-06-18
6
1,162 Views
Last Modified: 2012-08-13
I have the following query that connects dattbl_Case with dattbl_Policy

dattbl_Policy sometimes has more then one row. When this occurs I need my query to merge both of the POL_NUM column rows from this table into one POL_MERGED column in one row.

So instead of:
CASE_ID POL_ID
600000   POL_ID A
600000   POL_ID B
I need
CASE_ID POL_ID
600000   POL_ID A
               POL_ID B

Not sure how to go about doing this.
Thanks in advance.

 
SELECT     dattbl_Case.CASE_ID, dattbl_Policy.POL_NUM as POL_MERGED
FROM         dattbl_Case LEFT OUTER JOIN
                      dattbl_Policy ON dattbl_Case.CASE_ID = dattbl_Policy.CASE_ID
WHERE     (dattbl_Case.CASE_STATUS = 2)
ORDER BY dattbl_Case.CASE_ID

Open in new window

0
Comment
Question by:cbish21577
[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
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21816065
what is your database?
this should be done in the client application, that is not really a SQL functionality.
0
 

Author Comment

by:cbish21577
ID: 21816076
SQL 2000
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21816149
you will need a helper function like this:
CREATE FUNCTION dbo.ConcatPolicyNum(@CaseID int)
RETURNS VARCHAR(8000)
AS
BEGIN
  DECLARE @res VARCHAR(8000)
  SELECT @res = COALESCE(@res + ' ', '') + CAST(POL_NUM AS VARCHAR(20))
    FROM dattbl_Policy
   WHERE Case_ID = @caseID
  RETURN @res
END

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21816155
and your query becomes this:
SELECT c.CASE_ID, dbo.ConcatPolicyNum(c.CaseID) as POL_MERGED
  FROM dattbl_Case c
 WHERE c.CASE_STATUS = 2
 ORDER BY c.CASE_ID

Open in new window

0
 

Author Comment

by:cbish21577
ID: 21816170
Thanks angellll, I'll test this out. Much appreciated.


0
 

Author Closing Comment

by:cbish21577
ID: 31468530
Thanks, that worked perfect.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

732 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