比指定的SQL少的列

时间:2013-10-02 09:19:01

标签: sql join

我在SQL服务器上运行以下查询,它给了我错误:

'结果'的列数少于列列表中指定的数量。

我清楚地列出了几个列,所以我不确定问题是什么。我尝试只放置结果(ContractDesc)并解决了错误,所以我猜它认为我只在该查询中选择了一列。我该如何解决这个问题?

SELECT
Cl.LegalName AS ClientNames,
Results.ContractDesc AS ContractNames,
Results.ProjectNames AS ProjectNames,
Results.EmployeeNames AS EmployeeNames,
Results.TotalHours AS TotalHours, 
Results.TotalCharges AS TotalCharges, 
Results.BillingContacts AS BillingContacts

FROM Clients Cl

CROSS APPLY (
    SELECT 
    ( Cr.ContractDesc + ', ' ) AS ContractDesc, 
    ( P.ProjectName + ', ' ) AS ProjectNames, 
    ( E.FirstName + ' ' + E.LastName + ', ' ) AS EmployeeNames,
    ( WH.HoursWorked + ', ' ) AS TotalHours,
    ( WH.HoursWorked * BR.Rate ) + ', ' AS TotalCharges,
    ( Ca.FirstName + Ca.LastName + ', ' + Ca.AddrLine1 + ', ' ) AS BillingContacts

    FROM Contracts Cr

    JOIN Projects P ON( Cr.ContractID = P.ContractID )
    JOIN EmployeesProjects EP ON( P.ProjectID = EP.ProjectID )
    JOIN Employees E ON( EP.EmpID = E.EmpID )
    JOIN WorkHours WH ON( E.EmpID = WH.EmpID )
    JOIN BillingRates BR ON( E.TitleID = BR.TitleID ) AND ( E.Level = BR.Level )
    JOIN ContractsContacts CC ON( Cr.ContractID = CC.ContractID )
    JOIN Contacts Ca ON( CC.ContactID = Ca.ContactID )

    WHERE( Cl.ClientID = Cr.ClientID )
    AND ( WH.WH_Month = 4 )
    AND ( WH.WH_Year = 2013 )

    GROUP BY Cr.ContractID,
    Cr.ContractDesc,
    P.ProjectName,
    E.FirstName,
    E.LastName,
    WH.HoursWorked,
    BR.Rate,
    Ca.FirstName,
    Ca.LastName,
    Ca.AddrLine1

    FOR XML PATH( '' )
) Results ( ContractDesc, ProjectNames, EmployeeNames, TotalHours, TotalCharges, BillingContacts )

1 个答案:

答案 0 :(得分:1)

结果()中似乎缺少

ClientNames