在SQL Server中将行转换为列

时间:2016-05-25 03:25:55

标签: sql-server

我有一个返回结果的查询:

Location ID Location Code   Location Name   Division Code   sSourceCode Division Name   Business Unit Code  Business Unit Name
83806   D4009   Vanier Mitsubishi Fuso  SALES_01    SALES_01    Sales_01    SALES DEPARTMENT    Fuso Vehicle Sales Department
83806   D4009   Vanier Mitsubishi Fuso  Parts_A0    Parts_A0    Parts District A0   PARTS DEPARTMENT    Fuso Parts Department
83806   D4009   Vanier Mitsubishi Fuso  SERVICE_C1  SERVICE_C1  Service Division C1 SERVICE DEPARTMENT  Fuso Service Department

我希望得到如下结果:

Location ID    Location Code    Location Name   Parts Division Code   Parts sSourceCode  Parts Division Name    Sales Division Code   Sales Division Name       Sales Business Unit Code    Sales Business Unit Name    Parts Business Unit Code    Parts Business Unit Name

因此,对于每个locationID,我只需要一行,其各自的部分/服务/销售部门/业务部门代码和名称显示在同一行中作为单独的列....不相信我可以使用Pivoting,如果可能的话,有人可以指出我正确的方向,或者给我一些如何实现这一目标的例子。

2 个答案:

答案 0 :(得分:0)

如果没有透视,你需要自己多次将表连接到表中,尽管从你发布的数据看起来行数(部分)不是静态的,这会让你再次回到数据透视。

我建议你做的是将操作拆分为两个,其中第一个语句选择将被转换为列的行(后面有一个id用于链接):

ID 
Parts sSourceCode  
Parts Division Name    
Sales Division Code   
Sales Division Name       
Sales Business Unit Code    
Sales Business Unit Name 

并对该数据使用Pivoting,然后再次使用相同的表将此数据连接到

SELECT DISTINCT Location ID Location Code   Location Name   Division Code
From Table
Inner join PreviousData

答案 1 :(得分:0)

我认为我们可以使用CROSS / OUTER APPLY和FOR XML根据位置ID /位置代码将多行转换为单行。

这是为测试结果而生成的最终查询和临时表。

  

创建表格并填充一些记录

 CREATE TABLE TempLocations
(
    LocationID INT,
    LocationCode VARCHAR(20),
    LocationName VARCHAR(30),
    DivisionCode VARCHAR(100),
    SourceCode VARCHAR(100),
    DivisionName VARCHAR(100),
    BusinessUnitCode VARCHAR(100),
    BusinessUnitName VARCHAR(100)

)

INSERT INTO TempLocations
VALUES
(83806, 'D4009','Vanier Mitsubishi Fuso',  'SALES_01',    'SALES_01' ,'Sales_01',  'SALES DEPARTMENT',    'Fuso Vehicle Sales Department'),
(83806,  'D4009',   'Vanier Mitsubishi Fuso',  'Parts_A0',    'Parts_A0',    'Parts District A0',   'PARTS DEPARTMENT',    'Fuso Parts Department'),
(83806,   'D4009',   'Vanier Mitsubishi Fuso',  'SERVICE_C1',  'SERVICE_C1',  'Service Division C1', 'SERVICE DEPARTMENT',  'Fuso Service Department'),
(83807, 'D4008','Borusia',  'SALES_01',    'SALES_01' ,'Sales_01',  'SALES DEPARTMENT',    'Borusia Sales Department'),
(83807,  'D4008',   'Borusia',  'Parts_A0',    'Parts_A0',    'Parts District A0',   'PARTS DEPARTMENT',    'Borusia Parts Department'),
(83807,   'D4008',   'Borusia',  'SERVICE_C1',  'SERVICE_C1',  'Service Division C1', 'SERVICE DEPARTMENT',  'Borusia Service Department')
  

获取地点数据的最终查询

SELECT 
 DISTINCT
    T.LocationID,
    T.LocationCode,
    T.LocationName,
    TDIV.DivisionCode,
    TS.SourceCode,
    TDIVName.DivisionName,
    TB.BusinessUnitCode,
    TBName.BusinessUnitName
FROM TempLocations AS T
CROSS APPLY
(
SELECT 
Stuff(
        (
            SELECT DISTINCT N', ' + DivisionCode 
            FROM TempLocations AS TDIV
            WHERE TDIV.LocationID=T.LocationID FOR XML PATH(''),TYPE
        )
    .value('text()[1]','nvarchar(max)'),1,2,N''
    ) AS DivisionCode
) AS TDIV
CROSS APPLY
(
SELECT 
Stuff(
        (
            SELECT DISTINCT N', ' + SourceCode 
            FROM TempLocations AS TDIV
            WHERE TDIV.LocationID=T.LocationID FOR XML PATH(''),TYPE
        )
    .value('text()[1]','nvarchar(max)'),1,2,N''
    ) AS SourceCode
) AS TS
CROSS APPLY
(
SELECT 
Stuff(
        (
            SELECT DISTINCT N', ' + DivisionName 
            FROM TempLocations AS TDIV
            WHERE TDIV.LocationID=T.LocationID FOR XML PATH(''),TYPE
        )
    .value('text()[1]','nvarchar(max)'),1,2,N''
    ) AS DivisionName
) AS TDIVName
CROSS APPLY
(
SELECT 
Stuff(
        (
            SELECT DISTINCT N', ' + BusinessUnitCode 
            FROM TempLocations AS TDIV
            WHERE TDIV.LocationID=T.LocationID FOR XML PATH(''),TYPE
        )
    .value('text()[1]','nvarchar(max)'),1,2,N''
    ) AS BusinessUnitCode
) AS TB
CROSS APPLY
(
SELECT 
Stuff(
        (
            SELECT DISTINCT N', ' + BusinessUnitName 
            FROM TempLocations AS TDIV
            WHERE TDIV.LocationID=T.LocationID FOR XML PATH(''),TYPE
        )
    .value('text()[1]','nvarchar(max)'),1,2,N''
    ) AS BusinessUnitName
) AS TBName

-- Drop table
DROP TABLE TempLocations