Solved

Help with SQL

Posted on 2013-12-19
4
248 Views
Last Modified: 2013-12-23
I have a table with "tasks" associated with a "job".  The tasks table has a foreign key that maps to the PK of the job table.   The task table has a description column.   I want a query that pulls back all task descriptions for job into a single concatenated text column, with the literal word "Task" and number that indicates the task order, based on the task date ASC, and the date.  With CrLf between each.   For example:

Task 1, 11/17/2013: The Task aaaaaa description.
Task 2, 11/23/2013: The Task bbbbbb description.
Task 3, 12/4/2013: The Task ccccc description.
Task 4, 12/13/2013: The Task ddddd description.
0
Comment
Question by:HLRosenberger
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39730365
It would help if you can give us mockups of both the source of data, and the desired return set, as I'm not getting it from reading this question.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39730439
Something like this will do it:



SELECT j.column_name1, j.column_name2, --j....,
    REPLACE(SUBSTRING(CAST(
    (SELECT CAST('~' +
         'Task ' + CAST(t.task_number AS varchar(10)) + ' ' + CONVERT(varchar(10), t.task_date, 101) + ': ' + t.task_description
         AS varchar(8000))
     FROM dbo.tasks t
     WHERE t.job_key = j.job_key
     ORDER BY t.task_date
     FOR XML PATH('')
    ) AS varchar(8000)), 2, 8000), '~', CHAR(13) + CHAR(10)) AS Task_List
FROM dbo.jobs j
0
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 250 total points
ID: 39730553
Compare with:
select  'Task ' + B.b +','+ B.x +':'+ B.r from A Inner join B On A.a=B.a;
(No column name)
Task 1,1:b11
Task 2,1:b12
Task 1,1:b21

For tables: A and B
A
a      r      x      n
1      a1      1      1.0
2      a2      1      2.0

B
a      b      r      x      n
1      1      b11      1      1.0
1      2      b12      1      2.0
2      1      b21      1      3.0

You may need to use cast and convert where necessary.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 39736616
thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Detach & Attach 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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

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