0

我有下表用于自行车测量,我在 csv 文件中拥有并希望转置并导入到我的 SQL Server 数据库中的表中。

Size             XS S   M   L   XL
Frame Reach 365mm   385mm   405mm   425mm   445mm
Frame Stack 495mm   515mm   540mm   565mm   590mm
Stem Length 90mm    90mm    90mm    90mm    90mm
Seat Tube Length    505mm   525mm   551mm   577mm   603mm
Top Tube Length 481mm   504mm   530mm   557mm   582mm
B-B Drop             72mm   72mm    72mm    72mm    72mm
Chain-Stay Length   395mm   395mm   395mm   395mm   395mm
Seat-Tube Angle 77° 77° 77° 77° 77°
Head-Tube Angle 70.25°  71.5°   72° 72.5°   72.5°
Fork Rake            50mm   50mm    45mm    45mm    45mm
Trail            69mm   61mm    63mm    60mm    60mm
Front-Center    573mm   589mm   608mm   631mm   659mm
Wheelbase            957mm  973mm   992mm   1016mm  1043mm
Stand-Over Height   752mm   772mm   797mm   822mm   847mm
Head-Tube Length    99mm    115mm   139mm   163mm   189mm
Seat Post Setback   12.5mm  12.5mm  12.5mm  12.5mm  12.5mm
Seat-Post Length    350mm   350mm   350mm   350mm   350mm
Crank Length    167.5mm 170mm   172.5mm 172.5mm 175mm

所以对于上表,我希望它看起来像这样:

Size    Frame Reach Frame Stack Stem Length -18° Horizonta  Seat Tube Length    Top Tube Length B-B Drop    Chain-Stay Length   Seat-Tube Angle Head-Tube Angle Fork Rake   Trail   Front-Center    Wheelbase   Stand-Over Height   Head-Tube Length    Seat Post Setback   Seat-Post Length    Crank Length
XS  365mm   495mm   90mm    505mm   481mm   72mm    395mm   77° 70.25°  50mm    69mm    573mm   957mm   752mm   99mm    12.5mm  350mm   167.5mm
S   385mm   515mm   90mm    525mm   504mm   72mm    395mm   77° 71.5°   50mm    61mm    589mm   973mm   772mm   115mm   12.5mm  350mm   170mm
M   405mm   540mm   90mm    551mm   530mm   72mm    395mm   77° 72° 45mm    63mm    608mm   992mm   797mm   139mm   12.5mm  350mm   172.5mm
L   425mm   565mm   90mm    577mm   557mm   72mm    395mm   77° 72.5°   45mm    60mm    631mm   1016mm  822mm   163mm   12.5mm  350mm   172.5mm
XL  445mm   590mm   90mm    603mm   582mm   72mm    395mm   77° 72.5°   45mm    60mm    659mm   1043mm  847mm   189mm   12.5mm  350mm   175mm
XL

我在这个论坛中找到了 pivot / unpivot 功能,但我无法让它为我工作。请注意,我首先将 csv 导入到一个临时表中,并从数据中删除了“mm”和“°”。

任何帮助将不胜感激。

4

1 回答 1

1

对于一次性解决方案,我建议在 Excel 中打开 csv 文件,然后在那里执行转置。

如果您真的想使用 T-SQL 执行此操作,则必须先 UNPIVOT 数据,然后将其 PIVOT 回转置布局,但是,这仅适用于您在数据列上使用相同的数据类型 ("浮动”,例如)。假设您的临时表名为“Temp”,并具有以下列:“Dimension”、“XS”、“S”、“M”、“L”和“XL”:

反透视:

SELECT Dimension, Size, Value
FROM Temp
UNPIVOT (Value FOR Size IN (XS, S, M, L, XL)) AS unpvt;

将此输出放入一个新的临时表 (temp2),然后像这样使用 PIVOT:

SELECT Size, FrameReach, FrameStack, StemLength, SeatTubeLength, [...]
FROM Temp2
PIVOT (
    MAX(Value) FOR Dimension IN ('Frame Reach', 'Frame Stack', 'Stem Length', 'Seat Tube Length', [...])
    ) AS Pvt
于 2013-01-28T16:20:17.593 回答