将字符串组合在一起并计算总计

时间:2013-01-10 01:03:26

标签: python sql group-by

将一组电子邮件地址和数字放在一起,使用join在sql中从数据库中取出。

SELECT ost_ticket.email, ost_ticket_response.time_taken
FROM ost_ticket
INNER JOIN ost_ticket_response
ON ost_ticket_response.ticket_id=ost_ticket.ticket_id
WHERE time_taken > 0

输出

('log@tome.ie', 100)
('log@tome.ie', 100)
('log@tome.ie', 10)
('log@tome.ie', 2)
('temp@eircom.net', 30L)
('john@tome-ireland.com', 20L)
('1234567@tome.ie', 1234567L)
('log@tome.ie', 2L)
('log@tome.ie', 2L)
('valerie@tome.ie', 30L)
('john@cmi-tome.com', 10L)
('john@cmi-tome.com', 40L)

希望能够获取所有电子邮件地址并且总计如此

log@tome.ie = 216
temp@eircom.net = 30
john@tome-ireland.com = 70
1234567@tome.ie = 1234567
john@cmi-tome.com = 50 

是否可以通过sql或输出后在python中完成?

1 个答案:

答案 0 :(得分:3)

从数据库中提取数据时,请使用如下查询:

select emailaddress, sum(number) as total
from t
group by emailaddress