Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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 Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old March 17th, 2008, 10:59 AM
aaronic aaronic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 3 aaronic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 38 sec
Reputation Power: 0
Send a message via MSN to aaronic
Probably simple SQL

I am decent at simple SQL queries but this one is giving me a hard time, I'm sure someone else will figure this out in 2 seconds flat.

I have a clients table with clientid, then I have a transactions table with co-responding clientid.

I want to do a search for clients who have the most paid. The tricky part is many clients have more than one transaction, so how do I make a query that adds up all of the clients specific paid amounts?

For example, I have done something like this so far:

SELECT DISTINCT clients.clientid, transactions.amountPaid FROM clients, transactions WHERE transactions.amountPaid > $volume AND clients.clientid = transactions.clientid ORDER by transactions.amountPaid LIMIT 1000

This can give me all the clients who have a transaction over a certain amount, then I have this SQL:

SELECT SUM(amountPaid) as totalpaid FROM transactions WHERE clientid = $row[clientid]

Which calculated and puts the total amount in my table I create with the data.

The problem is if I search for clients over 50,000 paid for example, if they have two transactions for 30,000 it will not find them. My current method also doesn't allow me to sort by total amount paid for clients.

Any one have any good ideas/suggestions?

Thanks in advance!
Aaron

Reply With Quote
  #2  
Old March 17th, 2008, 11:16 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
select client, 
         sum(paidAmount) as total
  from clients, 
         transactions 
where clients.clientid = transactions.clientid 
 group by client 
having sum(paidAmount) > $volume
 order by total desc
 limit 1000
Comments on this post
aaronic agrees: Thanks, perfect answer

Reply With Quote
  #3  
Old March 17th, 2008, 11:38 AM
aaronic aaronic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 3 aaronic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 38 sec
Reputation Power: 0
Send a message via MSN to aaronic
Thanks so much, you have just shed a lot of light on how I can make all of my SQL queries more efficient!

Reply With Quote
  #4  
Old March 17th, 2008, 12:48 PM
aaronic aaronic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 3 aaronic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 38 sec
Reputation Power: 0
Send a message via MSN to aaronic
One more quick question...

On every result from the first select query, I run another query to get the name of the salesmen.

SELECT clients.firstName, clients.lastName, clients.country, sales.name FROM clients, sales WHERE clients.clientid = $row[clientid] AND clients.salesid = sales.salesid

I'm sure I could integrate this into the query that was just made above, just not sure how to go about it without screwing it up! Can anyone quickly put it together for me?

Thanks again so much

Aaron

Reply With Quote
  #5  
Old March 17th, 2008, 01:09 PM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,620 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 47 m 19 sec
Reputation Power: 259
Should be something like
Code:
select 
	c.firstName,
	c.lastName,
	c.country, 
	s.name,
        	sum(t.paidAmount) as total
  from 
clients c inner join transactions t 
 on c.clientid = t.clientid 
inner join sales s
 on c.salesid = s.salesid
group by 
	c.firstName,
	c.lastName,
	c.country,
	s.name
having 
sum(t.paidAmount) > $volume
order by 
 total desc
limit 1000
And PLEASE USE A DESCRIPTIVE SUBJECT for your posts

Last edited by pabloj : March 17th, 2008 at 01:14 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Probably simple SQL


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway