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

拉梅什

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

SELECT a.Category, a.categoryid, b.parentid,b.Level
FROM a JOIN b ONa.CatId=b.Catid

实际数据:-

  Category               Catid         ParentID      Level
  -----------------------------------------------------------
  Pumps & Accss             20               0           0                                            
  Inground Pumps           213               20          1
  Above Ground             215               20          1                                              
  Commercial Pumps         216               20          1 
  Hawrd super pumps        814               213         2 
  Hywrd north pumps        815               213         2

这是一个使用游标的示例,我必须对所有类别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列中的Level 1和Cat2列中的Level2像这样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

如果您共享表格定义,这有助于改善我的答案,希望它可以帮助您指出正确的方向

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Javascript或VueJS以以下格式显示JSON数据

将日期时间转换为以下格式的SQL Server

SQL Server 2008 R2需要使用哪个版本的Visual Studio和SSDT-BI?

如何在 SQL Server 中获得以下格式的结果?

是否需要使用#在SQL Server中创建临时表?

SQL Server:何时需要使用函数/过程而不是查询?

需要使用SQL以2020年1月,2019年2月等格式显示数据...

需要以下格式的正确输出

需要以下格式的 SQL 结果集

SQL以以下格式打印结果

SQL Server 2008:以hh:mm格式显示时间

使用 map() 或类似函数以以下格式返回数组数据

SQL Server 查询需要使用列表中的下拉值,如何?

如何更改SQL Server(2008)数据库的日期格式?

如何使用存储过程将xml格式的数据拆分为sql server 2008中的行列格式

需要使用MomentJS显示AM / PM

使用 AngularFire2 在 Angular 5 应用程序中读取和更新以下格式的 Fire-base 数据

需要使用httpclient获取表数据

将 sql 输出转换为以下格式

SQL Query 实现以下格式的序列

如何獲得以下格式的sql結果

使用SQL Server数据库是否需要SQL Server Management Studio?

如何使用以下格式的javascript创建json对象

我想将数据从SQL Server DB移至Hbase / Cassandra等。如何确定要使用哪个大数据数据库?

SQL Server 2008中的日期格式

我需要使用COUNT()函数选择表中不存在的数据为零,如何使用Sql来选择

如何在SQL Server 2008中将数据转换为json格式?

SQL:我需要使用 Case 作为值之一来格式化 Select Distinct Statement

需要使用func了解以下代码