0

我需要帮助来实现以下输出。我有一张桌子

declare @tblLevels  table(
levelsID                INT IDENTITY(1,1),
levelNumber             INT,
levelDesc               VARCHAR(100))
insert into @tblLevels(levelNumber,levelDescription)
 values 
(1,'L1D1'),
(1,'L1D2'),
(1,'L1D3'),
(1,'L1D4'),
(2,'L2D1'),
(2,'L2D2'),
(2,'L2D3'),
(2,'L2D4'),
(3,'L3D1'),
(3,'L3D2'),
(3,'L3D3')

select * from @tblLevels

levelsID    levelNumber levelDesc
========================================
1               1           L1D1
2               1           L1D2
3               1           L1D3
4               1           L1D4
5               2           L2D1
6               2           L2D2
7               2           L2D3
8               2           L2D4
9               3           L3D1
10              3           L3D2
11              3           L3D3

我有另一个表(@tblLevelSpreadOut),其结构如下

declare @tblLevelSpreadOut  table(
levelSpreadOutID                INT IDENTITY(1,1),
levelNumber1                    INT,
levelDesc1                      VARCHAR(100),
levelNumber2                    INT,
levelDesc2                      VARCHAR(100),
levelNumber3                    INT,
levelDesc3                      VARCHAR(100))

它需要从前一个表(@tblLevels)中填充,这样输出是

    select  levelNumber1, levelDesc1,  levelNumber2 ,levelDesc2 , levelNumber3 ,levelDesc3 
from @tblLevelSpreadOut

 levelNumber1 levelDesc1  levelNumber2 levelDesc2  levelNumber3 levelDesc3
============================================================================
1   L1D1    2   L2D1    3   L3D1
1   L1D1    2   L2D1    3   L3D2
1   L1D1    2   L2D1    3   L3D3
1   L1D1    2   L2D2    3   L3D1
1   L1D1    2   L2D2    3   L3D2
1   L1D1    2   L2D2    3   L3D3
1   L1D1    2   L2D3    3   L3D1
1   L1D1    2   L2D3    3   L3D2
1   L1D1    2   L2D3    3   L3D3
1   L1D1    2   L2D4    3   L3D1
1   L1D1    2   L2D4    3   L3D2
1   L1D1    2   L2D4    3   L3D3
1   L1D2    2   L2D1    3   L3D1
1   L1D2    2   L2D1    3   L3D2
1   L1D2    2   L2D1    3   L3D3
1   L1D2    2   L2D2    3   L3D1
1   L1D2    2   L2D2    3   L3D2
1   L1D2    2   L2D2    3   L3D3
1   L1D2    2   L2D3    3   L3D1
1   L1D2    2   L2D3    3   L3D2
1   L1D2    2   L2D3    3   L3D3
1   L1D2    2   L2D4    3   L3D1
1   L1D2    2   L2D4    3   L3D2
1   L1D2    2   L2D4    3   L3D3
1   L1D3    2   L2D1    3   L3D1
1   L1D3    2   L2D1    3   L3D2
1   L1D3    2   L2D1    3   L3D3
1   L1D3    2   L2D2    3   L3D1
1   L1D3    2   L2D2    3   L3D2
1   L1D3    2   L2D2    3   L3D3
1   L1D3    2   L2D3    3   L3D1
1   L1D3    2   L2D3    3   L3D2
1   L1D3    2   L2D3    3   L3D3
1   L1D3    2   L2D4    3   L3D1
1   L1D3    2   L2D4    3   L3D2
1   L1D3    2   L2D4    3   L3D3

感谢所有回复的人以及所有查看此问题的人。

4

1 回答 1

-1

你想要一个完整的外部连接吗?

尝试

select  
 t.levelNumber1, 
 t.levelDesc1,  
 t.levelNumber2 ,
 t.levelDesc2 , 
 t.levelNumber3 ,
 t.levelDesc3 
from 
 @tblLevelSpreadOut t outer join
 @tblLevelSpreadOut t2
order by
 t.levelDesc1,  
 t.levelDesc2 , 
 t.levelDesc3 

此外,考虑接受您的问题的答案。你会从社区中得到更好的回应。

于 2012-10-04T21:05:00.617 回答