简体   繁体   English

在SQL Server 2014中对数据进行舍入

[英]Rounding up of data in SQL Server 2014

I have developed an application in C# (ASP.NET 4.0) using SQL Server 2014 as the database. 我已经使用SQL Server 2014作为数据库在C#(ASP.NET 4.0)中开发了一个应用程序。

I have question about rounding up and summing data. 我对汇总和汇总数据有疑问。 I have data that comes in via CSV through FTP and I import the raw data into a table. 我有通过FTP通过CSV传入的数据,并将原始数据导入到表中。 The data is logged every minute by the customer. 客户每分钟记录一次数据。 The data is identified by customer Id. 数据由客户ID标识。

I have now been asked to take that data and sum the time series data into 15 minutes chunks from the hour. 现在,我被要求获取该数据并将时间序列数据汇总为每小时的15分钟。

They then want that data rounded up into days (midnight to midnight), then that data rounded up into weeks (Monday to Sunday). 然后,他们希望将数据四舍五入为几天(午夜至午夜),然后将数据四舍五入为几周(周一至周日)。 They also want the day data rounded up into calendar months from midnight to midnight and the month data is to be rounded up into a year. 他们还希望将日数据四舍五入为从午夜到午夜的日历月,并将月数据四舍五入为一年。

The idea is that the raw time series data is grouped into its constituent periods, such as day, week, month so they can see the total for that time period. 想法是将原始时间序列数据分组为组成部分的时间段,例如日,周,月,以便他们可以看到该时间段的总计。

I have looked at cursors and loops in SQL and I have been told the overhead will be too great as I already have 300 million rows and counting to process. 我查看了SQL中的游标和循环,并被告知开销将非常大,因为我已经有3亿行记录并在处理。

I don't whether I should develop a service in C# that does it all on the server or use the database. 我不应该使用C#开发服务来在服务器上完成所有工作,还是使用数据库。 The research I have done contradicts itself slightly in each case. 我所做的研究在每种情况下都略有矛盾。

Any hints would be great as where to look and what to try. 任何提示在哪里寻找和尝试什么都将是很棒的。

I believe you are looking more for a desighn than a solution here. 我相信您在这里寻求的不仅仅是解决方案。 I would suggest you to create a table which will hold the data of the ftp load along with a batch id (a unique identifier). 我建议您创建一个表,该表将保存ftp负载的数据以及批处理ID(唯一标识符)。 create another table where you load this batch id with a status column, alays insert a row here once if you are doen with ftp load into the table1, make status as N. This polling script should call the below sp. 创建另一个表,在其中使用状态列加载该批处理ID,如果您对表1进行ftp加载,则在此处插入一行,将状态设置为N。此轮询脚本应调用以下sp。

Now, create a polling script from c# or if you are experienced with service broker in sql use that, to poll this table2 with batch id and status with status as N. 现在,从c#创建一个轮询脚本,或者如果您有使用SQL服务代理的经验,则可以使用批处理ID和状态为N的状态轮询此table2。

Now, create another stored procedure which will sum up all the records for this batch id only. 现在,创建另一个存储过程,该存储过程将仅汇总该批次ID的所有记录。 And add the values to the daily count approaitely.. The same will be done weekly counts and all... 并将这些值适当地添加到每日计数中。.将同样执行每周计数和所有...

Once all this is done, remove the information from the table1 with the batch id for which we processed, if you need this info for future purpose you can sotre it in different table. 完成所有这些操作后,从table1中删除具有我们处理的批次ID的信息,如果您需要此信息以备将来使用,则可以将其存储在其他表中。

To have the power of managing data and ready for any change for business rules in the future, you need to add some control columns to the table. 为了具有管理数据的能力并准备将来对业务规则进行任何更改,您需要在表中添加一些控制列。 The controls managing period/hour/day/month/year /... whatever in future 控制周期/小时/天/月/年/ ...将来的控制

Just when you add the period fill the corresponding control fields once at a time with the corresponding value: 只需添加周期,就一次用相应的值填充相应的控制字段:

period 1..4
hour 1-24
day 1..366
week 1..55
month 1..12
year 1.. (if needed)

You can define set of SQL functions to fill these columns at once(during data loading from the file). 您可以定义一组SQL函数以一次填充这些列(在从文件加载数据的过程中)。

Create index for these columns. 为这些列创建索引。

Once , you do this you can by, c# code /sql code, you do the summing up dynamically to any period/hour/day/..... You can benefit from Analysis server / window functions / pivots to do your magic :) on data for any interval. 一旦完成此操作,您就可以通过c#代码/ sql代码,对任何时间段/小时/天/天进行动态汇总。..您可以受益于Analysis服务器/窗口函数/枢轴来做您的魔术: )在任意间隔的数据上。

This approach gives you the power of keeping data , no deletion , except for archiving purpose and managing changes in the future. 除了存档目的和将来管理更改之外,这种方法使您能够保留数据,不删除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM