If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SUM for DISTINCT rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-09, 12:46
brijniersce brijniersce is offline
Registered User
 
Join Date: Jul 2009
Posts: 2
SUM for DISTINCT rows

Hi,

I've been using SQL for a long time, but only very occasionally. So I get what I want with basic SELECT queries and then do the more complicated stuff in code.

For my current project I try to do something that I guess is trivial in SQL, but I can't get it to work

I'm using SQLLite, table has two colums
Amount | AcctNo
------------------
10 | 9113.12
15 | 9113.12
30 | 9114.10

I want a SUM for the DISTINCT AcctNO's. So the result should be
9113.12 | 25
9114.10 | 30

I've tried things like:
SELECT SUM(amount) FROM tbl WHERE acctno IN (SELECT DISTINCT AcctNo FROM tbl)

But I'm never getting anything more then 1 line

All suggestions welcome!

Bas
Reply With Quote
  #2 (permalink)  
Old 07-08-09, 13:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
SELECT acctno, SUM(amount) FROM tbl GROUP BY acctno

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-08-09, 13:16
brijniersce brijniersce is offline
Registered User
 
Join Date: Jul 2009
Posts: 2
That euhmm query is incredibly complex I would have never been able to come up with that

TX!
Bas

P.s What book would you recommend to get past the basic SELECT query level?
Reply With Quote
  #4 (permalink)  
Old 07-08-09, 13:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Quote:
Originally Posted by brijniersce
P.s What book would you recommend to get past the basic SELECT query level?
this is a trick question, right?

based on what i think you know about GROUP BY, i would say my book

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On