Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Query, distinct and text concat

Author  Topic 

foutuguy
Starting Member

3 Posts

Posted - 2008-01-30 : 05:56:26
[code]Hi,

I have the following table called "tests" :
id WeekNbr name test hours
--------------------------------------------------------------------------
1 2007/26 John "testA" 5
2 2007/26 John "testB" 6
3 2007/26 David "testA" 3
4 2007/28 David "testC" 2
5 2007/30 Victor "testD" 1

I want to write a query so that I have as a result one row per person and per week, as followed

WeekNbr name test hours
--------------------------------------------------------------------------------
2007/26 John "testA, testB" 11
2007/26 David "testA" 3
2007/28 David "testC" 2
2007/30 Victor "testD" 1

This means that I need to concatenate the values of the test column if in the same week and same person.
For now, I have only managed to do the job without the test concatenation the following way:

SELECT DISTINCT WeekNbr, name, SUM(hours) as [Total of hours]
FROM tests
GROUP BY WeekNbr, name

and I get the following:
WeekNbr name hours
-------------------------------------------------------
2007/26 John 11
2007/26 David 3
2007/28 David 2
2007/30 Victor 1


Anyone could help me please?

Thanks so much in advance

Pierrot[/code]

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-30 : 06:59:09
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -