Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having difficulties in getting first record while doing group by clause.
Below is the sample table which i would like to take as an example.

C#
Name	BankAccountNumber	Amount
Test1	12321	                 2000
Test1	1222	                 2000
Test2	2323232	                 3000
Test2	12312323	         1000


Result Expected:
C#
Name	BankAccountNumber	Amount
Test1	12321	                4000
Test2	2323232	                4000


What I have tried:

We can get the result in excel with the below way,

select name, FIRST(BankAccountNumber), SUM(Amount) 
from Customers
group by name, Amount


can somebody help me to get similar results in sql.
Posted
Updated 25-Mar-18 18:47pm

The problem is that you don't understand what GROUP BY does: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]
It is possible to do, but you are going the wrong way around it. Have a look here: SQL to select the first record in a group[^]
 
Share this answer
 
SQL
SELECT   Name,
         BankAccountNumbe,
         SUM(Amount) AS AMOUNT
 from Customers
    WHERE BankAccountNumbe in(12321,2323232)  GROUP BY Name,BankAccountNumbe
 
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