sql diplays重复行 - 需要总数

时间:2015-07-07 16:21:39

标签: sql teradata

当运行以下查询时,资产“1311”有2条相同的行。该表有2行用于该资产的distribution_type“DE”。一条线为当月折旧为13.25美元,第二条线为前期调整为291.50美元。我要做的是修改报告,使其显示1行,价格为304.75美元。

我认为我因为MAX而得到2行,但我不知道如何纠正它以获得总额的1行。我尝试用SUM替换MAX但是没有用。谢谢你的帮助。

WITH query_1_results AS

  (SELECT 
    ir.ERAC_BRANCH_LGCY_CD as ERACBR,
    pa.BUSINESS_UNIT, 
    pa.DESCR,
    pdr.DEPTID, 
    pdr.ASSET_ID, 
    pdr.ACCOUNT_AD,

  MAX(CASE WHEN ((pdr.FISCAL_YEAR =2015) AND (pdr.ACCOUNTING_PERIOD =11)) THEN  pdr.DEPR END) AS CURRENT_AMT,
  MAX(CASE WHEN ((pdr.FISCAL_YEAR = 2015) AND (pdr.ACCOUNTING_PERIOD =10)) THEN pdr.DEPR  END) AS PRIOR_AMT

  FROM PSFS.PS_ASSET pa

INNER JOIN PSFS.PS_DEPR_RPT pdr 
ON pa.ASSET_ID = pdr.ASSET_ID
AND pa.BUSINESS_UNIT = pdr.BUSINESS_UNIT

INNER JOIN INTGRT_RPT.DIM_LOCATION IR on pdr.deptid = ir.erac_branch_ps_org_cd and ir.curr_lrd_row_flg = 1

INNER JOIN RFS.STN_OPS_HIERARCHIES SOH ON pdr.deptid = soh.department

  WHERE 
    pa.BUSINESS_UNIT='A0150'
    AND pdr.BOOK='PERFORM'
    AND ((pdr.FISCAL_YEAR=  2015 AND pdr.ACCOUNTING_PERIOD=11) 
    OR (pdr.FISCAL_YEAR=2015 AND pdr.ACCOUNTING_PERIOD=10))
--and ( ? is null or soh.REGION_CD = ?)

  GROUP BY

    ir.ERAC_BRANCH_LGCY_CD,
    pa.business_unit,
    pa.descr,
    pdr.deptid,
    pdr.asset_id,
    pdr.account_ad)

  SELECT 
   q1.*,
    pdl.acct, 
    pdl.journal_id,
    pdl.journal_date

FROM query_1_results  q1

LEFT OUTER JOIN  psfs.ps_dist_ln pdl 
ON   pdl.asset_id = q1.asset_id
    AND  pdl.book = 'PERFORM'
    AND  pdl.business_unit ='A0150'
    AND  pdl.fiscal_year =2015
    AND  pdl.accounting_period =11
    AND  pdl.distribution_type = 'DE'

ORDER BY  q1.account_ad, q1.eracbr

0 个答案:

没有答案