mysql组号不止一次使用并增加总成本

时间:2014-12-04 13:22:00

标签: mysql sql

我需要帮助,我有日期,数量,持续时间和费用

我希望看到一个结果,其中一个号码被多次拨打并且总费用加在一起

date                     number     duration  cost
17/11/2014 10:32:19 am   0800       00:00:00  0.00
17/11/2014 11:06:47 am   071991     00:02:42  3.54
13/11/2014 02:47:40 pm   060302874  00:00:00  0.00
 6/11/2014 11:53:28 am   0100601555 00:00:47  0.50
24/11/2014 12:06:27 pm   0113151407 00:00:46  0.50
19/11/2014 08:37:34 am   0113941905 00:00:47  0.50
24/11/2014 02:48:43 pm   0113941905 00:00:29  0.50
19/11/2014 08:39:16 am   0113949182 00:01:03  0.50
24/11/2014 02:41:57 pm   0113949182 00:00:36  0.50
24/11/2014 12:08:09 pm   0113949182 00:00:43  0.50
24/11/2014 02:50:29 pm   0114922660 00:03:26  1.72

3 个答案:

答案 0 :(得分:1)

一个简单的数字分组应该这样做

select number, 
sum(cost) total_cost, 
count(*) times_dialed, 
min(date) earliest_date, 
max(date) latest_date
from mytable 
group by number
having count(*) > 1

答案 1 :(得分:1)

SELECT number
     , SUM(cost) total 
  FROM my_table 
 GROUP 
    BY number 
HAVING COUNT(*) > 1;
+------------+-------+
| number     | total |
+------------+-------+
| 0113941905 |  1.00 |
| 0113949182 |  1.50 |
+------------+-------+

答案 2 :(得分:-1)

你走了:

SELECT
number, sum(cost)
FROM myTable as a,
(SELECT 
    number, count(*) as dials
FROM myTable
GROUP BY 1) as b
WHERE a.number = b.number 
and b.dials > 1
GROUP BY 1