How to pivot, link and group a table with

末鹿安然 提交于 2019-12-11 04:39:54

问题


I have a table beta with cols Id, Parent, Name, Level

CREATE TABLE [beta](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Parent] [int] NULL,
[Name] [varchar](150) NOT NULL,
[Level] [int] NULL,
 CONSTRAINT [PK_Beta] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Sample contents are

Id Parent   Name             Level
1   NULL    Clinical         1
2   NULL    Custom           1
3   NULL    Medicare         1
4   NULL    Validation       1
5   1       Medicaid         2
6   2       CD               2
7   3       Specialty        2
8   5       Fraud            3
9   2       Institutional    3
10  8       Professional     4

Id is unique. There are up to 4 levels. Each 'Name' can be traced back to level 1. Level 1 'Parent' is always NULL.

I am trying to retrieve result sot look like this

Level1   Level2   Level3 Level4       Id
Clinical Medicaid Fraud  Professional 1, 5, 8, 10
Custom   CD       NULL   NULL         2, 6
...............

or like this

Level1   Level2   Level3 Level4       Level1Id Leve2Id Level3Id Level4Id
Clinical Medicaid Fraud  Professional 1        5       8        10
Custom   CD       NULL   NULL         2        6       NULL     NULL
...............

How do i get this in SQL query


回答1:


If you have a fixed number of levels

Example

Declare @YourTable Table ([Id] int,[Parent] int,[Name] varchar(50),[Level] int)
Insert Into @YourTable Values 
 (1,NULL,'Clinical',1)
,(2,NULL,'Custom',1)
,(3,NULL,'Medicare',1)
,(4,NULL,'Validation',1)
,(5,1,'Medicaid',2)
,(6,2,'CD',2)
,(7,3,'Specialty',2)
,(8,5,'Fraud',3)
,(9,2,'Institutional',3)
,(10,8,'Professional',4)

;with cteP as (
      Select Id
            ,Parent 
            ,PathID = cast(Id as varchar(500))
            ,PathNm = cast(name as varchar(500))
      From   @YourTable
      Where  Parent is null
      Union  All
      Select Id  = r.Id
            ,Parent  = r.Parent 
            ,PathID = cast(p.PathID+', '+cast(r.Id as varchar(25)) as varchar(500))
            ,PathNm = cast(p.PathNm+'||'+r.name as varchar(500))
      From   @YourTable r
      Join   cteP p on r.Parent  = p.Id)
Select B.*
      ,ID = A.PathID
 From  cteP A
 Cross Apply (
                Select Level1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
                      ,Level2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
                      ,Level3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
                      ,Level4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
                From  (Select Cast('<x>' + replace((Select replace(PathNm,'||','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as X 
             ) B
 Order by A.PathID

Returns



来源:https://stackoverflow.com/questions/45594542/how-to-pivot-link-and-group-a-table-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!