2013-12-17 92 views
0

您好我需要幫助解決這個存儲過程,是不是有什麼毛病我查詢?按分組分組。我收到此錯誤消息消息207,級別16,狀態1,行131 無效的列名稱'OrgUnit'。消息207,級別16,狀態1,行131無效列名「單位部門」

-- 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 

回答

2

不能通過子句中使用別名組,嘗試加入與該表使用如下

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

嗨parado,這裏是做後的錯誤消息。消息144,級別15,狀態1,行75 不能在用於GROUP BY子句的group by列表的表達式中使用聚合或子查詢。 – BA82283

+0

@ BA82283請嘗試更新的答案。 – Parado

+0

我希望我能和你一樣好。謝謝你解決這個問題。 – BA82283

相關問題