我试图向现有的存储过程中添加一些其他列。这不仅需要添加列,还需要创建LEFT JOIN子查询以收集在“销售和保修总计”中添加注释的其他列。现在,当我在存储过程中执行此代码时,SQL命令中出现错误。
我已经尝试打开一个新的查询窗口,并尝试以正确的格式重新创建查询以执行该查询,但是出现以下错误:消息102,级别15,状态1,第60行'+大小写为null(null)时,语法不正确。消息102,第15级,州1,第117行'+大小写不正确,如果为null(@TaxHeaders,')='然后为'。]
我还尝试过在包含枢轴的T和T1 LEFT JOIN上方更改我的LEFT JOIN查询(SWT,GT)的位置。
CREATE Procedure [GP].[spInvoiceReprintsSummary] @CompanyKey varchar(2)=5, @ParentCustomerNum varchar(255)='BCAA01', --'AAANYCITY', --'AAANPENN01', --'CAAQUEBE01', --'BCAA01', --'CAAQUEBE01', @StartDateKey varchar(8)=20130306, @EndDateKey varchar(8)=20130315, @TaxHeaders varchar(255)='HST,GST,PST,QST' --'GST, QST' As Begin Declare @SQL as varchar(max) Set @SQL = ' Select S.CompanyKey, S.TaxScheduleID, S.CustomerKey, S.ParentCustomerNum, S.StationID, S.CustomerName, S.DocumentTypeKey, S.DocumentNum, Case When Left(S.DocumentNum, 2)=''ST'' Then 0 ELSE 1 End As EligibleDiscount,S.ExtendedPrice,S.PurchaseOrderNum, DocumentDate, S.TaxAmount, SWT.SalesTotal, SWT.WarrantiesTotal, S.TerritoryId ' + case when isnull(@TaxHeaders,'') = '' then '' else ',' + @TaxHeaders end + ' From ( --Get distinct documents Select --Top 100 SD.CompanyKey, SD.TaxScheduleID, CS.CustomerKey, CS.ParentCustomerNum, CS.StationID, CS.CustomerName, SD.DocumentTypeKey, SD.DocumentNum,SD.PurchaseOrderNum, convert(datetime,convert(varchar, DateKey, 112)) As DocumentDate,sum(SD.TaxAmount) as TaxAmount, sum(SD.ExtendedPrice) As ExtendedPrice, CS.TerritoryId From GP.SalesDetail SD Inner Join GP.Customers CS on SD.CustomerKey = CS.CustomerKey Where CS.ParentCustomerNum= ''' + @ParentCustomerNum + ''' And SD.DateKey Between ' + @StartDateKey + ' and ' + @EndDateKey + ' And SD.CompanyKey = ' + @CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0 Group by SD.CompanyKey, SD.TaxScheduleID, CS.CustomerKey, CS.ParentCustomerNum, CS.StationID, CS.CustomerName, SD.DocumentTypeKey, SD.DocumentNum,SD.PurchaseOrderNum, convert(datetime,convert(varchar, DateKey, 112)), CS.TerritoryId --Order By SD.PurchaseOrderNum, SD.DocumentTypeKey ) S ---Addition of sales and warranties totals LEFT JOIN( SELECT CASE WHEN GroupName = ''SALES'' then sum(QuantitySold) else 0 end as SalesTotal,CASE WHEN GroupName = ''WARRANTIES'' then sum(QuantitySold) else 0 end as WarrantiesTotal, DocumentNum, CompanyKey FROM (SELECT SD.QuantitySold , DocumentNum , SD.CompanyKey ,CASE WHEN I.userDefItemClass1 = ''BATTERY/RETURN'' AND SD.QuantitySold >= 0 THEN ''SALES'' WHEN I.userDefItemClass1 = ''BATTERY/RETURN'' AND SD.QuantitySold < 0 THEN ''RETURNS'' WHEN I.ItemNum = ''RESTOCKING FEE'' AND sd.ExtendedPrice >= 0 THEN ''RETURNS'' WHEN I.ItemNum = ''RESTOCKING FEE'' AND sd.ExtendedPrice < 0 THEN ''SALES'' WHEN I.userDefItemClass1 = ''WARRANTY'' THEN ''WARRANTIES'' WHEN I.userDefItemClass1 = ''NRF'' THEN ''RECYCLING FEES'' WHEN I.userDefItemClass1 = ''CORES'' THEN ''SPENT BATTERIES'' WHEN I.ItemNum IN (''PU2'',''PU4.3'') AND sd.ExtendedPrice > 0 THEN ''RETURNS'' WHEN I.ItemNum IN (''PU2'',''PU4.3'') AND sd.ExtendedPrice < 0 THEN ''SALES'' ELSE ''OTHER'' END AS GroupName FROM GP.SalesDetail AS SD INNER JOIN GP.Items AS I ON SD.ItemKey = I.ItemKey Inner Join GP.Customers CS on SD.CustomerKey = CS.CustomerKey WHERE CS.ParentCustomerNum= '' + @ParentCustomerNum + '' And SD.DateKey Between ' + @StartDateKey + ' and ' + @EndDateKey + ' And SD.CompanyKey = ' + @CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0 ) GN GROUP BY DocumentNum, GroupName, CompanyKey ) SWT ON S.Companykey = SWT.CompanyKey And S.DocumentNum = SWT.DocumentNum Left join ( --Pivot the Tax Types Select Companykey, DocumentTypeKey, DocumentNum ' + case when isnull(@TaxHeaders,'') = '' then '' else ',' + @TaxHeaders end + ' From ( SELECT SD.CompanyKey,TD.DocumentTypeKey, TD.DocumentNum, TS.TaxDetailLabel, SUM(TD.TaxAmount) AS TaxAmount FROM GP.TaxSchedules AS TS INNER JOIN (SELECT SD.CompanyKey, SD.TaxScheduleId, SD.DocumentTypeKey, SD.DocumentNum FROM GP.SalesDetail SD Inner Join GP.Customers CS on SD.CustomerKey = CS.CustomerKey Where CS.ParentCustomerNum= ''' + @ParentCustomerNum + ''' And SD.DateKey Between ' + @StartDateKey + ' and ' + @EndDateKey + ' And SD.CompanyKey = ' + @CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0 GROUP BY SD.CompanyKey, SD.TaxScheduleId, SD.DocumentTypeKey, SD.DocumentNum) AS SD ON TS.CompanyKey = SD.CompanyKey AND TS.TaxScheduleId = SD.TaxScheduleId INNER JOIN GP.TaxDetails AS TD ON TD.DocumentNum = SD.DocumentNum AND TD.DocumentTypeKey = SD.DocumentTypeKey AND TD.LineItemSequenceNum = 0 AND TD.TaxDetailID = TS.TaxDetailId AND TD.CompanyKey = TS.CompanyKey GROUP BY SD.CompanyKey,TD.DocumentTypeKey,TD.DocumentNum, TS.TaxDetailLabel ) T Pivot (sum(TaxAmount) For TaxDetailLabel In (' + case when isnull(@TaxHeaders,'') = '' then 'Tax' else @TaxHeaders end + ')) as PVT ) T1 on S.Companykey = T1.CompanyKey And S.DocumentTypeKey = T1.DocumentTypeKey And S.DocumentNum = T1.DocumentNum' --print @SQL Exec (@SQL) End
我希望原始结果集的输出包括SalesTotal和WarrantiesTotal的新列
我试图向现有的存储过程中添加一些其他列。这不仅需要添加列,还需要创建LEFT JOIN子查询以收集以下额外的列:...
替换