【问题标题】:Msg 207, Level 16, State 1, Line 131 Invalid column name 'OrgUnit'消息 207,级别 16,状态 1,第 131 行无效的列名称“组织单位”
【发布时间】:2013-12-17 10:46:54
【问题描述】:

您好,我需要帮助来解决这个存储过程,我的查询有问题吗?在按部分分组。我收到此错误消息 Msg 207, Level 16, State 1, Line 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 

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案1】:

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

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

    【讨论】:

    • 嗨 parado,这是执行此操作后的错误消息。消息 144,级别 15,状态 1,第 75 行不能在用于 GROUP BY 子句的 group by 列表的表达式中使用聚合或子查询。
    • 我希望有一天我能像你一样优秀。谢谢你解决这个问题。
    • @BA82283 我想你会很快的。所以帮助提高你的技能;)。别忘了你也可以投票;)
    • @BA82283 您可以通过点击向上的箭头进行投票。 Here您可以找到更多信息,阅读页面后您将获得新徽章;)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2012-03-26
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多