SET @L_SQL2 = N'
SELECT DeptID, DeptName, AdminUserId, AdminUserName, AssignCount, TraceCount, UserIntention1, UserIntention2, UserIntention3, UserIntention4, UserIntention5, UserIntention6, UserIntention7
FROM (
SELECT
Z3.DeptId AS DeptID, Z6.DeptName AS DeptName,
Z3.AdminUserId AS AdminUserId, Z7.UserName AS AdminUserName,
Z4.AssignCount AS AssignCount,
Z5.TraceCount AS TraceCount,
Z3.UserIntention1 AS [UserIntention1],
Z3.UserIntention2 AS [UserIntention2],
Z3.UserIntention3 AS [UserIntention3],
Z3.UserIntention4 AS [UserIntention4],
Z3.UserIntention5 AS [UserIntention5],
Z3.UserIntention6 AS [UserIntention6],
Z3.UserIntention7 AS [UserIntention7]
FROM ' +
'##TempUserIntention AS Z3 ' + CHAR(13) +
' LEFT JOIN(
SELECT ZZ4.AssignCount, ZZ4.[AdminUserId]
FROM (
SELECT COUNT(DISTINCT [UserId]) AS AssignCount, [AdminUserId]
FROM [DbCRM].[dbo].[tblUserInfo]
GROUP BY [AdminUserId]
) AS ZZ4
) AS Z4 ON( Z4.[ADMINUSERID] = Z3.[ADMINUSERID])' + CHAR(13) +
' LEFT JOIN(
SELECT ZZ5.TraceCount, ZZ5.[AdminUserId]
FROM (
SELECT COUNT(DISTINCT [UserId]) AS TraceCount, [AdminUserId]
FROM [DbCRM].[dbo].[tblUserInfoRemarks]
GROUP BY [AdminUserId]
)AS ZZ5
) AS Z5 ON ( Z5.[ADMINUSERID] = Z3.[ADMINUSERID])' + CHAR(13) +
' LEFT JOIN [dbo].[tblSysDept] AS Z6 ON( Z3.[DeptId] = Z6.[DeptId])' + CHAR(13) +
' LEFT JOIN [dbo].[vw_UserInfo] AS Z7 ON( Z3.[AdminUserId] = Z7.[Id])' + CHAR(13) +
' WHERE Z3.[UID] >=' + CAST(@P_STARTINDEX AS VARCHAR(10)) +
' AND Z3.[UID] <= '+ CAST(@P_EndIndex AS VARCHAR(10)) + CHAR(13) +
' ) AS YY'; --一定要有别名YY,否则报')' 附近有语法错误。
SELECT DeptID, DeptName, AdminUserId, AdminUserName, AssignCount, TraceCount, UserIntention1, UserIntention2, UserIntention3, UserIntention4, UserIntention5, UserIntention6, UserIntention7
FROM (
SELECT
Z3.DeptId AS DeptID, Z6.DeptName AS DeptName,
Z3.AdminUserId AS AdminUserId, Z7.UserName AS AdminUserName,
Z4.AssignCount AS AssignCount,
Z5.TraceCount AS TraceCount,
Z3.UserIntention1 AS [UserIntention1],
Z3.UserIntention2 AS [UserIntention2],
Z3.UserIntention3 AS [UserIntention3],
Z3.UserIntention4 AS [UserIntention4],
Z3.UserIntention5 AS [UserIntention5],
Z3.UserIntention6 AS [UserIntention6],
Z3.UserIntention7 AS [UserIntention7]
FROM ' +
'##TempUserIntention AS Z3 ' + CHAR(13) +
' LEFT JOIN(
SELECT ZZ4.AssignCount, ZZ4.[AdminUserId]
FROM (
SELECT COUNT(DISTINCT [UserId]) AS AssignCount, [AdminUserId]
FROM [DbCRM].[dbo].[tblUserInfo]
GROUP BY [AdminUserId]
) AS ZZ4
) AS Z4 ON( Z4.[ADMINUSERID] = Z3.[ADMINUSERID])' + CHAR(13) +
' LEFT JOIN(
SELECT ZZ5.TraceCount, ZZ5.[AdminUserId]
FROM (
SELECT COUNT(DISTINCT [UserId]) AS TraceCount, [AdminUserId]
FROM [DbCRM].[dbo].[tblUserInfoRemarks]
GROUP BY [AdminUserId]
)AS ZZ5
) AS Z5 ON ( Z5.[ADMINUSERID] = Z3.[ADMINUSERID])' + CHAR(13) +
' LEFT JOIN [dbo].[tblSysDept] AS Z6 ON( Z3.[DeptId] = Z6.[DeptId])' + CHAR(13) +
' LEFT JOIN [dbo].[vw_UserInfo] AS Z7 ON( Z3.[AdminUserId] = Z7.[Id])' + CHAR(13) +
' WHERE Z3.[UID] >=' + CAST(@P_STARTINDEX AS VARCHAR(10)) +
' AND Z3.[UID] <= '+ CAST(@P_EndIndex AS VARCHAR(10)) + CHAR(13) +
' ) AS YY'; --一定要有别名YY,否则报')' 附近有语法错误。