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 > Database Server Software > MySQL > Using Distinct and SUM in query problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-09, 13:28
smitty912 smitty912 is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Using Distinct and SUM in query problem

Hi,

I tried the following query and it doesn't work. The 'created' field is in datetime.

select distinct month(created) as created,
sum(cost) as total_cost
from finances
group by created;

I want to display the total cost incurred for each unique month. A simple request yet its confusing to make it work.

Please help

Regards,
wayne
Reply With Quote
  #2 (permalink)  
Old 06-11-09, 13:45
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
Try this instead:
Code:
select month(created) as created,
sum(cost) as total_cost
from finances
group by month(created);
Regards,

Ax
Reply With Quote
  #3 (permalink)  
Old 06-11-09, 14:03
smitty912 smitty912 is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Quote:
Originally Posted by Ax238
Try this instead:
Code:
select month(created) as created,
sum(cost) as total_cost
from finances
group by month(created);
Regards,

Ax
Cheers Ax , thank you so much! It works now, i just spend a whole day figuring it out..damn i'm a noob
Reply With Quote
  #4 (permalink)  
Old 06-11-09, 14:37
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
Great to hear! No worries, easy mistake.
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