如何在两个 table 或一个详细的 table 中规范化 sql table

how to Normalize the sql table in two tables or one detailed table

我希望对 SQL 规范化有一点帮助。我有一个 table 如下:

tbl_code

Ucode, desc, code1, code2, code3
1      aa    1      1      1
2      bb    1      2      2
3      cc    1      1      1

现在我想让这个 table 标准化为:

ucode, desc, code,  value
1      aa    code1  1     
2      bb    code1  1    
3      cc    code1  1
1      aa    code2  1

等等...

如何在 SQL 中执行此操作?有人可以帮我吗?

这应该可以让你继续,但你可能会在现实生活中以 NULL 等结束?

SELECT
    ucode,
    [desc],
    'code1' AS code,
    code1 AS [value]
FROM
    tbl_code
UNION ALL
SELECT
    ucode,
    [desc],
    'code2' AS code,
    code2 AS [value]
FROM
    tbl_code
UNION ALL
SELECT
    ucode,
    [desc],
    'code3' AS code,
    code3 AS [value]
FROM
    tbl_code;

还有一个选择

Select A.UCode
      ,A.[Desc]
      ,B.*
 From tbl_code A
 Cross Apply (
               values ('code1',code1)
                     ,('code2',code2)
                     ,('code3',code3)
             ) B(code,value)

Updated - Dynamic without using Dynamic SQL

Select A.Ucode
      ,A.[desc]
      ,C.*
 From  tbl_code A
 Cross Apply ( values (cast((Select A.* for XML RAW) as xml))) B(XMLData)
 Cross Apply (
                Select Code  = a.value('local-name(.)','varchar(100)')
                      ,Value = a.value('.','varchar(max)') 
                 From  B.XMLData.nodes('/row')  as C1(n)
                 Cross Apply C1.n.nodes('./@*') as C2(a)
                 Where a.value('local-name(.)','varchar(100)') not in ('Ucode','desc')
                 -- {or you can use} Where a.value('local-name(.)','varchar(100)') like 'code%'
             ) C

您可以像这样使用 UNPIVOT:

SELECT distinct ucode, desc, code, value
FROM 
(
SELECT *
FROM tbl_code
) AS cp
UNPIVOT (value
FOR code IN (code1, code2, code3)) AS up
Order by code;

输出将如下所示: