消息207,级别16,状态1,行131无效的列名称“OrgUnit”

时间:2013-12-17 10:46:55

标签: sql sql-server-2008

您好我需要帮助来解决此存储过程,我的查询有问题吗?按组分组。我收到此消息消息消息207,级别16,状态1,行131 列名称“OrgUnit”无效。

-- get actual time - REGULAR



    declare @IncludeID int
    declare @TimeDetail int
    declare @FromDate Datetime
    declare @ToDate Datetime
    declare @TimeTypeGroup int
    declare @ResourceID nvarchar(30)
    declare @OrgUnit nvarchar(15)

    set @IncludeID = 1
    set @TimeDetail = 2
    set @FromDate = '2013-11-01'
    set @ToDate = '2013-11-30'
    set @TimeTypeGroup = 2
    set @ResourceID = 'DM6299'
    set @OrgUnit = 'NSW%'

    create table #ItemisedTimeandMaterialsTESTREGULAR

    (
    IDNo int,
    OrderBy1 varchar(60),
    ItemDate datetime,--MOD005
    RevenueTypeCode varchar(24),
    TimeType varchar(24),
    ProjectCode varchar(20),
    taskUID int,
    OutlineNum varchar(60),
    taskname varchar(60),
    activitycode varchar(24),
    ActivityDesc varchar(60),
    ResourceID varchar(24),
    OrganizationID nvarchar(15),
    firstname varchar(60),
    lastname varchar(60),
    ExpenseTypeCode varchar(24),
    ExpenseTypeDesc varchar(60),
    Hours decimal(8,2),
    Rate decimal(8,2),
    Total decimal(20,8),
    Descr varchar(256), --MOD005 DM Added col for relevant detail for Expenses
    TimeTypeCode nvarchar(10)
    )


    create table #Resources
    (
    ResourceID nvarchar(30),
    OrganizationID nvarchar(15)
    )


    --insert into #Resources
    -- 1. @resourceid is present then only 1 single record in the table
    -- 2. if @orgunit  is present, find all resourceID belongs to this orgunit and insert into #resources

    if @ResourceID <> ''
     begin
     insert into #Resources

      Select  ResourceID, OrganizationID from ResourceOrganization
      where ResourceID = @ResourceID

    end

    if @OrgUnit <> ''
     begin
     insert into #Resources
      Select ResourceID, OrganizationID from ResourceOrganization
      where OrganizationID like '' + @OrgUnit + '%'
      end


    insert into #ItemisedTimeandMaterialsTESTREGULAR
    select      
    Case when @IncludeID = 1 then b.timeID else '' end, --mod 07
    e.lastname + e.firstname, 
    case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
    'FEES',
    'Regular',
    b.projectcode,
    b.taskuid,
    f.outlinenum,
    f.taskname,
    b.ActivityCode,
    c.ActivityDesc,
    b.resourceID,
    (select OrganizationID from #Resources where resourceID = b.resourceID) as OrgUnit,
    e.firstname,
    e.lastname,
    '','', -- expense
    sum(isnull(b.StandardHours,0)), -- MOD003 - added in isnull's
    0,--h.StandardAmt,--b.NegotiatedChargeRate, --MOD005 Change to NegotiatedChargeRate from StandardChargeRate
    0,--sum(isnull(b.StandardHours,0)* IsNull(h.standardAmt,0)),--sum(bd.BilledAmt),--MOD005 Change from BillableAmt feild (was incorrect for adjustments)
    case when @TimeDetail = 3 then b.invoicecomment else '' end,--MOD005
    case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008

    from time b 
    join activity c
    on b.activitycode = c.activitycode
    join resource e 
    on b.resourceID = e.resourceID
    join project p 
    on b.ProjectCode=p.ProjectCode 
    and p.RevisionStatusCode='A'
    join task f 
    on b.projectcode = f.projectcode 
    and b.taskuid =f.taskuid 
    and f.revisionnum = p.RevisionNum
    join SMECWeekEnding g   
    on b.TimeEntryDate between g.StartDate and g.EndDate
    --left join ratesetresource h on h.resourceid = b.resourceid
    where       --b.projectcode = @PROJECTCODE and
    b.statuscode in ('A','V','T')
    and b.TimeEntryDate >= @FromDate
    and b.TimeEntryDate <= @ToDate
    and Isnull(b.StandardHours,0) <> 0
    --and b.resourceid in(Select ResourceId from #Resources)

    group by  
    b.projectcode,
    b.taskuid,
    f.outlinenum,
    f.taskname,
    b.ActivityCode,
    c.ActivityDesc,
    b.resourceID,
    OrgUnit,
    e.firstname,
    e.lastname,

    case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
    case when @TimeDetail = 3 then b.invoicecomment else '' end,
    Case when @IncludeID = 1 then b.timeID else '' end, --mod 07
    case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008
    having sum(isnull(b.StandardHours,0)) <> 0 

1 个答案:

答案 0 :(得分:2)

你不能在group by子句中使用别名,尝试加入表并按如下所示使用

select ...
       RES.OrganizationID as OrgUnit 
       ...   
from 
...
join #Resources RES on RES.resourceID = b.resourceID
...
where 
group by 
...
RES.OrganizationID
...
having 
...