Solved

Help with SQL

Posted on 2013-12-19
4
258 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
[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
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

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
query optimization 6 29
Stored Procedure needs owner to execute 5 48
How to add an Index to a date/time field? 15 39
when sa SQL Login becomes locked 6 22
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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