对相似记录进行分组 Sql Server 2008
我将编写一个查询来实现如下所示的功能:
TableName: Application
AppId (PK,int) AppType (bit)
1 0
2 0
3 0
4 0
5 1
6 0
7 0
8 0
9 1
10 1
11 0
12 0
13 1
14 0
15 1
我必须根据 App_type 按顺序对 App_Id 进行分组并创建一批记录。需要注意的重要一点是,我在创建批次时必须维护 AppId 的序列。一个批次可以拥有的最大记录数取决于批次大小参数(假设批次大小目前设置为 3)。创建批次后,将详细信息插入到名为 ApplicationBatch 的不同表中。所以我想要一个类似的输出:
TableName: ApplicationBatch
BatchId MinAppId MaxAppId AppType
001 1 3 0
002 4 4 0
003 5 5 1
004 6 8 0
005 9 10 1
006 11 12 0
007 13 13 1
008 14 14 0
009 15 15 1
我还必须以最有效和优化的方式设计查询,因为应用程序表中可以有超过一百万条记录。
更新:
目前我有应用程序表(在上面的原始问题中定义),我想根据应用程序表中的数据填充 ApplicationBatch 表。
I will be writing a query to achieve something like below:
TableName: Application
AppId (PK,int) AppType (bit)
1 0
2 0
3 0
4 0
5 1
6 0
7 0
8 0
9 1
10 1
11 0
12 0
13 1
14 0
15 1
I have to sequentially group App_Id based on App_type and create a batch of records. The important point to note is that I have to maintain the SEQUENCE of AppId while creating the batches. The maximum number of records a batch can have depends on batch size parameter (say batch size set to 3 for now). Once the batch is created, insert the details in a different table say called ApplicationBatch. So I want an output something like:
TableName: ApplicationBatch
BatchId MinAppId MaxAppId AppType
001 1 3 0
002 4 4 0
003 5 5 1
004 6 8 0
005 9 10 1
006 11 12 0
007 13 13 1
008 14 14 0
009 15 15 1
One more thing I have to design the query in a best efficient and optimized way because Application table can have more than million records in it.
Update:
Currently I have Application table (defined in my original question above) and I want to populate ApplicationBatch table based on data from Application table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个。相当复杂但有效。我没有在这么多行上测试它,但它只迭代表一次。
首先你必须做一些预处理。
并执行查询:
Try this. Pretty complicated but works. I didn't test it on so many rows, but it iterates through the table only once.
First you have to do some preporocesing.
And execute the query:
问题没说清楚,不过我明白了下。
您想要一个应用程序序列,并且这取决于插入到另一个表中的行数?
再见。
the question is not clear, but I understand the next.
you want to have a sequence for aplication and that depends of the number of lines inserted in anohter table?
see you.
如果没有 CURSORS 或 SQLCLR,很难做到这一点。您是否会考虑用 C# 编写表值函数并将程序集嵌入 SQL Server 中? (SQLCLR)
这就是我要做的,然后我会做一个 while 循环按顺序处理记录。
It's hard to do this without CURSORS or SQLCLR. Would you consider writting a table-valued function in c# and ambedding the assembly in SQL Server? (SQLCLR)
That's what I would do, and then I would do a while loop processing the records sequantially.