如何去规范化这个SQL Server表?

时间:2022-01-23 04:16:04

I have a very wide table denormalized (if you can say that) like this, the option columns go to 100+

我有一个非常宽的非规范化表(如果你能这么说的话)像这样,选项列变为100+

Year  ProductID ProductName Option1 Option2 Option3 ....Option100
-----------------------------------------------------------------
2016    1        Test1        A1      A1a     A3
2015    1        Test1        A1      A2      A2a

The problem is we have dynamic queries trying to determine the option column and then finding the option value in them

问题是,我们有动态查询试图确定选项列,然后在其中查找选项值

i.e.

即。

@SQL= 'SELECT Option' + @getOptionNum +' FROM ProductMapping

Ideally I want this converted to something like this

理想情况下,我想把它转换成这样

Year ProductID ProductName OptionName
-------------------------------------
2016   1         Test1      Option1
2016   1         Test1      Option2
2016   1         Test1      Option3
2015   1         Test1      Option1
2015   1         Test1      Option2
2015   1         Test1      Option3

OptionID OptionName OptionValue  Year
-------------------------------------
  1        Option1     A1        2016
  2        Option2     A1a       2016
  3        Option3     A3        2016
  4        Option1     A1        2015
  5        Option2     A2        2015
  6        Option3     A2a       2015

SELECT * 
FROM ProductMapping map
LEFT JOIN OptionList list ON map.OptionName = list.OptionName
                          AND map.Year = list.Year
                          AND map.OptionName = 'Option1'

The problem I am running into is how to convert that wide table into the two tables structure through queries since it's a lot of columns and rows and I cannot normalize all of that manually.

我遇到的问题是如何通过查询将宽表转换成两个表结构,因为它有很多列和行,而我不能手动将所有这些都规范化。

Yes I also understand ideally the 2nd table needs to be normalized further to keep the Option1...Option3 in a separate table and the Option1..A1 mapping in a separate table but it's a start...

是的,我也理解理想情况下,第二张表需要进一步规范化以保持选项1……选项3在单独的表格和选项1中。A1映射在一个单独的表中但是这是一个开始…

Hopefully the simple example sheds light of the following facts

希望这个简单的例子能说明以下事实

  1. Option1...100 columns need to be normalized in a separate table
  2. Option1……需要在单独的表中规范化100列
  3. The option columns to values mapping changes every year
  4. 选项列中值映射每年变化。

Any thoughts?

任何想法吗?

2 个解决方案

#1


3  

Declare @YourTable table (Year int,ProductID int,ProductName varchar(50),Option1 varchar(50),Option2 varchar(50),Option3 varchar(50))
Insert into @YourTable values
(2016,1,'Test1','A1','A1a','A3'),
(2015,1,'Test1','A1','A2','A2a')

Declare @XML xml
Set @XML = (Select * from @YourTable for XML RAW)

Select * 
 From  (
        Select Year        = r.value('@Year','int')
              ,ProductID   = r.value('@ProductID','int')
              ,ProductName = r.value('@ProductName','varchar(50)')
              ,OptionName  = Attr.value('local-name(.)','varchar(100)')
            From  @XML.nodes('/row') as A(r)
            Cross Apply A.r.nodes('./@*[local-name(.)!="ProductID"]') as B(Attr)
       ) A
 Where OptionName Like 'Option%'
 Order by Year Desc,OptionName

Select OptionID=Row_Number() over (Order By Year Desc,OptionName),* 
 From (
        Select OptionName  = Attr.value('local-name(.)','varchar(100)')
              ,OptionValue = Attr.value('.','varchar(100)') 
              ,Year        = r.value('@Year','int')
         From  @XML.nodes('/row') as A(r)
         Cross Apply A.r.nodes('./@*[local-name(.)!="ProductID"]') as B(Attr)
         --CROSS APPLY A.r.nodes('./@*') AS B(Attr)
        ) A 
 Where OptionName Like 'Option%'

Returns

返回

Year    ProductID   ProductName OptionName
2016    1           Test1       Option1
2016    1           Test1       Option2
2016    1           Test1       Option3
2015    1           Test1       Option1
2015    1           Test1       Option2
2015    1           Test1       Option3

and

OptionID    OptionName  OptionValue Year
1           Option1     A1          2016
2           Option2     A1a         2016
3           Option3     A3          2016
4           Option1     A1          2015
5           Option2     A2          2015
6           Option3     A2a         2015

EDIT

编辑

Now if you wanted a STRAIGHT Normalization

如果你想要一个直接的标准化

Declare @YourTable table (Year int,ProductID int,ProductName varchar(50),Option1 varchar(50),Option2 varchar(50),Option3 varchar(50))
Insert into @YourTable values
(2016,1,'Test1','A1','A1a','A3'),
(2015,1,'Test1','A1','A2','A2a')

Declare @XML xml
Set @XML = (Select * from @YourTable for XML RAW)


Select ID    = r.value('@id','int')                             --<<'@id' Should be YOUR PK
      ,Item  = Attr.value('local-name(.)','varchar(100)')
      ,Value = Attr.value('.','varchar(max)') 
 From  @XML.nodes('/row') as A(r)
 Cross Apply A.r.nodes('./@*[local-name(.)!="id"]') as B(Attr)  --<<'id' Should be YOUR PK
 --CROSS APPLY A.r.nodes('./@*') AS B(Attr)

Returns (the nulls would normally be your PK)

返回(null通常是您的PK)

ID      Item        Value
NULL    Year        2016
NULL    ProductID   1
NULL    ProductName Test1
NULL    Option1     A1
NULL    Option2     A1a
NULL    Option3     A3
NULL    Year        2015
NULL    ProductID   1
NULL    ProductName Test1
NULL    Option1     A1
NULL    Option2     A2
NULL    Option3     A2a

#2


1  

I would normalize the table into a single table with rows per option:

我将把表规范化为一个单独的表,每个选项都有行:

select pm.year, pm.productid, pm.productname, v.option, v.optionvalue
from productmapping pm cross apply
     (values ('option1', option1), ('option2', option2), . . .
     ) v(option, optionvalue);

I'm struggling with putting this into two tables, however. I could imagine giving ids to the options (but not the option/value pairs).

不过,我正在努力把这个问题摆到两张桌子上。我可以想象给选项(而不是选项/值对)提供id。

#1


3  

Declare @YourTable table (Year int,ProductID int,ProductName varchar(50),Option1 varchar(50),Option2 varchar(50),Option3 varchar(50))
Insert into @YourTable values
(2016,1,'Test1','A1','A1a','A3'),
(2015,1,'Test1','A1','A2','A2a')

Declare @XML xml
Set @XML = (Select * from @YourTable for XML RAW)

Select * 
 From  (
        Select Year        = r.value('@Year','int')
              ,ProductID   = r.value('@ProductID','int')
              ,ProductName = r.value('@ProductName','varchar(50)')
              ,OptionName  = Attr.value('local-name(.)','varchar(100)')
            From  @XML.nodes('/row') as A(r)
            Cross Apply A.r.nodes('./@*[local-name(.)!="ProductID"]') as B(Attr)
       ) A
 Where OptionName Like 'Option%'
 Order by Year Desc,OptionName

Select OptionID=Row_Number() over (Order By Year Desc,OptionName),* 
 From (
        Select OptionName  = Attr.value('local-name(.)','varchar(100)')
              ,OptionValue = Attr.value('.','varchar(100)') 
              ,Year        = r.value('@Year','int')
         From  @XML.nodes('/row') as A(r)
         Cross Apply A.r.nodes('./@*[local-name(.)!="ProductID"]') as B(Attr)
         --CROSS APPLY A.r.nodes('./@*') AS B(Attr)
        ) A 
 Where OptionName Like 'Option%'

Returns

返回

Year    ProductID   ProductName OptionName
2016    1           Test1       Option1
2016    1           Test1       Option2
2016    1           Test1       Option3
2015    1           Test1       Option1
2015    1           Test1       Option2
2015    1           Test1       Option3

and

OptionID    OptionName  OptionValue Year
1           Option1     A1          2016
2           Option2     A1a         2016
3           Option3     A3          2016
4           Option1     A1          2015
5           Option2     A2          2015
6           Option3     A2a         2015

EDIT

编辑

Now if you wanted a STRAIGHT Normalization

如果你想要一个直接的标准化

Declare @YourTable table (Year int,ProductID int,ProductName varchar(50),Option1 varchar(50),Option2 varchar(50),Option3 varchar(50))
Insert into @YourTable values
(2016,1,'Test1','A1','A1a','A3'),
(2015,1,'Test1','A1','A2','A2a')

Declare @XML xml
Set @XML = (Select * from @YourTable for XML RAW)


Select ID    = r.value('@id','int')                             --<<'@id' Should be YOUR PK
      ,Item  = Attr.value('local-name(.)','varchar(100)')
      ,Value = Attr.value('.','varchar(max)') 
 From  @XML.nodes('/row') as A(r)
 Cross Apply A.r.nodes('./@*[local-name(.)!="id"]') as B(Attr)  --<<'id' Should be YOUR PK
 --CROSS APPLY A.r.nodes('./@*') AS B(Attr)

Returns (the nulls would normally be your PK)

返回(null通常是您的PK)

ID      Item        Value
NULL    Year        2016
NULL    ProductID   1
NULL    ProductName Test1
NULL    Option1     A1
NULL    Option2     A1a
NULL    Option3     A3
NULL    Year        2015
NULL    ProductID   1
NULL    ProductName Test1
NULL    Option1     A1
NULL    Option2     A2
NULL    Option3     A2a

#2


1  

I would normalize the table into a single table with rows per option:

我将把表规范化为一个单独的表,每个选项都有行:

select pm.year, pm.productid, pm.productname, v.option, v.optionvalue
from productmapping pm cross apply
     (values ('option1', option1), ('option2', option2), . . .
     ) v(option, optionvalue);

I'm struggling with putting this into two tables, however. I could imagine giving ids to the options (but not the option/value pairs).

不过,我正在努力把这个问题摆到两张桌子上。我可以想象给选项(而不是选项/值对)提供id。