获取错误:在FROM子句中多次指定相关名称“General”

时间:2012-01-23 14:47:31

标签: sql

ALTER PROCEDURE [dbo].[REPORTING_MonthlyClientTimesheet_wrapper2_TRR_Month]
      (@StartDate DATETIME,
       @taskcode VARCHAR(250),
       @username VARCHAR(250))
AS 
BEGIN 
      SET NOCOUNT ON 
      DECLARE @ProcedureName SYSNAME     

        -- Give them one full month of records
      DECLARE @EndDate DATETIME


      SET @ProcedureName = OBJECT_NAME(@@PROCID) 

       --SET @EndDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@StartDate)+1,0)) -- end month
      SET @EndDate = DATEADD(s, -1, DATEADD(YY, DATEDIFF(YYYY, 0, @StartDate) + 1, 0)) --set to end of Year


      BEGIN TRY


            SELECT DISTINCT
                @username,
                cal.calendaryear,
                cal.calendarmonth,
                cal.calendarday,
                DATEPART(wk, Calendardate) AS weekno,
                DATENAME(month, DATEADD(month, cal.calendarmonth - 1, 0)) AS iMonthName,
                DATEADD(s, -1, DATEADD(YY, DATEDIFF(YYYY, 0, @StartDate) + 1, 0)) AS enddated,
                LEFT(cal.dayofweekname, 2) AS Weekday,
                    --  Taskcode,
                CASE WHEN Duration IS NULL THEN 0
                     ELSE duration
                END AS hours,
                CASE WHEN leave.leave IS NULL THEN 0
                     ELSE leave.leave
                END AS leave,
                CASE WHEN TOIL.toil IS NULL THEN 0
                     ELSE TOIL.toil
                END AS toil,
                CASE WHEN sick.sick IS NULL THEN 0
                     ELSE sick.sick
                END AS sick,
                CASE WHEN General.General IS NULL THEN 0
                     ELSE General.General
                END AS General,
                CONVERT(VARCHAR(10), Calendardate, 103) AS Calendardate
            FROM
                (SELECT
                    UserInfo.ID AS UserID,
                    dbo.UserInfo.FirstName,
                    tse.Id AS TimesheetentryID,
                    tse.EntryDate,
                    Duration,
                    TaskCode,
                    TaskId,
                    tts.Id AS tasktimesheetid,
                    YEAR(entrydate) AS EntryYear,
                    MONTH(entrydate) AS EntryMOnth,
                    DAY(entrydate) AS Entryday
                 FROM
                    TimesheetEntry tse
                 INNER JOIN TaskTimesheet tts ON tse.TaskTimesheetId = tts.Id
                 INNER JOIN dbo.Task WITH (NOLOCK) ON dbo.task.Id = TTS.TaskId
                 INNER JOIN dbo.ReportPeriod WITH (NOLOCK) ON TTS.ReportPeriodId = dbo.ReportPeriod.Id
                 INNER JOIN dbo.Login WITH (NOLOCK) ON dbo.ReportPeriod.UserId = dbo.Login.UserId
                 INNER JOIN dbo.UserInfo WITH (NOLOCK) ON dbo.ReportPeriod.UserId = dbo.UserInfo.Id
                 INNER JOIN dbo.[Login] logdet WITH (NOLOCK) ON logdet.Userid = UserInfo.Id
                 WHERE
                    Task.TaskCode = @taskcode
                    AND 'a\' + login.LoginName = @username) TimeSE
            RIGHT JOIN dbo.TestCalendar cal ON cal.CalendarYear = Entryyear
                                               AND cal.CalendarMonth = entrymonth
                                               AND cal.CalendarDay = entryday
            LEFT JOIN (SELECT --UserInfo.ID as UserID,
                        --  dbo.UserInfo.FirstName,
                        --  tse.Id as TimesheetentryID,
                        tse.EntryDate,
                        Duration AS General
                        --  TaskCode,                           
                        --  TaskId,
                        --  tts.Id as tasktimesheetid,
                        --  year(entrydate) as EntryYear,
                        --  month(entrydate) as EntryMOnth,
                        --  day(entrydate) as Entryday
                       FROM
                        TimesheetEntry tse
                       INNER JOIN TaskTimesheet tts ON tse.TaskTimesheetId = tts.Id
                       INNER JOIN dbo.Task WITH (NOLOCK) ON dbo.task.Id = TTS.TaskId
                       INNER JOIN dbo.ReportPeriod WITH (NOLOCK) ON TTS.ReportPeriodId = dbo.ReportPeriod.Id
                       INNER JOIN dbo.Login WITH (NOLOCK) ON dbo.ReportPeriod.UserId = dbo.Login.UserId
                       INNER JOIN dbo.UserInfo WITH (NOLOCK) ON dbo.ReportPeriod.UserId = dbo.UserInfo.Id
                       INNER JOIN dbo.[Login] logdet WITH (NOLOCK) ON logdet.Userid = UserInfo.Id
                       WHERE
                        Task.TaskCode = @taskcode
                        AND 'a\' + login.LoginName = @username
                        AND TaskCode = 'KPJ.GENERAL') AS General ON general.EntryDate = cal.CalendarDate
            LEFT JOIN (SELECT
                        userid,
                        EntryDate,
                        SUM(Hours) AS leave
                       FROM
                        TimeOffs Toff
                       INNER JOIN TimeOffCode ToffC ON toff.TimeOffCodeId = toffc.Id
                       INNER JOIN dbo.TimeOffEntries ToffE ON toffe.timeoffid = toff.id
                       WHERE
                        name IN ('Bank & Public Holidays', 'Annual Leave', 'authorised Absence')
                        AND EntryDate >= @startdate
                        AND EntryDate <= @enddate
                        AND userid = (SELECT
                                        UserId
                                      FROM
                                        login
                                      WHERE
                                        'a\' + LoginName = @username)
                       GROUP BY
                        toff.UserId,
                        EntryDate) AS Leave ON leave.EntryDate = cal.calendardate
            LEFT JOIN --TOIL
                (SELECT
                    toff.userid,
                    EntryDate,
                    Hours AS TOIL
                 FROM
                    TimeOffs Toff
                 INNER JOIN TimeOffCode ToffC ON toff.TimeOffCodeId = toffc.Id
                 INNER JOIN dbo.TimeOffEntries ToffE ON toffe.timeoffid = toff.id
                 WHERE
                    name IN ('TOIL')
                    AND EntryDate >= @StartDate
                    AND EntryDate <= @enddate
                    AND userid = (SELECT
                                    UserId
                                  FROM
                                    Login
                                  WHERE
                                    'a\' + LoginName = @username)) AS TOIL ON TOIL.EntryDate = cal.calendardate
            LEFT JOIN --SICK
                (SELECT
                    toff.userid,
                    EntryDate,
                    Hours AS SICK
                 FROM
                    TimeOffs Toff
                 INNER JOIN TimeOffCode ToffC ON toff.TimeOffCodeId = toffc.Id
                 INNER JOIN dbo.TimeOffEntries ToffE ON toffe.timeoffid = toff.id
                 WHERE
                    name IN ('SICK')
                    AND EntryDate >= @startdate
                    AND EntryDate <= @enddate
                    AND userid = (SELECT
                                    UserId
                                  FROM
                                    login
                                  WHERE
                                    'a\' + LoginName = @username)) AS SICK ON SICK.EntryDate = cal.calendardate
            LEFT JOIN --SICK
                (SELECT
                    toff.userid,
                    EntryDate,
                    Hours AS SICK
                 FROM
                    TimeOffs Toff
                 INNER JOIN TimeOffCode ToffC ON toff.TimeOffCodeId = toffc.Id
                 INNER JOIN dbo.TimeOffEntries ToffE ON toffe.timeoffid = toff.id
                 WHERE
                    name IN ('SICK')
                    AND EntryDate >= @startdate
                    AND EntryDate <= @enddate
                    AND userid = (SELECT
                                    UserId
                                  FROM
                                    login
                                  WHERE
                                    'a\' + LoginName = @username)) AS General ON SICK.EntryDate = cal.calendardate
            WHERE
                cal.Calendardate >= @startdate
                AND Cal.calendardate <= DATEADD(s, -1, DATEADD(YY, DATEDIFF(YYYY, 0, @startdate) + 1, 0))
            ORDER BY
                cal.CalendarMonth
      END TRY 
      BEGIN CATCH 
            SELECT
                @ProcedureName AS StoredProcName,
                ERROR_NUMBER() AS ErrorNumber ; 

            RETURN(-1) 
      END CATCH 

      RETURN(0) 
END 

1 个答案:

答案 0 :(得分:5)

您有两个子查询。两个名称为General的别名。您需要更改其中一个的名称,或删除其中一个(第二个具有与子查询无关的JOIN条件,因此可能会误将其粘贴?) ...

这应该更容易看到。

from 
  (A sub-query) TimeSE
right join
  dbo.TestCalendar cal
    on cal.CalendarYear =Entryyear
    and cal.CalendarMonth = entrymonth
    and cal.CalendarDay = entryday
LEFT JOIN 
  (a sub-query) AS General
    ON general.EntryDate=cal.CalendarDate
LEFT JOIN 
  (a sub-query) as Leave
    on leave.EntryDate=cal.calendardate                 
LEFT Join
  (A sub-query) as TOIL
    ON TOIL.EntryDate=cal.calendardate  
LEFT JOIN
  (a sub-query) as SICK
    ON SICK.EntryDate=cal.calendardate  
LEFT JOIN   
  (a sub-query) as General
    ON SICK.EntryDate=cal.calendardate  
WHERE
     cal.Calendardate  >=@startdate
  and Cal.calendardate <=DATEADD(s,-1,DATEADD(YY, DATEDIFF(YYYY,0,@startdate)+1,0))
order by
  cal.CalendarMonth