i have a table TB_Orders , i save orders in thatl,ike below:
userid | factorid | productname | economy | vip | count |
---|---|---|---|---|---|
1 | A-123 | name1 | 13000 | 18000 | 3 |
1 | A-123 | name2 | 9000 | 13000 | 4 |
2 | A-124 | name2 | 2000 | 5000 | 2 |
For example, user 1: has registered two orders with invoice number A-123.
And user 2 has only registered one order
I want to report as follows:
userid | factorid | total economy | total vip | total count |
---|---|---|---|---|
1 | A-123 | 21000 | 31000 | 7 |
2 | A-124 | 2000 | 5000 | 2 |
thanks for your help
This is an aggregation query:
select userid, factorid, sum(economy), sum(vip), sum(count)
from tb_orders
group by userid, factorid;
Aggregation is a very basic part of SQL functionality. If you don't recognize it, I would suggest that you brush up on your SQL skills with a tutorial, book, or something like that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.