公共表表达式已定义但未使用

时间:2017-01-18 16:59:36

标签: sql sql-server sql-server-2008 tsql

我无法弄清楚为什么会收到此错误。我当然想尝试使用名为" dvr_base"的cte。你可以在内部选择中看到dvr_base上有一个带有aliad d的连接。所有的" d。" select中的列表示"多部分列无法绑定"

    with dvr_base (program, cawpid, cecode, costrep, period, hours, direct, overhead, mhxdev, plwdev, ganda, comlabor, comganda, tcost)
as
    (
        select
            program,
            cawpid,
            cecode,
            costset,
            right('0000' + cast(datepart(year, pd_finish) as varchar(4)), 4) + right('00' + cast(datepart(month, pd_finish) as varchar(2)), 2),
            sum(hours),
            sum(direct),
            sum(overhead),
            sum(mhxdev),
            sum(plwdev),
            sum(ganda),
            sum(COMLABOR),
            sum(COMGANDA),
            sum(direct+overhead+mhxdev+plwdev+ganda+comlabor+comganda)
        from
            tphase join costdetl u on tphase.class = u.class and u.instance = @costdetlid
            join rcutoff r on tphase.df_date between r.pd_start and r.pd_finish
        where
            tphase.program = @project and
            tphase.df_date <= @statusdate and
            r.instance = @cutoffid
        group by
            tphase.program,
            tphase.cawpid,
            tphase.cecode,
            u.costset,
            r.pd_finish
    )

select
    c.program as ProjectID,
    c.ca3 as "Control Acct",
    c.descrip as "Control Acct Description",
    c.ca2 as OBS,
    c.ca1 as WBS,
    d.cecode as Resource,
    w.d1 as "Resource Type",
    d.costrep as "Cost Type",
    d.period as "YYYYMM",
    d.hours as Hours,
    d.direct as Direct,
    d.overhead as Overhead,
    d.mhxdev as MHX,
    d..plwdev as PLW,
    d..ganda as "G&A",
    d.COMLABOR as "COM Labor",
    d..COMGANDA) as "COM G&A",
    d.tcost as "Total Cost"
from
    cawp c join dvr_base d on
    c.program = d.program and c.cawpid = d.cawpid
    join calcdesc w on d.cecode = w.cecode and w.calcfile = @calcfile
where
    c.program = @project and
order by
    c.program,
    c.ca3,
    c.descrip,
    c.ca2,
    c.ca1,
    d.cecode,
    w.d1,
    d.period
;

1 个答案:

答案 0 :(得分:3)

您的查询中存在一些问题:

  1. d..COMGANDA) as "COM G&A",有一个额外的右括号和一个额外的点;

  2. c.program = @project and有一个额外的and;

  3. 多行有一个额外的点(例如:d..COMGANDA)。它应该是一个点d.COMGANDA

  4. 修复上述问题后, 已定义但未使用的公用表表达式 错误消失。