我是编写SQL代码的新手,当我尝试运行以下代码时,我得到了一个无效的标识符。我想知道我试图求和的字段是否会引起我的问题。
我试图获得1个记录,其中depr和accumdepr字段已被添加到一起
我也想知道你是否有任何可以建议的资源可以帮助我变得更加愚蠢。任何帮助将非常感谢.....
SELECT pa.BUSINESS_UNIT as bu,
pc.DEPTID as deptid,
pa.ASSET_ID as assetnumber,
pa.DESCR as descr,
pa.IN_SERVICE_DT as inservdate,
pc.COST as assetcost,
pb.METHOD as deprecmeth,
pb.LIFE as assetlife,
Sum(pdr.DEPR) as depr,
Sum(pdr.DEPR_YTD) as accumdepr
from PS_ASSET pa
inner join PS_COST pc on pc.ASSET_ID = pa.ASSET_ID
inner join PS_BOOK pb on pb.ASSET_ID= pa.ASSET_ID
inner join PS_DEPR_RPT pdr on pdr.ASSET_ID= pa.ASSET_ID
where pa.BUSINESS_UNIT = 'A0057'
and pa.ASSET_ID = '000000000020'
and pdr.FISCAL_YEAR = '2014'
and pdr.ACCOUNTING_PERIOD = '11'
group by bu,
deptid,
assetnumber,
descr,
inservicedate,
assetcost,
deprecmeth,
assetlife,
depr,
accumdepr;
答案 0 :(得分:1)
GROUP BY子句应仅包含用于标识属于同一组的行的列。由于您对pdr.DEPR和pdr.DEPR_YTD的SUM感兴趣,我假设您对具有相同bu,deptid,assetnumber,descr,inservciedate,asstecost,deprecmeth和assetlife的行的SUM感兴趣。 SUM(dpr.DEPR)和SUM(dpr.DEPR_YTD)是您要查找的计算。
要解决此问题,您需要删除GROUP BY中的最后两个条目,如下所示:
GROUP BY bu,
deptid,
assetnumber,
descr,
inservicedate,
assetcost,
deprecmeth,
assetlife;
答案 1 :(得分:0)
您无法引用您在select中定义的列的别名,因此请执行此操作。此外,您不能对组功能进行分组,因此开始使用accumdepr组没有多大意义。
SELECT pa.BUSINESS_UNIT as bu,
pc.DEPTID as deptid,
pa.ASSET_ID as assetnumber,
pa.DESCR as descr,
pa.IN_SERVICE_DT as inservdate,
pc.COST as assetcost,
pb.METHOD as deprecmeth,
pb.LIFE as assetlife,
Sum(pdr.DEPR) as depr,
Sum(pdr.DEPR_YTD) as accumdepr
from PS_ASSET pa
inner join PS_COST pc on pc.ASSET_ID = pa.ASSET_ID
inner join PS_BOOK pb on pb.ASSET_ID= pa.ASSET_ID
inner join PS_DEPR_RPT pdr on pdr.ASSET_ID= pa.ASSET_ID
where pa.BUSINESS_UNIT = 'A0057'
and pa.ASSET_ID = '000000000020'
and pdr.FISCAL_YEAR = '2014'
and pdr.ACCOUNTING_PERIOD = '11'
group by
pa.BUSINESS_UNIT,
pc.DEPTID,
pa.ASSET_ID,
pa.DESCR,
pa.IN_SERVICE_DT,
pc.COST,
pb.METHOD,
pb.LIFE;