Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,
How to Concatenate same columns different values in group by clause in sql 2008R2?

I want to select count of shopkeeper Registration and their amount limit against a dealer kindly guide me.

What I have tried:

SQL
select COUNT( ShopkeeperID)TotalReg,ShopkeeperID,DealerID  from InsuranceMaster
where ShopkeeperID=4
group by ShopkeeperID,DealerID

its showing result
C#
TotalReg	ShopkeeperID	DealerID	AmountLimit
1	             4	           21	            1
1	             4	           21	            6

My desired result is
C#
TotalReg	ShopkeeperID	DealerID      AmountLimit
   2                4	          21            1,6
Posted
Updated 29-Nov-16 22:59pm
v4
Comments
Mehdi Gholam 30-Nov-16 1:35am    
Why?
Noman Suleman 30-Nov-16 1:38am    
its user's requirement

1 solution

C#
Create TABLE #x( ShopkeeperID INT, DealerID INT, amtlimit int);

INSERT #X(ShopkeeperID, DealerID, amtlimit) VALUES
(1,1,20), (1,1,30), (2,1,20),  (2,2,20),
(2,2,20), (3,1,20), (3,1,20),  (3,2,60),
(3,2,60);

############################################


 SELECT COUNT(t.ShopkeeperID) as tcount, t.ShopkeeperID, t.DealerID
       ,STUFF((SELECT ', ' + CAST(t1.amtlimit AS VARCHAR(10)) [text()]
         FROM #x t1
         WHERE t1.DealerID = t.DealerID  and t1.ShopkeeperID = 3 
FOR XML PATH(''), TYPE)
        .value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM #x t
where t.ShopkeeperID = 3
GROUP BY t.ShopkeeperID, t.DealerID
 
Share this answer
 
Comments
$*Developer - Vaibhav*$ 30-Nov-16 5:00am    
out put like this

tcount ShopkeeperID DealerID List_Output
2 3 1 20, 20
2 3 2 60, 60
Noman Suleman 30-Nov-16 5:27am    
Thanks its perfect :)
Noman Suleman 30-Nov-16 8:19am    
hi, when i trying to get column from other table with inner join its not getting desired result kindly guide and check my query

SELECT COUNT( t.ShopkeeperID) as tcount,t.DealerID, t.ShopkeeperID
,STUFF((SELECT ', ' + CAST(Config_LimitInsurance.Name AS VARCHAR(10)) [text()]
FROM InsuranceMaster t1
WHERE t1.ShopkeeperID = t.ShopkeeperID and t1.DealerID = 21
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM InsuranceMaster t INNER JOIN

Config_LimitInsurance ON t.AmountLimit = Config_LimitInsurance.ID
WHERE (t.DealerID = 21)
GROUP BY t.DealerID, t.ShopkeeperID, Config_LimitInsurance.Name


result is

tcount DealerID ShopkeeperID List_Output
2 21 4 50k, 50k
1 21 10 50k
1 21 11854 50k
$*Developer - Vaibhav*$ 1-Dec-16 5:41am    
please share both table structure and value?
Noman Suleman 1-Dec-16 7:09am    
thanks. i have done it by call query in views now its work fine showing my desire result with the help of your query thanks again

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