[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with SQL

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

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 31

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

834 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