Solved

Help with SQL

Posted on 2013-12-19
4
242 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:
ScottPletcher 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now