0

因此,我找到了一个动态数据透视代码,并试图将其应用于我的需要,但我得到了错误:

消息 8156,级别 16,状态 1,第 52 行 为“PVTTable”多次指定了“:Id”列。消息 4104,级别 16,状态 1,第 45 行 无法绑定多部分标识符“T2.PartNumber”。

这是我的代码:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME([Designation <en>])
FROM (SELECT DISTINCT [Designation <en>] FROM [HellaPim].[dbo].[MasterData.Countries]) AS Country

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT T2.[PartNumber] as ArtNr, ' + @ColumnName + '
    FROM [HellaPim].[dbo].[Article.ArticleCarPark] T1 WITH (NOLOCK)
            LEFT OUTER JOIN [HellaPim].[dbo].[Article.Articles] T2 WITH (NOLOCK)
                ON T2.[ArticleID]=T1.[Article:Link]
            LEFT OUTER JOIN [HellaPim].[dbo].[MasterData.Countries] T3 WITH (NOLOCK)
                ON T3.[CountryCode]=T1.[Country:Link]
    PIVOT(SUM(T1.[PassengerCars]) 
          FOR T3.[Designation <en>] IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

数据透视代码在静态表上工作正常,当我尝试将表链接到其他表时出现问题。确实 [:Id] 存在于我的所有 3 个表中,但我不使用它们。

关于我缺少什么的任何提示?

谢谢

4

2 回答 2

1

我刚刚对您的动态 sql 查询进行了一些更改,它可能会对您有所帮助

   DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
   DECLARE @ColumnName AS NVARCHAR(MAX)


    SET @DynamicPivotQuery = 
              N'SELECT ArtNr, ' + @ColumnName + ' 
                FROM
                (
                SELECT T2.[PartNumber] as ArtNr,
                       T3.[Designation <en>]
                FROM [HellaPim].[dbo].[Article.ArticleCarPark] T1 WITH (NOLOCK)
                    LEFT OUTER JOIN [HellaPim].[dbo].[Article.Articles] T2 WITH (NOLOCK)
                        ON T2.[ArticleID]=T1.[Article:Link]
                    LEFT OUTER JOIN [HellaPim].[dbo].[MasterData.Countries] T3 WITH (NOLOCK)
                        ON T3.[CountryCode]=T1.[Country:Link]
                ) AS Src
                PIVOT
                (
                SUM(Src.[PassengerCars]) FOR Src.[Designation <en>] IN (' + @ColumnName + ')
                ) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery
于 2019-03-22T13:14:21.297 回答
0

抱歉回复晚了,这是我原始语法的打印:

Msg 8156, Level 16, State 1, Line 8
The column ':Id' was specified multiple times for 'PVTTable'.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "T2.PartNumber" could not be bound.
SELECT T2.[PartNumber] as ArtNr, [(Democratic Republic of the) Congo],[(Republic of) Côte d'Ivoire],[Afghanistan],[Aland Islands],[Albania],[Algeria],[American Samoa],[Andorra],[Angola],[Anguilla],[Antarctica],[Antigua],[Argentina],[Armenia],[Aruba],[Aserbaidjan],[Australia],[Austria],[Bahamas],[Bahrain],[Bangladesh],[Barbados],[Belgium],[Belize],[Belorussia],[Benin],[Bermuda],[Bhutan],[Bolivia],[Bosnia-Herzegovina],[Botswana],[Bouvet Islands],[Brazil],[British Indian Ocean Territory],[British Virgin Islands],[Brunei],[Bulgaria],[Burkina Faso],[Burma],[Burundi],[California],[Cambodia],[Cameroon],[Canada],[Cape Verde],[Casakhstan],[Cayman Islands],[Central African Republic],[Chad],[Chile],[China],[Christmas Island],[Cocos (Keeling) Islands],[Columbia],[Comores],[Congo],[Cook Islands],[Costa Rica],[Croatia],[Cuba],[Cyprus],[Czech Republic],[Czechoslovakia],[Democratic People's Republic of Korea],[Denmark],[Djibouti],[Dominica],[Dominican Republic],[Ecuador],[Egypt],[El Salvador],[Equatorial Guinea],[Eritrea],[Estonia],[Ethiopia],[Faeroes],[Falkland Islands],[Fidji],[Finland],[France],[French Guyana],[French Polynesia],[French Southern and Antarctic Territories],[Gabon],[Gambia],[Georgia],[Germany],[Ghana],[Gibraltar],[Great Britain],[Greece],[Greenland],[Grenada],[Guadeloupe],[Guam],[Guatemala],[Guernsey],[Guinea],[Guinea-Bissau],[Guyana],[Haiti],[Heard and McDonald Islands],[Honduras],[Hong Kong],[Hungary],[Iceland],[India],[Indonesia],[Iran],[Iraq],[Ireland],[Isle of Man],[Israel],[Italy],[Jamaica],[Japan],[Jersey],[Jordan],[Kenya],[Kirgistan],[Kiribati],[Korea (Republic)],[Kuwait],[Laos],[Latvia],[Lebanon],[Lesotho],[Liberia],[Libya],[Liechtenstein],[Lithuania],[Luxemburg],[Macau],[Macedonia],[Madagascar],[Malawi],[Malaysia],[Maldives],[Mali],[Malta],[Marshall Islands],[Martinique],[Mauritania],[Mauritius],[Mayotte],[Mexico],[Micronesia],[Monaco],[Mongolia],[Montenegro],[Montserrat],[Morocco],[Mozambique],[Namibia],[Nauru],[Nepal],[Netherlands],[Netherlands Antilles],[New Caledonia],[New Zealand],[Nicaragua],[Niger],[Nigeria],[Niue],[Norfolk Island],[Northern Mariana Islands],[Norway],[Oman],[Pakistan],[Palau],[Palestine],[Panama],[Papua New Guinea],[Paraguay],[Peru],[Philippines],[Pitcairn Islands],[Poland],[Portugal],[Puerto Rico],[Qatar],[Republic of Moldova],[Reunion],[Rumania],[Russia],[Rwanda],[Saint Barthélemy],[Saint Helena],[Saint Kitts and Nevis],[Saint Martin],[Saint Pierre and Miquelon],[Salomon Islands],[Samoa Islands],[San Marino],[Santa Lucia],[Sao Tome and Principe],[Saudi Arabia],[Senegal],[Serbia],[Serbia and Montenegro],[Seychelles],[Sierra Leone],[Singapore],[Slovenia],[Slowakian Republic],[Somalia],[South Africa],[South Georgia and the South Sandwich Islands],[South Sudan],[Sowjet Union (former)],[Spain],[Sri Lanka],[St. Vincent and Grenadines],[Sudan],[Surinam],[Svalbard and Jan Mayen],[Swaziland],[Sweden],[Switzerland],[Syria],[Taiwan],[Tajikistan],[Tanzania],[Thailand],[Timor-Leste],[Togo],[Tokelau],[Tonga],[Trinidad and Tobago],[Tunisia],[Turkey],[Turkmenistan],[Turks and Caicos Islands],[Tuvalu],[Uganda],[Ukraine],[United Arab Emirates],[United States Minor Outlying Islands],[United States of America],[United States Virgin Islands],[Uruguay],[Usbekistan],[Vanuatu],[Vatican City],[Venezuela],[Vietnam],[Wallis and Futuna],[Western Sahara],[Yemen (Democratic Peoples' Republic)],[Yugoslawia],[Zambia],[Zimbabwe]
    FROM [HellaPim].[dbo].[Article.ArticleCarPark] T1 WITH (NOLOCK)
            LEFT OUTER JOIN [HellaPim].[dbo].[Article.Articles] T2 WITH (NOLOCK)
                ON T2.[ArticleID]=T1.[Article:Link]
            LEFT OUTER JOIN [HellaPim].[dbo].[MasterData.Countries] T3 WITH (NOLOCK)
                ON T3.[CountryCode]=T1.[Country:Link]
    PIVOT(SUM(T1.[PassengerCars]) 
          FOR T3.[Designation <en>] IN ([(Democratic Republic of the) Congo],[(Republic of) Côte d'Ivoire],[Afghanistan],[Aland Islands],[Albania],[Algeria],[American Samoa],[Andorra],[Angola],[Anguilla],[Antarctica],[Anti

这里也是 CSV 中每个选项卡的示例数据: 来自 3 个表的数据示例

谢谢!

于 2019-03-25T07:56:30.540 回答