2011-10-31 105 views
0

我有一个包含以下列的遗留表。将多列转换为行

Create Table test 
(id varchar(50) 
, code1 varchar(50) 
, codeDesc1 varchar(50) 
, code2 varchar(50) 
, codeDesc2 varchar(50) 
, code3 varchar(50) 
, codeDesc3 varchar(50) 
, code4 varchar(50) 
, codeDesc4 varchar(50) 
, code5 varchar(50) 
, codeDesc5 varchar(50) 
, code6 varchar(50) 
, codeDesc6 varchar(50) 
) 

insert into test (id, code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6) 
values ('1','100.1', 'first code','100.2', 'Second code','100.3', 'Third code','100.4', 'Fourth code','100.5', 'Fifth code','100.6', 'Sixth code') 

insert into test (id, code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6) 
values ('2','100.1', 'first code','100.2', 'Second code','100.3', 'Third code','100.4', 'Fourth code','100.5', 'Fifth code','100.6', 'Sixth code') 

select * from test 

回报如下:

id, code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6 

1,100.1, first code,100.2, Second code,100.3, Third code,100.4, Fourth code,100.5, Fifth code,100.6, Sixth code 

2,100.1, first code,100.2, Second code,100.3, Third code,100.4, Fourth code,100.5, Fifth code,100.6, Sixth code 

,但我想找回数据,如下图所示:

ID, Code, Description 

1, 100.1, First Code 
1, 100.2, Second Code 
1, 100.3, Third Code 
1, 100.4, Fourth Code 
1, 100.5, Fifth Code 
1, 100.6, Sixth Code 
2, 100.1, First Code 
2, 100.2, Second Code 
2, 100.3, Third Code 
2, 100.4, Fourth Code 
2, 100.5, Fifth Code 
2, 100.6, Sixth Code 

谁能帮助我该怎么做,好吗?

我试过UNPIVOT,但没有给我确切的结果,我一直在寻找。这里是我使用的查询,但需要进行调整才能获得准确的结果,以便找到困难时间。

SELECT Id, FieldCode , FieldValue 
    FROM 
    (
     SELECT id, code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6 
     FROM test 
    ) MyTable 
    UNPIVOT 
    (FieldValue FOR FieldCode IN (code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6))AS MyUnPivot 

感谢您的帮助提前。

+2

这是什么? SQL或类似的东西?请正确标记 –

+0

你想让数据库以这种方式输出吗?或者你想让一些编程语言像这样打印出来吗? – user482594

+0

感谢您的回复。是的,我需要查询来完成转换,因为我必须获取此信息并更新另一个表。我知道我可以使用游标,但我不喜欢游标,并且我知道有一种方法可以使用PIVOT/UNPIVOT命令来执行此操作,但缺少某些内容,我不会看到它。 – Vincy

回答

1

不要介意,找到答案。感谢这个美好的曼加勒的网站http://mangalpardeshi.blogspot.com/2009/04/unpivot-multiple-columns.html

这里是我的查询解决我的问题:我在这里张贴所以,如果有人谁看起来

SELECT Id, FieldCode, FieldValue 
    FROM 
    (
     SELECT id, code1, codeDesc1, code2, codeDesc2, code3, codeDesc3, code4, codeDesc4, code5, codeDesc5, code6, codeDesc6 
     FROM test 
    ) MyTable 

    UNPIVOT 
    (FieldCode FOR FieldCodes IN (code1, code2, code3, code4, code5, code6))AS CODE 

    UNPIVOT 
    (FieldValue FOR FieldValues IN (codeDesc1, codeDesc2, codeDesc3, codeDesc4, codeDesc5, codeDesc6))AS FieldValues 

WHERE RIGHT(FieldCodes,1) = RIGHT(FieldValues,1) 

现在的伎俩部分,以获得正确的结果回是在最后的WHERE子句取决于您使用的列名称。因此,在尝试使用它之前尝试理解代码。

此外,它是一个好主意,以检查您在返回原始列的初始查询中的ISNULL。因为一旦处于UNPIVOT模式,您不需要更改就可以检查它,并可能产生错误。

祝你好运!