Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SUM REPEATED VALUE

Author  Topic 

belkin_99
Starting Member

27 Posts

Posted - 2009-02-22 : 21:58:26
I KNOW THIS QUESTION MAY BE SIMPLE, AND REPEATED.
I HAVE A TABLE

UNIT PRICE
ER 50
TY 44
QW 12
ER 23
ER 100
TY 230
AND I WANT TO CALCULATE THE SUM (PRICE) FOR EACH UNIT WITHOUT REPEAT THE UNIT

UNIT PRICE
ER 173
TY 274
QW 12
TY 230
SELECT UNIT, sum(PRICE)
FROM (SELECT DISTINCT UNIT,FROM TABLE1)
GROUP BY UNIT
BUT IT DID NOT WORK

THANK YOU

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-22 : 22:57:08
[code]SELECT UNIT,SUM(PRICE)
FROM TABLE
GROUP BY UNIT
ORDER BY UNIT[/code]
Go to Top of Page

belkin_99
Starting Member

27 Posts

Posted - 2009-02-23 : 07:33:07
THANK YOU SODEEP FOR THE REPLY, BUT CAN YOU TELL ME WHY WHEN I RUN THE SAME LINE I GOT THE FOLLOWING ERROR:

SELECT UNIT,UNIT_DATE, SUM(PRICE)AS COST
FROM TABLE
GROUP BY ORDER_NUMB
ORDER BY ORDER_NUMB

Msg 8120, Level 16, State 1, Line 86
Column 'TABLE.UNIT_DATE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-23 : 07:40:00
quote:
Originally posted by belkin_99

THANK YOU SODEEP FOR THE REPLY, BUT CAN YOU TELL ME WHY WHEN I RUN THE SAME LINE I GOT THE FOLLOWING ERROR:

SELECT UNIT,UNIT_DATE, SUM(PRICE)AS COST
FROM TABLE
GROUP BY ORDER_NUMB UNIT,UNIT_DATE
ORDER BY UNIT,UNIT_DATE

Msg 8120, Level 16, State 1, Line 86
Column 'TABLE.UNIT_DATE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Go to Top of Page

belkin_99
Starting Member

27 Posts

Posted - 2009-02-23 : 19:20:33
Mr. SODEEP Thank you
Yes it is working correctly .

Go to Top of Page
   

- Advertisement -