Jump to content

Recommended Posts

I have these two queries, I need to make this one query where the amount on each row adds up to a number.

 

mysql>  select SUM(amount), last_day(from_unixtime(the_date)) as d from
purchases Where the_date>  1285891200 and member_id = 110719 group by d;
+-------------+------------+
| SUM(amount) | d          |
+-------------+------------+
|   2059.9500 | 2010-10-31 |
|      0.0000 | 2010-11-30 |
|      0.0000 | 2010-12-31 |
|      0.0000 | 2011-01-31 |
+-------------+------------+
4 rows in set (0.00 sec)

mysql>  select SUM(amount), last_day(from_unixtime(transfer_date)) as d
from cash_out Where transfer_date>  1285891200 and user_id = 110719 AND
descr LIKE '%purchase%' group by d;
+-------------+------------+
| SUM(amount) | d          |
+-------------+------------+
|        6.31 | 2010-10-31 |
|       83.82 | 2010-11-30 |
|      116.88 | 2010-12-31 |
|       12.51 | 2011-01-31 |
+-------------+------------+
4 rows in set (0.00 sec)

 

I currently have this, but I am getting the wrong numbers:

 

select sum(distinct c.amount) + sum(distinct p.amount) as amount, last_day(from_unixtime(the_date)) as d  
from purchases p join cash_out c on(member_id = user_id) 
where the_date > 1285891200 and transfer_date > 1285891200 and member_id = 110719 
group by d;

+-----------+------------+
| amount    | d          |
+-----------+------------+
| 2205.7300 | 2010-10-31 |
|  145.7800 | 2010-11-30 |
|  145.7800 | 2010-12-31 |
|  145.7800 | 2011-01-31 |
+-----------+------------+
4 rows in set (0.01 sec)

 

I am looking for this result:

+-----------+------------+
| amount    | d          |
+-----------+------------+
| 2066.2600 | 2010-10-31 |
|     83.82 | 2010-11-30 |
|    116.88 | 2010-12-31 |
|     12.51 | 2011-01-31 |
+-----------+------------+

 

What can I do?

Link to comment
https://forums.phpfreaks.com/topic/223697-combine-2-queries/
Share on other sites

SUM() is a function that calculates the sum for the COLUMN.  You can do fielda + fieldb to give you a result of fieldc on rowX.  You need to select the data you need where a.the_date = b.transfer_date

 

Essentially, something like (untested):

SELECT (a.amount + b.amount) FROM a,b WHERE a.the_date = b.transfer_date

 

Link to comment
https://forums.phpfreaks.com/topic/223697-combine-2-queries/#findComment-1156386
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.