Format a result from several rowns into one

Hi,

I have this query looking like this:
SELECT * FROM dbo.EmployeeWorkTimeSchedule AS ewts WHERE EmployeeID = 2514 AND MONTH(ewts.fromDate) = 12

Open in new window


And giving the following result:
Result
How can I make a query so that the rusult will be formatted like this?
2018-12-01   Pass 1: 8:00-12:00 | Lunch: 12:00 - 13:00 | Pass 2: 13:00-17:00
2018-12-02   Pass 1: 8:00-12:00 | Lunch: 12:00 - 13:00 | Pass 2: 13:00-17:00

Thanks for help!

Peter
Peter NordbergIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
First of all: Use table alias names consistently.
Then: I don't understand your model. Is it relational or dimensional?

This should be done in the front-end. Otherwise it is a string concat, using STRING_AGG() (SQL Server 2017+) or FOR XML. E.g. FOR XML:

SELECT   CAST(O.fromDate AS DATE) ,
         STUFF((   SELECT   '| ' + I.descr + ': ' + FORMAT(I.fromDate, 'hh:nn') + '-' + FORMAT(I.toDate, 'hh:nn')
                   FROM     dbo.EmployeeWorkTimeSchedule I
                   WHERE    I.EmployeeID = O.EmployeeID
                            AND CAST(I.fromDate AS DATE) = CAST(O.fromDate AS DATE)
                   ORDER BY I.fromDate
                   FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,
               1 ,
               2 ,
               '') AS Desc_Agg
FROM     dbo.EmployeeWorkTimeSchedule O
WHERE    O.EmployeeID = 2514
         AND MONTH(O.fromDate) = 12
GROUP BY CAST(O.fromDate AS DATE);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter NordbergIT ManagerAuthor Commented:
Thank you! It worked as expected!

Peter
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
T-SQL

From novice to tech pro — start learning today.