如何从上面的行中选择信息?

发布于 2024-12-28 02:46:07 字数 1179 浏览 1 评论 0原文

我想在我的表中添加一列,如下所示: 这只是表结构的一个示例,实际表有超过 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:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冬天的雪花 2025-01-04 02:46:07

SQL Server 2012 建议使用窗口偏移函数。
在这种情况下:LAG

是这样的:

SELECT [No_]
  ,[Name]
  ,[Account_Type]
  ,[Subgroup]
  ,LAG([No_]) OVER(PARTITION BY [Subgroup]
ORDER BY [No_]) as [PrevValue]
FROM table

这是来自 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:

SELECT [No_]
  ,[Name]
  ,[Account_Type]
  ,[Subgroup]
  ,LAG([No_]) OVER(PARTITION BY [Subgroup]
ORDER BY [No_]) as [PrevValue]
FROM table

Here is an example from MS:
http://technet.microsoft.com/en-us/library/hh231256.aspx

哭泣的笑容 2025-01-04 02:46:07

ROW_NUMBER 函数将允许您找出该行的编号,但由于它是一个窗口函数,因此您必须使用公共表表达式 (CTE) 将表与其自身连接起来。

WITH cte AS
(
    SELECT [No_], Name, Account_Type, Subgroup, [Row] = ROW_NUMBER() OVER (ORDER BY [No_])
    FROM table
)
SELECT t1.*, t2.[No_]
FROM cte t1 
LEFT JOIN cte t2 ON t1.Row = t2.Row - 1

希望这有帮助。

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.

WITH cte AS
(
    SELECT [No_], Name, Account_Type, Subgroup, [Row] = ROW_NUMBER() OVER (ORDER BY [No_])
    FROM table
)
SELECT t1.*, t2.[No_]
FROM cte t1 
LEFT JOIN cte t2 ON t1.Row = t2.Row - 1

Hope this helps.

无可置疑 2025-01-04 02:46:07

下一个查询将返回父行的名称而不是行本身,即 Sales、Underwear、Bikes 的 Sales;利润换利润,现金:

select ISNULL(t2.Name, t1.Name)
from table t1
left join table t2 on t1.NewColumn = t2.No

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:

select ISNULL(t2.Name, t1.Name)
from table t1
left join table t2 on t1.NewColumn = t2.No
溇涏 2025-01-04 02:46:07

因此,在 SQL Server 2008 中,我创建了包含 3 个值的测试表:

create table #ttable
(
    id int primary key identity,
    number int,
    number_prev int
)
Go
Insert Into #ttable (number) 
Output inserted.id
Values (10), (20), (30);

插入表,它会执行您需要的操作(至少如果理解正确的话),如下所示:

declare @new_value int;
set @new_value = 13; -- NEW value
Insert Into #ttable (number, number_prev)
Values (@new_value, 
    (Select Max(number) From #ttable t Where t.number < @new_value)) 

[添加了这部分] 要使用子组 - 只需修改内部选择将其过滤掉:

Select Max(number) From #ttable t 
Where t.number < @new_value And Subgroup != @Subgroup

So in SQL Server 2008 i created test table with 3 values in it:

create table #ttable
(
    id int primary key identity,
    number int,
    number_prev int
)
Go
Insert Into #ttable (number) 
Output inserted.id
Values (10), (20), (30);

Insert in table, that does what you need (at least if understood correctly) looks like this:

declare @new_value int;
set @new_value = 13; -- NEW value
Insert Into #ttable (number, number_prev)
Values (@new_value, 
    (Select Max(number) From #ttable t Where t.number < @new_value)) 

[This part added] And to work with subgroup- just modify the inner select to filter out it:

Select Max(number) From #ttable t 
Where t.number < @new_value And Subgroup != @Subgroup
一袭水袖舞倾城 2025-01-04 02:46:07
SELECT
      No_
    , Name 
    , Account_Type 
    , Subgroup 
    , ( SELECT MAX(above.No_) 
        FROM TableX AS above
        WHERE above.No_ < a.No_
          AND above.Account_Type = 3
          AND a.Account_Type <> 3
      ) AS NewColumn
FROM
      TableX AS a
SELECT
      No_
    , Name 
    , Account_Type 
    , Subgroup 
    , ( SELECT MAX(above.No_) 
        FROM TableX AS above
        WHERE above.No_ < a.No_
          AND above.Account_Type = 3
          AND a.Account_Type <> 3
      ) AS NewColumn
FROM
      TableX AS a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文