Friday, March 23, 2012

Query for SUM

Hi, i have a simple table with itemcode and quantity

Code Qty

--

A 2

B 3

C 5

A 3

How do i query the table to return me the total quantity by itemcode (as below)?

Code Qty

--

A 5

B 3

C 5

use aggregate function sum () and grup by

Select code,sum(qty) as TotalQty from YourTableName group by Code

Madhu

|||

use the following code,

Code Snippet

Create Table #data (

[Code] Varchar(100) ,

[Qty] int

);

Insert Into #data Values('A','2');

Insert Into #data Values('B','3');

Insert Into #data Values('C','5');

Insert Into #data Values('A','3');

Select

Code

,Sum(Qty)

From

#Data

Group By

Code

No comments:

Post a Comment