需要使用SQL Server 2008以如下格式显示数据

需要使用SQL Server 2008以如下格式显示数据,sql,sql-server-2008,cursor,Sql,Sql Server 2008,Cursor,如果我加入两个表,我将得到下表 SELECT a.Category, a.categoryid, b.parentid,b.Level FROM a JOIN b ONa.CatId=b.Catid 实际数据:- 这是一个使用游标的示例,我必须像这样应用于所有类别Id 我需要这样显示这些数据: Cat1 Cat2 Cat3 ItemNo ---------------------------------------

如果我加入两个表,我将得到下表

SELECT a.Category, a.categoryid, b.parentid,b.Level
FROM a JOIN b ONa.CatId=b.Catid
实际数据:-

这是一个使用游标的示例,我必须像这样应用于所有类别Id

我需要这样显示这些数据:

  Cat1               Cat2           Cat3             ItemNo
  ------------------------------------------------------------
  Pumps & Accss      Above Ground   Hawrd super      AX007123 
  Pumps & Accss      Above Ground   Hywrd north      AX0071201 
  Pumps & Accss      Commercial Pu  Hawrd super      AX007754  
  Pumps & Accss      Commercial Pu  Hawrd super      AX0077891 
  Pumps & Accss      Inground Pumps Hywrd north      AX0071251
Cat1列中的级别1和Cat2列中的级别2,如图i所示

需要获得输出。像这样,我有10个级别,请发布一些好的答案
解决此问题。

检查此。。。您可以在同一个表中使用多次来构建多级联接

declare @category table(categoryId int, category char(100), principalId int)
declare @article table(articleId int, article varchar(100), categoryId int)

insert into @category(categoryId, category, principalId)
values (1,'One', null)

insert into @category(categoryId, category, principalId)
values (2,'two', null)

insert into @category(categoryId, category, principalId)
values (11,'eleven', 1)

insert into @category(categoryId, category, principalId)
values (21,'twenty one', 2)

insert into @category(categoryId, category, principalId)
values (22,'twenty two', 2)

insert into @category(categoryId, category, principalId)
values (111,'one hundred eleven', 11)

insert into @category(categoryId, category, principalId)
values (211,'two hundred eleven', 21)

insert into @category(categoryId, category, principalId)
values (221,'two hundred twenty one', 22)

insert into @category(categoryId, category, principalId)
values (2211,'two thousand two hundred twenty one', 221)

insert into @article(articleId, article, categoryId)
values (1, 'Article 1', 111)

insert into @article(articleId, article, categoryId)
values (2, 'Article 2', 211)

insert into @article(articleId, article, categoryId)
values (3, 'Article 3', 221)

insert into @article(articleId, article, categoryId)
values (4, 'Article 4', 2211)

select coalesce(c5.category,'') c5,
    coalesce(c4.category,'') c4,
    coalesce(c3.category,'') c3,
    coalesce(c2.category,'') c2,
    coalesce(c1.category,'') c1,
    a.article
from @article a
    left join @category c1 on c1.categoryId = a.categoryId
    left join @category c2 on c2.categoryId = c1.principalId
    left join @category c3 on c3.categoryId = c2.principalId
    left join @category c4 on c4.categoryId = c3.principalId
    left join @category c5 on c5.categoryId = c4.principalId
如果你分享表格定义,这有助于改进我的答案,
希望它能帮助你指出正确的方向

我想你需要一个支点:你知道这个朋友吗?你不能使用包含itemno的表连接。itemno在产品表上它会一直读到最后一个孩子,最后一级与itemno表有关系。我使用产品的定义为:declare@article tablearticleId int,文章varchar100,categoryId int,正如我从您那里得到的,您的数据是:productproductId int,itemNo varcharx,categoryId int。。。想法是一样的,只需更改表的名称。。。如果我有空的单元格值,级别信息在我所在行的类别表中。如何删除?朋友请参考此url:-我需要该格式的结果
declare @category table(categoryId int, category char(100), principalId int)
declare @article table(articleId int, article varchar(100), categoryId int)

insert into @category(categoryId, category, principalId)
values (1,'One', null)

insert into @category(categoryId, category, principalId)
values (2,'two', null)

insert into @category(categoryId, category, principalId)
values (11,'eleven', 1)

insert into @category(categoryId, category, principalId)
values (21,'twenty one', 2)

insert into @category(categoryId, category, principalId)
values (22,'twenty two', 2)

insert into @category(categoryId, category, principalId)
values (111,'one hundred eleven', 11)

insert into @category(categoryId, category, principalId)
values (211,'two hundred eleven', 21)

insert into @category(categoryId, category, principalId)
values (221,'two hundred twenty one', 22)

insert into @category(categoryId, category, principalId)
values (2211,'two thousand two hundred twenty one', 221)

insert into @article(articleId, article, categoryId)
values (1, 'Article 1', 111)

insert into @article(articleId, article, categoryId)
values (2, 'Article 2', 211)

insert into @article(articleId, article, categoryId)
values (3, 'Article 3', 221)

insert into @article(articleId, article, categoryId)
values (4, 'Article 4', 2211)

select coalesce(c5.category,'') c5,
    coalesce(c4.category,'') c4,
    coalesce(c3.category,'') c3,
    coalesce(c2.category,'') c2,
    coalesce(c1.category,'') c1,
    a.article
from @article a
    left join @category c1 on c1.categoryId = a.categoryId
    left join @category c2 on c2.categoryId = c1.principalId
    left join @category c3 on c3.categoryId = c2.principalId
    left join @category c4 on c4.categoryId = c3.principalId
    left join @category c5 on c5.categoryId = c4.principalId