SQL SubQuery中的列无效

时间:2017-01-25 00:53:32

标签: sql-server-2012

我正在尝试在下面运行查询,但我收到以下错误,无法弄明白。

请查看如何计算列AllocPer。错误来自的地方:

  

Msg 207,Level 16,State 1,Procedure USP_RS_Dealio_JLL_ECRDetails_test,Line 688
  列名称无效' dealid'。

     

Msg 207,Level 16,State 1,Procedure USP_RS_Dealio_JLL_ECRDetails_test,Line 689
  列名称无效' empid'。

我的SQL代码:

SELECT 
    MarketPerDiff, CommissionDate, empid, dealid 
INTO
    #vwDealEmpSplitDetail_Emp 
FROM 
    dbo.vwDealEmpSplitDetail_Emp

PRINT 'Query 1 Starts at ' + CONVERT(nvarchaR(36), getdate(), 114)
SET@ t1 = GETDATE()

SELECT
    Per.enddate "CommEndDate",
    'Period ' + CAST(Per.Period AS VARCHAR) "SubPeriod",
    Col.EligibleDate AS "CollectionDate",
    Col.DealID,
    Client.clientName,
    Deal.DealName,
    Col.EmpID AS "ProfID",
    Emp.FullName AS "ProfName",
    SUM(ISNULL(Col.CashCollected, 0)) CashCollected,
    Stat.MarketID,
    Stat.OpUnit,
    HB.Description,
    SUM(ISNULL(Col.CostHurdle, 0)) AS "DEmpCostHurdle",
    ISNULL(Prof.DealCostMultiplier, 50)[DealCostMultiplier], 
    (SELECT SUM(MarketPerDiff) "MarketPerDiff"
     FROM #vwDealEmpSplitDetail_Emp 
     WHERE #vwDealEmpSplitDetail_Emp.dealid = Deal.DealID AND 
           #vwDealEmpSplitDetail_Emp.empid = Emp.EmpID) as AllocPer
INTO 
    #SummedValues
FROM 
    tblEmpCollectionAdj Col WITH(NOLOCK)
JOIN 
    tblDeal Deal WITH(NOLOCK) ON Deal.DealID = Col.DealID
LEFT JOIN 
    dbo.tblClient Client WITH(NOLOCK) ON Client.ClientCode = Deal.ClientCode
JOIN 
    tblMktOpUnitCompStatus Stat WITH(NOLOCK) ON Stat.BatchID = Col.BatchID
LEFT JOIN 
    dbo.tblEmployee Emp WITH(NOLOCK) ON Emp.EmpID = Col.EmpID AND Emp.Active = 1
LEFT JOIN 
    dbo.tblCommPeriod Per WITH(NOLOCK) ON Col.EligibleDate BETWEEN Per.StartDate AND Per.Enddate
LEFT JOIN 
    tblEmpCompProfile Prof WITH(NOLOCK) ON Prof.EmpID = Col.EmpId 
                                        AND Prof.Active = 1
                                        AND Prof.PeriodID = @PeriodId
                                        AND Prof.Batchid = Col.Batchid
LEFT JOIN 
    tblHBAlloc HB ON HB.HBAcctEmpID = Prof.EmpID 
                  AND HB.OpUnit = Prof.OpUnit
                  AND HB.CF3 = Prof.MarketId 
                  AND HB.FiscalYear = @Year1
WHERE 
    Col.Active = 1
    AND Deal.Active = 1
    AND Col.PeriodId = @PeriodId
    AND ISNULL(Col.CashCollected, 0) + ISNULL(Col.CostHurdle, 0) NOT BETWEEN - .01 AND.01
GROUP BY
    Per.enddate,
    'Period ' + CAST(Per.Period AS VARCHAR),
    Col.EligibleDate, Col.DealID, Client.clientName,
    Deal.DealName,
    Col.EmpID,
    Emp.FullName,
    Col.CashCollected,
    Stat.MarketID, Stat.OpUnit,
    HB.Description,
    ISNULL(Prof.DealCostMultiplier, 50), 
    deal.dealid, Emp.empid, Per.StartDate, Per.Enddate

PRINT 'Query 1 Ends ' + CONVERT(nvarchaR(36), getdate(), 114)
SET@ t2 = GETDATE()
PRINT ' TIME ELAPSED ' + CAST(DATEDIFF(millisecond, @t1, @t2) AS NVARCHAR(255))

1 个答案:

答案 0 :(得分:1)

错误应该是自我解释的。您似乎正在引用您尝试从中访问它的表中不存在的列。无法访问您的表结构,遗憾的是我们只能猜测这个问题。但是,您的错误表明列dealidempid无效。我看到它们被用在了几个地方。

检查以确保以下表格/视图中存在dealidtblEmpCollectionAdjvwDealEmpSplitDetail_EmptblDeal

并检查以下表格/视图中是否存在empidvwDealEmpSplitDetail_EmptblEmpCollectionAdjtblEmployeetblEmpCompProfile

此外,@ Blorgbeard在评论中提到的一点,检查您是否正在使用适当的列和列别名。您将表tblDeal别名为Deal,但至少在一个地方将其引用为deal