动态透视行到列-SQL 服务器
Posted
技术标签:
【中文标题】动态透视行到列-SQL 服务器【英文标题】:Dynamic Pivot rows to Columns-SQL server 【发布时间】:2018-10-22 09:47:08 【问题描述】:我需要根据表中的列动态生成数据透视表。
这是看起来像这样的示例数据。
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)
我没有得到预期的数据透视列。其中缺少一些值。前任。枢轴中缺少一些可用的费用。我也不确定查询是对还是错。
谢谢
【问题讨论】:
添加您的样本数据以及您的预期结果 我一直觉得,对于这种性质的查询,其中枢轴列不断变化,枢轴是错误的方式 - 列应该是相对固定/不变的,并且行呈现变化.. 数据库 101 嗨@CaiusJard,原始表中的两列都会不断变化。但是,我的列也将添加适当的枢轴值...... 嗨@Sreenu131,我在链接中添加了数据和输出,仅供参考,我需要在我的数据中调整活动描述列。如果您需要任何东西,请告诉我。 @krishna31 嗨,您编写的枢轴代码似乎是正确的,但我不确定该代码是否能满足您的要求。但是我可以让你的源数据表 Sql server 脚本与有限的 crate 行和插入脚本,以便我可以尝试。我无法通过查看图像生成插入脚本 【参考方案1】:您好,试试这个根据您的示例数据编写的动态 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)
【讨论】:
谢谢,我尝试使用您上面的代码,但收到错误,因为在 pivot 附近有无效的语法。使用以下代码,SET sql = 'select [Container No],[Payer Name],[Size],[Type], [CHAName],[Act Gatein Date], [Container Agent Name],[Importer Name],' + Columns + ' FROM [dbo].[FCLParent] Group by [Container No] pivot (max([Amount]) for [Activity Description] in (' + Columns + ')' EXEC(sql)。你能纠正我吗, 使用 groupby 时出现此错误。 它在 AS 附近抛出无效语法以上是关于动态透视行到列-SQL 服务器的主要内容,如果未能解决你的问题,请参考以下文章