【发布时间】:2014-07-31 14:13:47
【问题描述】:
我需要从表中选择与此相关的数据:
monthStart monthEnd newPhones totalPhones oblid
1 1 1 2 1
2 2 1 2 2
1 2 2 2 3
2 2 1 1 4
2 3 0 3 5
所以我想选择 4 个字段:月份,以 monthStart 为基础计算月份的 obj,以 monthEnd 为基础的 newPhones 总和,以 monthEnd 为基础的 totalPhones 总和。
所以对于这个数据我需要选择这个:
month count totalPhones newPhones
1 3 2 1
2 2 5 4
3 0 3 0
第 1 个月的计数 = 3,因为我们有 3 行,monthStart = 1,但我们只有一行 monthEnd = 1,所以 1 个月的电话总数 = 2,newPhones = 1 计算第 3 个月 = 0,因为我们有 0 行,monthStart = 3, 但是我们有 3 个 totalPhones 和 0 个 newPhones for monthEnd = 3 - 我们应该显示这些数据。
我一直坚持这一点。我试图从这个选择的结果中进行选择:
SELECT
monthStart,
monthEnd,
count(1) as uploaded,
sum(newPhones) as newPhones,
sum(totalPhones) as totalPhones
from TestGB
group by monthEnd, monthStart
但我无法得到理想的结果。 感谢您的帮助!
【问题讨论】: