导出前的SQL平均数据

时间:2013-01-08 08:50:00

标签: sql csv export average

不是SQL的新手,也不是专家,但这对我来说太复杂了。

我有一个大型数据集,我需要在将数据导出到.csv文件之前平均一些样本。 我需要的是将数据平均为例如20Hz数据样本并将其导出到.csv文件。我真的想在sql中这样做,以便我可以构建多个不同的查询,针对最终用户的不同数据请求。所有人都必须使用平均功能。 我的数据集的.csv转储看起来像这样。

    TimeStamp;Ds01;Ds02;Ds03;Ms01;Ms02
2012-11-16 07:24:11.0250 +00:00;4.18527784986003000000;9.94218092211272000000;2.31807112793595000000;249.34515570476700000000;-0.05185083571517050000
2012-11-16 07:24:11.0790 +00:00;5.61238782234661000000;10.29512074186070000000;2.47669503367129000000;249.34515570476700000000;-0.00729058410636121000
2012-11-16 07:24:11.1320 +00:00;4.15225189079078000000;10.68445062998030000000;2.58478777829982000000;249.34515570476700000000;0.10729292003057700000
2012-11-16 07:24:11.1860 +00:00;4.59751952718327000000;10.49899281165300000000;1.99555049965409000000;249.34515570476700000000;0.01339810414058600000
2012-11-16 07:24:11.2400 +00:00;5.19004408695513000000;9.79530534495047000000;1.95512557076862000000;249.34515570476700000000;-0.12028265068584200000
2012-11-16 07:24:11.2930 +00:00;4.77508262476736000000;11.18075855042080000000;2.57731795448403000000;249.34515570476700000000;0.01976385437041590000
2012-11-16 07:24:11.3470 +00:00;4.59557682370861000000;10.11536257279660000000;3.04659865067615000000;249.34515570476700000000;0.07228129376651260000
2012-11-16 07:24:11.4010 +00:00;3.88066194503305000000;9.86326270154796000000;3.01144653860183000000;249.34515570476700000000;0.06352838720049650000
2012-11-16 07:24:11.4540 +00:00;3.55234505781520000000;11.20268027835550000000;2.73813886722402000000;249.34515570476700000000;0.05716263697066660000
2012-11-16 07:24:11.5080 +00:00;4.78324197936095000000;10.46567178519230000000;1.01744298118624000000;249.34515570476700000000;0.05954979330685280000
2012-11-16 07:24:11.5620 +00:00;4.03063865327694000000;10.96987152768940000000;1.79606226363236000000;249.34515570476700000000;-0.01126917800000490000
2012-11-16 07:24:11.6150 +00:00;5.20636279614228000000;10.03381374487960000000;2.13659834935230000000;249.34515570476700000000;0.00544091635329865000
2012-11-16 07:24:11.6690 +00:00;4.52758220209545000000;10.86289349536830000000;1.82374455189088000000;249.34515570476700000000;0.03408679238753320000
2012-11-16 07:24:11.7230 +00:00;4.21014445433570000000;9.75233875819858000000;2.85150442866369000000;249.34515570476700000000;0.14787457774574300000
2012-11-16 07:24:11.7760 +00:00;4.48484272565288000000;10.72873252040820000000;2.51140774434468000000;249.34515570476700000000;0.02055957314914460000
2012-11-16 07:24:11.8300 +00:00;4.60762158525151000000;9.75628466922681000000;2.87655030851664000000;249.43409815763600000000;-0.05901230472372910000
2012-11-16 07:24:11.8840 +00:00;4.11145511782289000000;9.80407403612444000000;3.28607241418242000000;249.43409815763600000000;0.07228129376651260000
2012-11-16 07:24:11.9370 +00:00;5.81986855344050000000;9.38142312154414000000;2.31367711392666000000;249.43409815763600000000;-0.03991505403423940000
2012-11-16 07:24:11.9910 +00:00;5.19198679042979000000;9.17755105175183000000;2.31367711392666000000;249.43409815763600000000;0.02453816704278830000

所以我需要的是一个像这样的好输出

2012-11-16 07:24:11.0000 +00:00;4.18527784986003000000;9.94218092211272000000;2.31807112793595000000;249.34515570476700000000;-0.05185083571517050000
2012-11-16 07:24:11.2000 +00:00;5.61238782234661000000;10.29512074186070000000;2.47669503367129000000;249.34515570476700000000;-0.00729058410636121000
2012-11-16 07:24:11.4000 +00:00;4.15225189079078000000;10.68445062998030000000;2.58478777829982000000;249.34515570476700000000;0.10729292003057700000
2012-11-16 07:24:11.6000 +00:00;4.59751952718327000000;10.49899281165300000000;1.99555049965409000000;249.34515570476700000000;0.01339810414058600000
2012-11-16 07:24:11.8000 +00:00;5.19004408695513000000;9.79530534495047000000;1.95512557076862000000;249.34515570476700000000;-0.12028265068584200000
2012-11-20 07:24:12.0000 +00:00;10.21270965034590000000;3.40336791376326000000;-1.90150052518475000000;331.83395934740000000000;0.06511982475795400000

Pheew ....我不知道从哪里开始? 任何人有任何好的想法/代码样本?

1 个答案:

答案 0 :(得分:1)

假设MS-SQL(并且在澄清之前不关注出口方面) - 这应该将您的日期分为200ms:

SET DATEFORMAT YMD

IF OBJECT_ID('tempdb..#dates') IS NOT NULL DROP TABLE #dates
SELECT '2012-11-16 07:24:11.025' as [date],4.18527784986003000000 as figure1,9.94218092211272000000 as figure2,2.31807112793595000000 as figure3,249.34515570476700000000 as figure4,-0.05185083571517050000 as figure5 INTO #dates
UNION ALL SELECT '2012-11-16 07:24:11.079',5.61238782234661000000,10.29512074186070000000,2.47669503367129000000,249.34515570476700000000,-0.00729058410636121000
UNION ALL SELECT '2012-11-16 07:24:11.132',4.15225189079078000000,10.68445062998030000000,2.58478777829982000000,249.34515570476700000000,0.10729292003057700000
UNION ALL SELECT '2012-11-16 07:24:11.186',4.59751952718327000000,10.49899281165300000000,1.99555049965409000000,249.34515570476700000000,0.01339810414058600000
UNION ALL SELECT '2012-11-16 07:24:11.240',5.19004408695513000000,9.79530534495047000000,1.95512557076862000000,249.34515570476700000000,-0.12028265068584200000
UNION ALL SELECT '2012-11-16 07:24:11.293',4.77508262476736000000,11.18075855042080000000,2.57731795448403000000,249.34515570476700000000,0.01976385437041590000
UNION ALL SELECT '2012-11-16 07:24:11.347',4.59557682370861000000,10.11536257279660000000,3.04659865067615000000,249.34515570476700000000,0.07228129376651260000
UNION ALL SELECT '2012-11-16 07:24:11.401',3.88066194503305000000,9.86326270154796000000,3.01144653860183000000,249.34515570476700000000,0.06352838720049650000

SELECT 
DATEADD(ms,0- DATEPART(ms,date) % 200,date) as date_group
,AVG(figure1) as figure1
,AVG(figure2) as figure2
,AVG(figure3) as figure3
,AVG(figure4) as figure4
,AVG(figure5) as figure5
FROM #dates
GROUP BY DATEADD(ms,0- DATEPART(ms,date) % 200,date)