我的查询是ms-access:
SELECT FORMAT(MSC_cdr_counters2.[datetime1],"DD/MM/YYYY") AS date1, MSC_cdr_counters2.[type], sum(MSC_cdr_counters2.[counter]) AS counter
FROM MSC_cdr_counters2
WHERE (MSC_cdr_counters2.[datetime1] BETWEEN #10/01/2016# AND #11/01/2016 00:01:00#)
AND (MSC_cdr_counters2.[type] = "MOC"
OR MSC_cdr_counters2.[type] = "FORW"
OR MSC_cdr_counters2.[type] = "SMMO"
OR MSC_cdr_counters2.[type] = "SOC"
OR MSC_cdr_counters2.[type] = "PBXO")
AND MSC_cdr_counters2.[datetime1] LIKE "*2016 0:00:0*"
GROUP BY MSC_cdr_counters2.[datetime1], MSC_cdr_counters2.[type] ;
结果(部分内容):
date1 type counter
04.10.2016 FORW 6173453
04.10.2016 MOC 563216323
04.10.2016 PBXO 19
04.10.2016 SMMO 186950644
04.10.2016 SOC 17377649
04.10.2016 FORW 43247163
04.10.2016 MOC 3026671916
04.10.2016 PBXO 17812373
04.10.2016 SMMO 704397148
04.10.2016 SOC 1690090
05.10.2016 FORW 6173593
05.10.2016 MOC 563216323
05.10.2016 PBXO 19
05.10.2016 SMMO 186950644
05.10.2016 SOC 17399117
05.10.2016 FORW 43261775
05.10.2016 MOC 3027868092
05.10.2016 PBXO 17812373
05.10.2016 SMMO 704782312
05.10.2016 SOC 1694332
06.10.2016 FORW 6173752
06.10.2016 MOC 563216323
06.10.2016 PBXO 19
06.10.2016 SMMO 186950644
06.10.2016 SOC 17421247
06.10.2016 FORW 43276252
06.10.2016 MOC 3029133560
06.10.2016 PBXO 17812373
06.10.2016 SMMO 705187581
06.10.2016 SOC 1698475
但我想要输出:
Date Type Increase in counters per day vs next day
4.10.2016 FORW 14752
4.10.2016 MOC 1196176
4.10.2016 PBXO 0
4.10.2016 SMMO 385164
4.10.2016 SOC 25710
5.10.2016 FORW 14636
5.10.2016 MOC 1265468
5.10.2016 PBXO 0
5.10.2016 SMMO 405269
5.10.2016 SOC 26273
6.10.2016 FORW 14308
6.10.2016 MOC 1237012
6.10.2016 PBXO 0
6.10.2016 SMMO 390327
6.10.2016 SOC 25520
我一直在谷歌搜索很多,但我似乎无法找到一种方法如何查询每种类型的每日计数(5.10计数器 - 4.10计数器,6.10计数器 - 5.10计数器等)。我的sql技能不是很好。有人可以帮助我吗?
答案 0 :(得分:0)
如何获得相同日期和类型的重复行?看起来在同一天必须有两个不同的时间条目来给出你得到的结果。您需要逻辑将它们组合到单个值。我设置此查询以将其保存为MSC_CDR_Summary
SELECT Year([DateTime1]) AS Yr,
Month([DateTime1]) AS Mn,
Day([DateTime1]) as Dy,
First(MSC_CDR_Counter2.DateTime1) AS DateTime1,
MSC_CDR_Counter2.Type,
Sum(MSC_CDR_Counter2.Counter) AS [Counter]
FROM MSC_CDR_Counter2
GROUP BY Year([DateTime1]), Month([DateTime1]), MSC_CDR_Counter2.Type
ORDER BY First(MSC_CDR_Counter2.DateTime1), MSC_CDR_Counter2.Type;
通过此摘要,我创建了以下内容以在天之间进行更改:
SELECT MSC_CDR_Summary.DateTime1,
MSC_CDR_Summary.Type,
MSC_CDR_Summary.Counter,
(select first(Counter) from MSC_CDR_Summary T where T.Type=MSC_CDR_Summary.Type and T.DateTime1>MSC_CDR_Summary.DateTime1) AS [Next],
[Next]-[Counter] AS Change
FROM MSC_CDR_Summary;
这使用子查询来查找相同类型的下一个日期并获取该日期的计数器。如果更清楚,可以将其作为单独的查询拉出来。
在我看来,您希望在第5天报告从第4天到第5天的更改,但我显然不知道您的全部要求。