?
Solved

Help with SQL

Posted on 2013-12-19
4
Medium Priority
?
267 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 66

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 1000 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 1000 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

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

In this article I will describe the Backup & Restore 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

765 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