带有连接和变量的SQL Pivot

时间:2014-12-18 14:50:08

标签: sql variables pivot procedure

我对SQL很陌生,而且最近的任务已经过时了。这个程序是我使用一个正常运行的SP构建的,有一些小的改动。

这仍然是失败的,我不明白为什么,有经验的人可以扫描一下并解释我哪里出错了吗?或提供任何意见?

这应该包含给定@acctgrp中每个帐户的数据行。每个月都应该有一列我们有A.TotalAllExtendedCostLessExcludedItems, A.GenericPercentSource个数据。

这个项目对我来说只是第一次。这是一次很棒的学习经历,但我处在一个无法攀爬的洞里。

USE [SQLDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [Customer].[SMRebate]
@months dec(3,0), @AcctGrp varchar(5)
as
begin tran

DECLARE @MonthString varchar(max), @SQL varchar(max), @FirstMonth varchar(7),

Select DISTINCT
A.OrdYrMth,
A.AcctNo as 'Cust',
C.BILNAM as 'Customer Name',
C.BilAd2 as 'City/State',
isNULL (X.SANAME,'None Assigned') as 'Manager',
C.CHosp as 'Pay Terms',
--D.SANAME as Salesperson
A.Multiplier as 'InvMPX',
H.SHPNAM as 'Admin',
'                     ' as 'Pet Pct',
isNull (I.GroupName,J.[Account Reference]) as 'Group Name',
A.TotalAllExtendedCostLessExcludedItems,
A.GenericPercentSource
INTO ##SMRebateTemp  --- used global reference ##
from 
Customer.rebateHistory  A 
Join Person.Person B with(nolock) on A.AcctNo=B.AcctNo
Join Customer E with(nolock) on A.Acctno=E.Acctno
Join AS400_CUSMAST C with(nolock) on B.AcctNo=C.AcctNo
Join  customer.admingroups H with(nolock) on C.AcctNo=H.AcctNo
Join SQLDB.Customer.RetailGroups I with(nolock) on H.AcctNo=I.AcctNo
Join customer.customergroups J with(nolock) on I.AcctNo= J.Account
left JOIN [SQLDB].[Sales].[SalesManAS400] D  with(nolock) on D.SALENO=E.Salesman
left outer JOIN [SQLDB].[Sales].[SalesManAS400] X with(nolock) on D.SREGNO=X.SALENO

---not certain what this does exactly, but I did my best to logically mimic the template I was given
select @MonthString = COALESCE(@MonthSTring + ', ', '') + '[' + CONVERT(varchar,derived.OrdYrMth) + ']'
from 
    (
        select derived.OrdYrMth
        from ##SMRebateTemp derived with(nolock)
        where derived.OrdYrMth >= SQLDB.dbo.YrMth(DATEADD(month,-@months,getdate()))
        group by derived.OrdYrMth
    ) derived
order by derived.OrdYrMth

set @FirstMonth = SQLDB.dbo.YrMth(DATEADD(month,-@months,getdate()))


set @SQL = 
'
Select Pivot1.Cust, Table1.[Customer Name],Table1.[City/State],Table1.Manager,Table1.[Pay Terms],Table1.InvMPX,
Table1.Admin,Table1.[Group Name],Table1.TotalAllExtendedCostLessExcludedItems,Table1.GenericPercentSource,' + @MonthString + ',Table1.[Pet Pct]

FROM
(
Select DISTINCT
derived.OrdYrMth,
A.AcctNo as "Cust",
C.BILNAM as "Customer Name",
C.BilAd2 as "City/State",
isNULL (X.SANAME,''None Assigned'') as "Manager",
C.CHosp as "Pay Terms",
--D.SANAME as Salesperson
A.Multiplier as "InvMPX",
H.SHPNAM as "Admin",
"                     " as "Pet Pct",
isNull (I.GroupName,J.[Account Reference]) as "Group Name",
A.TotalAllExtendedCostLessExcludedItems,
A.GenericPercentSource

from 
Customer.rebateHistory  A 
Join Person.Person B with(nolock) on A.AcctNo=B.AcctNo
Join Customer E with(nolock) on A.Acctno=E.Acctno
Join AS400_CUSMAST C with(nolock) on B.AcctNo=C.AcctNo
Join  customer.admingroups H with(nolock) on C.AcctNo=H.AcctNo
Join SQLDB.Customer.RetailGroups I with(nolock) on H.AcctNo=I.AcctNo
Join customer.customergroups J with(nolock) on I.AcctNo= J.Account
Join ##SMRebateTemp Temp with(nolock) on J.Account = Temp.Cust
left JOIN [SQLDB].[Sales].[SalesManAS400] D  with(nolock) on D.SALENO=E.Salesman
left outer JOIN [SQLDB].[Sales].[SalesManAS400] X with(nolock) on D.SREGNO=X.SALENO

left join SQLDB.Sales.SalesmanMaster s with(nolock) on c.CSLSMN = s.SalesmanNo 
join SQLDB.dbo.acctgrp g on d.AcctNo = g.AcctNo
and g.GrpKey = '''+ @AcctGrp +'''

where Temp.OrdYrMth >= ('+ @FirstMonth +')
) as Table1
pivot
(
sum(A.TotalAllExtendedCostLessExcludedItems)
for a.OrdYrMth in (' + @MonthString + ')
) as Pivot1'

Exec (@SQL)

Drop Table ##SMRebateTemp

0 个答案:

没有答案