Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table value is
Group    Amount
A Group I  1500
A Group II 2000
B Group I  2500
B Group II 500


Expected Output

Group     Amount
A Group I    3500
B Group I    3000


How to do this?

What I have tried:

Im using

SQL
Select Group,Sum(Amount)
From tblAmount
Group by Group
Posted
Updated 27-Mar-19 2:02am
v4

Since you're aggregating (summing) the Amount, you shouldn't include it in the GROUP BY clause.

SQL
SELECT   [Group], 
         SUM([Amount]) AS SumAmount 
FROM     [dbo].[tblAmount] 
WHERE [Group] LIKE '%Group I'
GROUP BY [Group]


BTW, there is no WHERE class involved based on your expected output
 
Share this answer
 
v2
Comments
[no name] 27-Mar-19 7:32am    
i tried it sir but its not working
#realJSOP 27-Mar-19 7:42am    
"Is not working" doesn't tell me a damn thing.
#realJSOP 27-Mar-19 7:44am    
I changed my answer. try it now.
[no name] 27-Mar-19 7:46am    
ok thank you
#realJSOP 27-Mar-19 7:45am    
And for the record, you should NEVER name your tables, views, columns and other stuff using SQL reserved words. "Group" is NOT a good name.
What you're trying to achieve is called "group by part of string"

SELECT   [Group], SUM([Amount]) AS SumAmount 
FROM     [dbo].[tblAmount] 
GROUP BY LEFT([Group], CHARINDEX('Group', [Group]+5)


Note, that below statement:
LEFT([Group], CHARINDEX('Group', [Group]+5)

searches for string "Group" in a [Group] column and returns: {"A Group", "B Group"}
 
Share this answer
 
Comments
[no name] 27-Mar-19 8:47am    
It's Working Fine.Thank you sir
Maciej Los 27-Mar-19 9:16am    
You're very welcome.

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