2013-01-08 49 views
1

不是SQL新手,但也不是專家,但這對我來說太複雜了。導出前的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.059
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 ....我不知道在哪裏可以從這裏開始? 任何人有任何好主意/代碼樣本?

+3

什麼RDBMS這是什麼? –

+0

你想如何導出數據? –

回答

1

假設MS-SQL(而不是側重於出口方面,直到澄清) - 這應該組您的日期,200毫秒間隔:

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)