SQL サーバーで次のクエリを実行していますが、エラーが発生します。
「結果」には、列リストで指定された数より少ない列があります。
いくつかの列を明確にリストしているので、何が問題なのかわかりません。Results( ContractDesc ) だけを入れてみたところ、エラーが解決したので、そのクエリ内で 1 つの列のみを選択していると思われると思います。これを解決するにはどうすればよいですか?
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 )