T-SQL 查询问题

发布于 2024-10-09 21:17:01 字数 688 浏览 0 评论 0原文

我有一个名为 Summary 的表,表中的数据如下所示:

ID     Type    Name         Parent

1      Act     Rent          Null
2      Eng     E21-01-Rent   Rent
3      Prj     P01-12-Rent   E21-01-Rent
1      Act     Fin           Null
2      Eng     E13-27-Fin    Fin
3      Prj     P56-35-Fin    E13-27-Fin

我正在编写一个 SP,它必须根据类型拉取父级。这里 Act 类型的 ID 始终为 1,Eng 类型的 ID 为 2,Prj 类型的 ID 为 3。ACT

父类型始终为 NULL, 类型 Eng 父级是 Act 并且 type Prj Parent is Eng

现在我有一个名为 Detail 的表。我正在编写一个 SP 将详细表数据插入到汇总表中。

我将 id 作为参数传递:

我与父级有问题。我怎样才能得到它?

我总是可以说,当 ID 为 1 时,父级为 Null,但当 ID 为 2 时,父级是 ID 1 的名称,类似地,当 ID 为 3 时,父级是 ID2 的名称。

我怎样才能得到它?

谁能帮我解决这个问题:

I have table called Summary and the data in the table looks like this:

ID     Type    Name         Parent

1      Act     Rent          Null
2      Eng     E21-01-Rent   Rent
3      Prj     P01-12-Rent   E21-01-Rent
1      Act     Fin           Null
2      Eng     E13-27-Fin    Fin
3      Prj     P56-35-Fin    E13-27-Fin

I am writing a SP which has to pull the parent based on type. Here always the type Act has ID 1, Eng has ID 2 and Prj has ID 3.

The type ACT parent is always NUll,
type Eng parent is Act and
type Prj parent is Eng

Now I have table called Detail.I am writing a SP to insert Detail Table data to the Summary table.

I am passing the id as parameter:

I am having problem with the parent. How do I get that?

I can always say when ID is 1 then parent is Null but when ID is 2 then parent is name of ID 1 similarly when ID is 3 then parent is name of ID2.

How do I get that?

Can anyone help me with this:

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

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

发布评论

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

评论(2

孤凫 2024-10-16 21:17:01

我建议重新访问您的数据模型,但这也许会有所帮助...

/* @Parent is VarChar(255) because definition is unknown.
   We use Top(1) because in example ID is not unique.
   We assume the ID being passed in is declared as @ID.*/
Declare @ParentID Int
Set @ParentID=(@ID-1)
Declare @Parent VarChar(255)
Set @Parent = (Select Top(1) Name From Summary
               Where ID=@ParentID) 

只需将 @Parent 插入 Parent 列即可。

I advise re-visiting your data model, but maybe this will help...

/* @Parent is VarChar(255) because definition is unknown.
   We use Top(1) because in example ID is not unique.
   We assume the ID being passed in is declared as @ID.*/
Declare @ParentID Int
Set @ParentID=(@ID-1)
Declare @Parent VarChar(255)
Set @Parent = (Select Top(1) Name From Summary
               Where ID=@ParentID) 

Just insert @Parent into the Parent column.

幻梦 2024-10-16 21:17:01

我认为您必须有一个主键(另一个唯一的 ID),因为使用此模型,您将始终拥有该类型的第一行,例如 prj 将始终具有父级“E21-01-Rent”,并且无法到达“ E13-27-鳍”。

最后尝试这个查询

Select id,name,(Select Top 1 [Name] From [Summary]
               Where ID=@ID -1) as parent2
 from [Summary]
where id = @ID

I think you must have a primary key (another ID which is unique) because with this model you will always have the first row of the type for example prj will always have the parent "E21-01-Rent" and no way to reach "E13-27-Fin" .

At last try this query

Select id,name,(Select Top 1 [Name] From [Summary]
               Where ID=@ID -1) as parent2
 from [Summary]
where id = @ID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文