SQL Basics

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsDatabasesSQL Basics

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old January 23rd, 2013, 06:04 PM
PosiJoel PosiJoel is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 PosiJoel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 16 sec
Reputation Power: 0
Sum(query).I'm sure this is simple to resolve

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!

Reply With Quote
  #2  
Old February 24th, 2013, 08:32 PM
SouthAustin SouthAustin is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 SouthAustin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 47 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!


select UnitID, SUM(VALUE) from table group by UnitID

will do it. Google SQL Group by to find more examples. HTH

Reply With Quote
  #3  
Old March 26th, 2013, 05:23 AM
gsindhu gsindhu is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 gsindhu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Tutorialized ForumsDatabasesSQL Basics > Sum(query).I'm sure this is simple to resolve


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap