Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i have a sql table thats look like:-

SQL
NAME  | SALARY  | MONTH

SQL
Ashish| 2500| Feb

SQL
Ashish| 3000| Mar


i need a result of this table like:-

SQL
NAME  | SALARY  | MONTH

SQL
Ashish| 5500| Feb,Mar


What I have tried:

i am trying query but its not working for me:

SELECT [Name],Sum(Salary)
, STUFF((SELECT ', ' + A.Month FROM Table1 A
Where A.Name=B.Table1 FOR XML PATH('')),1,1,'') As [Month]
From Table1 B
Group By [Name]
Posted
Updated 6-Oct-16 21:41pm
Comments
Jawad Ahmed Tanoli 7-Oct-16 3:48am    
what is your Query result? i think result is same as you required

1 solution

try

SQL
declare @table table 
(
NAME varchar(50),
 SALARY int,
MONTH varchar(250)
)
insert into @table(NAME,SALARY,MONTH)values( 'aa',2000,'Jan')
insert into @table(NAME,SALARY,MONTH)values( 'aa',3000,'Feb')
insert into @table(NAME,SALARY,MONTH)values( 'bb',1000,'Mar')
insert into @table(NAME,SALARY,MONTH)values( 'bb',3000,'Apr')
insert into @table(NAME,SALARY,MONTH)values( 'bb',2500,'Jun')
 

SELECT Name, Salary = sum(salary), Month = 
    STUFF((SELECT ', ' + MONTH
           FROM @table b 
           WHERE b.NAME = a.NAME 
          FOR XML PATH('')), 1, 2, '')
FROM @table a
GROUP BY NAME
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900