
March 26th, 2013, 05:23 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 37 m 22 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by PosiJoel Hey guys,
New to the site, new to writing SQL queries and have no background in this type of thing.
But I've been googling my way to victory for the most part.
I'm struggling on probably a very simple problem.
I'd like to Sum the contents of a column, if a different value in the row is the same as in another... That was worded horribly - how about an illustration:
Table
Unit ID---------Value
-------------------------
10000-----------100
10000-----------100
10000-----------100
10010-----------300
10010-----------150
10010-----------50
10020-----------1000
10030-----------999
10030-----------1001
10040-----------25
10050-----------26
So basically I'm looking for an output when I run the script that shows
Unit ID------Value
10000------300
10010------500
10020------1000
10030------2000
10040------25
10050------26
I don't really understand the SQL syntax very well.
I've been trying stuff like
SELECT DISTINCT (Unit ID), SUM(Value) FROM Table;
And various other simplistic queries, but at this point I'm willing to admit that I haven't a clue what I'm doing.
Any help would be appreciated,
Thanks! |
Hi.. Your query is 50% correct since it performs sum operation on "value" field. But still u haven't achieved what u want i.e., the condition based on which it shld be summed.u have to perform sum() only when ur unit ID field is same.
So query will be
select unit ID,sum(value) from table group by unit ID.
Hope it helps.
|