动态数据透视表行到列SQL Server

时间:2018-10-22 09:47:09

标签: sql sql-server

我需要根据表格中的列动态生成数据透视。

这里是看起来像这样的示例数据。

Data

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                                                                                                                                                                                                                                    |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| +--------+----------------------+--------------+----------------------------------+---------+--------------+------+------+---------+---------+-----------------------+-----------------+------------------------------+-----------------------+-----------------------------------------+--------+ |
| | Sr No  |    Invoice Date    |  Invoice No  |            Payer Name            |  IGMNo  | Container No | Size | Type | CHACode | CHAName |    Act Gatein Date    | Container Agent |    Container  Agent  Name    |    Importer Name    |        Activity Description        | Amount |          |
| +--------+----------------------+--------------+----------------------------------+---------+--------------+------+------+---------+---------+-----------------------+-----------------+------------------------------+-----------------------+-----------------------------------------+--------+ |
| |    1 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Contrainer Ground Rent Charges        |    650 |          |
| |    2 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Fuel Charges                            |    850 |        |
| |    3 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Handling and  PNR Movement Charges    |   7400 |          |
| |    4 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Weighment Charges                    |    200 |           |
| |    5 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Movement and Increase Charges |   1800 |        |
| |    6 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Documentation Charges                |    250 |           |
| |    7 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Fuel Charges                            |    850 |        |
| |    8 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Handling and  PNR Movement Charges    |   7400 |          |
| |    9 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Insurance Charges                    |    300 |           |
| |    10 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Weighment Charges                    |    200 |          |
| |    11 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Movement and Increase Charges |   1800 |       |
| |    12 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Tracking Charges            |    100 |         |
| |    13 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Documentation Charges                |    250 |          |
| |    14 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Seal Charges                            |    0 |         |
| |    15 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Tracking Charges            |    100 |         |
| |    16 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1699560  |   20 | GB   |        |        | 6-29-2018 12:10:52 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Seal Charges                            |    0 |         |
| |    17 | 7-1-2018 12:42:56 AM | MII180700001 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | DFSU1696812  |   20 | GB   |        |        | 6-28-2018 9:32:02 PM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Insurance Charges                    |    300 |          |
| |    18 | 7-1-2018 12:50:13 AM | MII180700002 | TAEWOONG LOGISTICS PVT LTD    | 2198466 | CNSU2012015  |   20 | GB   |        |        | 6-29-2018 7:40:06 AM  | AABCH5572J    | HYUNDAI MERCHANT MARINE LINE | TAEWOONGLOGISTICSPVTL | Container Tracking Charges            |    100 |           |
| |    19 | 7-1-2018 12:50:13 AM | MII180700002 | TAEWOONG LOGISTICS PVT LTD    | 2198466 | CNSU2012015  |   20 | GB   |        |        | 6-29-2018 7:40:06 AM  | AABCH5572J    | HYUNDAI MERCHANT MARINE LINE | TAEWOONGLOGISTICSPVTL | Seal Charges                            |    0 |           |
| |    20 | 7-1-2018 12:50:13 AM | MII180700002 | TAEWOONG LOGISTICS PVT LTD    | 2198466 | CNSU2012015  |   20 | GB   |        |        | 6-29-2018 7:40:06 AM  | AABCH5572J    | HYUNDAI MERCHANT MARINE LINE | TAEWOONGLOGISTICSPVTL | Value Added Charges                    |   1000 |          |
| |    21 | 7-1-2018 12:50:13 AM | MII180700002 | TAEWOONG LOGISTICS PVT LTD    | 2198466 | CNSU2012015  |   20 | GB   |        |        | 6-29-2018 7:40:06 AM  | AABCH5572J    | HYUNDAI MERCHANT MARINE LINE | TAEWOONGLOGISTICSPVTL | Handling and  PNR Movement Charges    |   6850 |           |
| |    22 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | FCIU4016495  |   20 | GB   |        |        | 6-29-2018 11:35:29 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Movement and Increase Charges |   1800 |       |
| |    23 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | FCIU4016495  |   20 | GB   |        |        | 6-29-2018 11:35:29 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Fuel Charges                            |    850 |       |
| |    24 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | FCIU4016495  |   20 | GB   |        |        | 6-29-2018 11:35:29 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Insurance Charges                    |    300 |          |
| |    25 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | TEMU5684719  |   20 | GB   |        |        | 6-29-2018 1:00:46 AM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Movement and Increase Charges |   1800 |       |
| |    26 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | TEMU5684719  |   20 | GB   |        |        | 6-29-2018 1:00:46 AM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Container Tracking Charges            |    100 |         |
| |    27 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | TEMU5684719  |   20 | GB   |        |        | 6-29-2018 1:00:46 AM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Handling and  PNR Movement Charges    |   7400 |         |
| |    28 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | TEMU5684719  |   20 | GB   |        |        | 6-29-2018 1:00:46 AM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Insurance Charges                    |    300 |          |
| |    29 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | TEMU5684719  |   20 | GB   |        |        | 6-29-2018 1:00:46 AM  | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Seal Charges                            |    0 |         |
| |    30 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | FCIU4016495  |   20 | GB   |        |        | 6-29-2018 11:35:29 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Documentation Charges                |    250 |          |
| |    31 | 7-1-2018 1:55:51 AM  | MII180700003 | PROMEDIA TRADING PRIVATE LIMITED | 2198105 | FCIU4016495  |   20 | GB   |        |        | 6-29-2018 11:35:29 AM | AACCT8966D    | T S LINES AGENCY            | PROMEDIATRADINGPRIVAT | Handling and  PNR Movement Charges    |   7400 |         |
| +--------+----------------------+--------------+----------------------------------+---------+--------------+------+------+---------+---------+-----------------------+-----------------+------------------------------+-----------------------+-----------------------------------------+--------+ |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

我需要透视活动描述列,这些列可以动态添加到我的FCL_Parent表中。

因此,为了从该列动态获取值,我使用了下面的代码,

 DECLARE @Columns VARCHAR(MAX) = ''
 SET @Columns = (SELECT DISTINCT Quotename([Activity Description]) + ',' 
             FROM  [dbo].[FCLParent]  
             FOR xml path('')) 
 SET @Columns = LEFT (@Columns, Len(@Columns) - 1)  

以下是我的要求,

我需要使用不同的容器编号,活动说明作为数据透视列,金额列也作为数据透视图中的值,而我的其他必填列也是如此。

Output

为了实现这一点,我在下面的代码中使用了

  SET @sql = 'select [Payer Name],[Container No],[Size],[Type],
  [CHAName],[Act Gatein Date], [Container  Agent  Name],[Importer Name],'         + @Columns 
   + 'FROM [dbo].[FCLParent]
   pivot (max([Amount]) for [Activity Description] in (' 
       + @Columns + '))pv' 
   EXEC(@sql)

我没有得到预期的枢轴列。其中缺少一些值。例如枢轴中缺少一些可用的费用。我也不确定查询是对还是错。

谢谢

1 个答案:

答案 0 :(得分:0)

HI尝试使用基于您的示例数据编写的动态sql。我可能认为此代码肯定会帮助您 以下是示例数据

IF OBJECT_ID('tempdb..#TempData')IS NOT NULL
DROP TABLE #TempData
;WITH CTE(SrNo  ,    InvoiceDate    ,  InvoiceNo  ,            PayerName            ,  IGMNo  , ContainerNo , Size , [Type] , CHACode , CHAName ,    ActGateinDate    , ContainerAgent ,    ContainerAgentName    ,    ImporterName    ,        ActivityDescription        , Amount          )
AS
(
SELECT 1 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM'  , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Contrainer Ground Rent Charges'   , 650      UNION ALL
SELECT 2 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM'  , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Fuel Charges'                            , 850    UNION ALL
SELECT 3 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM'  , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Handling and  PNR Movement Charges'    ,7400      UNION ALL 
SELECT 4 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM'  , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Weighment Charges'                    ,200       UNION ALL 
SELECT 5 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Container Movement and Increase Charges' ,1800    UNION ALL 
SELECT 6 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Documentation Charges'                ,250       UNION ALL 
SELECT 7 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Fuel Charges'                           ,850     UNION ALL
SELECT 8 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Handling and  PNR Movement Charges'    ,7400      UNION ALL 
SELECT 9 ,  '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Insurance Charges'                    ,300       UNION ALL 
SELECT 10 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Weighment Charges'                    ,200       UNION ALL
SELECT 11 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM' ,  'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Container Movement and Increase Charges',1800    UNION ALL
SELECT 12 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM' ,  'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Container Tracking Charges'            ,100      UNION ALL
SELECT 13 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM' ,  'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Documentation Charges'                ,250       UNION ALL
SELECT 14 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM' ,  'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Seal Charges'                            ,0      UNION ALL
SELECT 15 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Container Tracking Charges'            ,100      UNION ALL
SELECT 16 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1699560' ,   20 , 'GB'   ,NULL  ,NULL , '6-29-2018 12:10:52 AM' , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Seal Charges'                          ,0      UNION ALL
SELECT 17 , '7-1-2018 12:42:56 AM' , 'MII180700001' , 'PROMEDIA TRADING PRIVATE LIMITED' , 2198105 , 'DFSU1696812' ,   20 , 'GB'   ,NULL  ,NULL , '6-28-2018 9:32:02 PM'  , 'AACCT8966D'    , 'T S LINES AGENCY'  , 'PROMEDIATRADINGPRIVAT' , 'Insurance Charges'                    ,300       
)
SELECT * INTO #TempData FROM CTE

Sql脚本

    DECLARE @Columns VARCHAR(MAX) ='',
            @Columns2 VARCHAR(MAX) ='',
            @Sql nvarchar (max)=''

    SELECT @Columns=STUFF((SELECT DISTINCT ',',+ QUOTENAME([ActivityDescription] )
                    FROM  #TempData FOR XML PATH ('')),1,1,'')

    SELECT @Columns
    SELECT @Columns2=STUFF((SELECT DISTINCT ',',+ 'MAX('+QUOTENAME([ActivityDescription] ) +') AS '+QUOTENAME([ActivityDescription] )
                    FROM  #TempData FOR XML PATH ('')),1,1,'')

    SET @sql = 'SELECT  [PayerName],
                        [ContainerNo],
                        [Size],
                        [Type],
                        [CHAName],
                        [ActGateinDate], 
                        [ContainerAgentName],
                        [ImporterName],'+ @Columns2+
                'FROM
                (SELECT * FROM  #TempData) AS Src
                PIVOT(MAX([Amount]) FOR [ActivityDescription] IN ('+ @Columns +')
                )pv  
                GROUP BY [PayerName],[ContainerNo],[Size],[Type],
                        [CHAName],[ActGateinDate], [ContainerAgentName],
                        [ImporterName]'

    PRINT(@sql)
    EXEC (@sql)