将单行拆分为多行 - SQL Server Management Studio 2018

Split a single row in multi rows - SQL Server Management Studio 2018

我正在尝试将单行拆分为多行,但没有成功。任何帮助或提示将不胜感激。

Table结构

结果

您使用UNPIVOT。只需将列转换为最大的类型并应用它。像这样:

DECLARE @DataSource TABLE
(
    [Dat_Cha_hOME_Away] VARCHAR(32)
   ,[Date] DATE
   ,[Half_Time_Score] VARCHAR(8)
   ,[Half Time_Goal Scored] CHAR(1)
   ,[Half Time_Goal Conceded] CHAR(1)
   ,[Half_Time_Both Teams Scored] CHAR(1)
);

INSERT INTO @DataSource 
VALUES ('2019_...italy01', '2019-03-24', '0-1', 'N', 'Y', 'N')
      ,('2019_...italy02', '2019-03-24', '2-1', 'Y', 'Y', 'Y')
SELECT *
FROM 
(
    SELECT [Dat_Cha_hOME_Away]
          ,CONVERT(VARCHAR(10), [Date], 121) AS [Date]
          ,CAST([Half_Time_Score] AS VARCHAR(10)) AS [Half_Time_Score]
          ,CAST([Half Time_Goal Scored] AS VARCHAR(10)) AS [Half Time_Goal Scored]
          ,CAST([Half Time_Goal Conceded] AS VARCHAR(10)) AS [Half Time_Goal Conceded]
          ,CAST([Half_Time_Both Teams Scored] AS VARCHAR(10)) AS [Half_Time_Both Teams Scored]
    FROM @DataSource
) DS
UNPIVOT
(
    [value] FOR [column] IN ([Date], [Half_Time_Score], [Half Time_Goal Scored], [Half Time_Goal Conceded], [Half_Time_Both Teams Scored])
) UNPVT;

基于@gotqn 的回答,我设法创建了我自己的一个并获得了理想的结果。非常感谢提供有用指南和提示的人们。

INSERT INTO Ins_Match_Details
SELECT
       [Dat_Cha_Team] + '_' + Results_Type AS [Dat_Cha_Team_Res Typ]
     , [Date]
     , [Championship]
     , [Dat_Cha_Hom_Vs_Away]
     , [Team]
     , [Site]
     , CAST(Results_Type AS NVARCHAR(20))
     , CAST(Results_Value AS NVARCHAR(10))
from
       (
              SELECT
                     [Dat_Cha_Team]
                   , [Dat_Cha_Hom_Vs_Away]
                   , [Date]
                   , [Championship]
                   , [Team]
                   , [Site]
                   , [Half Time Score]
                   , CAST([Half Time_Goal Scored] AS NVARCHAR(180)) AS [Half Time_Goal Scored]
                   , CAST([Half Time_Goal Conceded] AS NVARCHAR(180)) AS [Half Time_Goal Conceded]
                   , CAST([Half Time_Both Teams Scored] AS NVARCHAR(180)) AS [Half Time_Both Teams Scored]
                   , CAST([Half Time_Over 0.5] AS NVARCHAR(180)) AS [Half Time_Over 0.5]
                   , CAST([Half Time_Over 1.5] AS NVARCHAR(180)) AS [Half Time_Over 1.5]
                   , CAST([Half Time_Result] AS NVARCHAR(180)) AS [Half Time_Result]
                   , [Full Time Score]
                   , CAST([Full Time_Goal Scored] AS NVARCHAR(180)) AS [Full Time_Goal Scored]
                   , CAST([Full Time_Goal Conceded] AS NVARCHAR(180)) AS [Full Time_Goal Conceded]
                   , CAST([Full Time_Both Teams Scored] AS NVARCHAR(180)) AS [Full Time_Both Teams Scored]
                   , CAST([Full Time_Over 0.5] AS NVARCHAR(180)) AS [Full Time_Over 0.5]
                   , CAST([Full Time_Over 1.5] AS NVARCHAR(180)) AS [Full Time_Over 1.5]
                   , CAST([Full Time_Over 2.5] AS NVARCHAR(180)) AS [Full Time_Over 2.5]
                   , CAST([Full Time_Result] AS NVARCHAR(180)) AS [Full Time_Result]
              FROM
                     [Soccer_DatabASe].[dbo].[Imd_Match_Details]
       )
       [Imd_Match_Details] UNPIVOT (Results_Value FOR Results_Type in ([Half Time_Goal Scored]
                                                                     , [Half Time_Goal Conceded]
                                                                     , [Half Time_Both Teams Scored]
                                                                     , [Half Time_Over 0.5]
                                                                     , [Half Time_Over 1.5]
                                                                     , [Half Time_Result]
                                                                     , [Full Time_Goal Scored]
                                                                     , [Full Time_Goal Conceded]
                                                                     , [Full Time_Both Teams Scored]
                                                                     , [Full Time_Over 0.5]
                                                                     , [Full Time_Over 1.5]
                                                                     , [Full Time_Over 2.5]
                                                                     , [Full Time_Result]) ) AS ScoresUnpivot
WHERE
       [Dat_Cha_Team] + '_' + Results_Type NOT IN
       (
              SELECT
                     [Dat_Cha_Tea_Res Typ]
              FROM
                     [Soccer_Database].[dbo].[Ins_Match_Details]
       )