469,603 Members | 2,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

Total without and with a condition

How do I get the total without a condition and the total with the
condition at the same time?

This is my mysql table:

id datetimeconfirmed customerid amount
1 2007-08-14 06:19:03 1 12.25
2 2007-08-06 06:19:19 1 15
3 2007-08-02 06:19:41 2 5
4 2007-08-07 06:19:58 2 7.25
5 2007-08-09 06:36:41 3 25

If I run this query:

select customerid, sum(amount) from test
group by customerid

I get this result:

customerid sum( amount )
1 27.25
2 12.25
3 25

If I run this query:

select customerid, sum(amount) from test
where datetimeconfirmed < '2007-08-08'
group by customerid

I get this result:

customerid sum( amount )
1 15
2 12.25

Which query do I have to run to get this result?

customerid sum( amount ) total
1 15 27.25
2 12.25 12.25
3 NULL 25

Regards,

Jan Nordgreen

PS: I posted this question at comp.databases.mysql a week ago, but no
answers. I know this is a php question, but still.

Aug 18 '07 #1
1 1352
damezumari wrote:
How do I get the total without a condition and the total with the
condition at the same time?

This is my mysql table:

id datetimeconfirmed customerid amount
1 2007-08-14 06:19:03 1 12.25
2 2007-08-06 06:19:19 1 15
3 2007-08-02 06:19:41 2 5
4 2007-08-07 06:19:58 2 7.25
5 2007-08-09 06:36:41 3 25

If I run this query:

select customerid, sum(amount) from test
group by customerid

I get this result:

customerid sum( amount )
1 27.25
2 12.25
3 25

If I run this query:

select customerid, sum(amount) from test
where datetimeconfirmed < '2007-08-08'
group by customerid

I get this result:

customerid sum( amount )
1 15
2 12.25

Which query do I have to run to get this result?

customerid sum( amount ) total
1 15 27.25
2 12.25 12.25
3 NULL 25

Regards,

Jan Nordgreen

PS: I posted this question at comp.databases.mysql a week ago, but no
answers. I know this is a php question, but still.
Answered over on comp.databases.mysql
Aug 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Graham | last post: by
4 posts views Thread by James Greig | last post: by
22 posts views Thread by John | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.