如何从上面的行中选择信息?
我想在我的表中添加一列,如下所示: 这只是表结构的一个示例,实际表有超过 10,000 行。
No_ Name Account_Type Subgroup (New_Column)
100 Sales 3
200 Underwear 0 250 *100
300 Bikes 0 250 *100
400 Profit 3
500 Cash 0 450 *400
因此,每次“子组”中有一个值时,我希望 (New_Column) 从上面的行获取值 [No_]
No_ Name Account_Type Subgroup (New_Column)
100 Sales 3
150 TotalSales 3
200 Underwear 0 250 *150
300 Bikes 0 250 *150
400 Profit 3
500 Cash 0 450 *400
在某些情况下,表格与上面类似,上面有两个“标题”。在这种情况下,我还想要上面的第一行(150)。
这是光标的情况还是您有什么建议?
数据按 No_
--EDIT--
排序,从第一行开始,然后遍历整个表: 有没有办法可以存储 [No_] 的值,其中 [Subgroup] 是 ''? 然后将此 [No_] 值插入到下面每行的 (New_Column) 中,并在 [Subgroup] 行中具有值。 当 [Subgroup] 行为空时,该过程将继续进行,在 (New_Column) 中插入下一个 [No_] 值,也就是说,如果下一行在 [Subgroup] 中有一个值,
这是我所要的更好的图像尝试做:
I want to add a column to my table that is like the following:
This is just an example of how the table is structured, the real table is more than 10.000 rows.
No_ Name Account_Type Subgroup (New_Column)
100 Sales 3
200 Underwear 0 250 *100
300 Bikes 0 250 *100
400 Profit 3
500 Cash 0 450 *400
So for every time there is a value in 'Subgroup' I want the (New_Column) to get the value [No_] from the row above
No_ Name Account_Type Subgroup (New_Column)
100 Sales 3
150 TotalSales 3
200 Underwear 0 250 *150
300 Bikes 0 250 *150
400 Profit 3
500 Cash 0 450 *400
There are cases where the table is like the above, where two "Headers" are above. And in that case I also want the first above row (150) in this case.
Is this a case for a cursor or what do you recommend?
The data is ordered by No_
--EDIT--
Starting from the first line and then running through the whole table:
Is there a way I can store the value for [No_] where [Subgroup] is ''?
And following that insert this [No_] value in the (New_Column) in each row below having value in the [Subgroup] row.
And when the [Subgroup] row is empty the process will keep going, inserting the next [No_] value in (New_Column), that is if the next line has a value in [Subgroup]
Here is a better image for what I´m trying to do:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SQL Server 2012 建议使用窗口偏移函数。
在这种情况下:LAG
是这样的:
这是来自 MS 的示例:
http://technet.microsoft.com/en-us/library/hh231256.aspx
SQL Server 2012 suggests using Window Offset Functions.
In this case : LAG
Something like this:
Here is an example from MS:
http://technet.microsoft.com/en-us/library/hh231256.aspx
ROW_NUMBER 函数将允许您找出该行的编号,但由于它是一个窗口函数,因此您必须使用公共表表达式 (CTE) 将表与其自身连接起来。
希望这有帮助。
The ROW_NUMBER function will allow you to find out what number the row is, but because it is a windowed function, you will have to use a common table expression (CTE) to join the table with itself.
Hope this helps.
下一个查询将返回父行的名称而不是行本身,即 Sales、Underwear、Bikes 的 Sales;利润换利润,现金:
Next query will return Name of the parent row instead of the row itself, i.e. Sales for both Sales, Underwear, Bikes; and Profit for Profit, Cash:
因此,在 SQL Server 2008 中,我创建了包含 3 个值的测试表:
插入表,它会执行您需要的操作(至少如果理解正确的话),如下所示:
[添加了这部分] 要使用子组 - 只需修改内部选择将其过滤掉:
So in SQL Server 2008 i created test table with 3 values in it:
Insert in table, that does what you need (at least if understood correctly) looks like this:
[This part added] And to work with subgroup- just modify the inner select to filter out it: