我的查询返回一些重复记录。我已经弄清楚原因是某些资产在PS_ASSET_ACQ_DET表中有2条记录。这是由于某些用户群设置资产的方式。
在PS_ASSET_ACQ_DET表中,有一个名为" seq_nbr_6"的字段。每个新记录都会增加。我想为每个资产选择此表中具有最大价值的记录。
我尝试在SELECT语句中使用MAx(seq_nbr_6),然后在SELECT语句中的所有其他字段上使用GROUP BY,但这对我没有用。
我正在寻找一种只从PS_ASSET_ACQ_DET表返回1条记录的方法。感谢您的帮助,并花时间看看这个问题......
SELECT
pdr.ACCOUNT_FA AS acct,
ir.ERAC_BRANCH_LGCY_CD AS gpbr,
pdr.DEPTID,
pa.ASSET_ID,
pa.serial_id,
pa.tag_number,
pa.asset_type,
pa.DESCR,
pb.IN_SERVICE_DT,
pb.LIFE,
pdr.COST,
pant.ACCUM_DEPR AS accum_depr,
pant.NET_BK_VALUE AS net_book_value,
pdr.FISCAL_YEAR AS fy,
pdr.ACCOUNTING_PERIOD AS ap,
pdr.category,
pad.voucher_id,
pad.po_id,
pdr.location AS loc1,
pant.location AS loc2
FROM PSFS.PS_ASSET pa
INNER JOIN PSFS.PS_BOOK pb ON pb.ASSET_ID= pa.ASSET_ID AND pb.BUSINESS_UNIT = pa.BUSINESS_UNIT
INNER JOIN PSFS.PS_DEPR_RPT pdr ON pdr.ASSET_ID= pb.ASSET_ID AND pdr.BUSINESS_UNIT = pb.BUSINESS_UNIT AND pdr.BOOK = pb.BOOK
INNER JOIN PSFS.PS_ASSET_ACQ_DET pad ON pad.ASSET_ID= pb.ASSET_ID AND pad.BUSINESS_UNIT = pb.BUSINESS_UNIT AND pdr.BOOK = pb.BOOK
INNER JOIN PSFS.PS_ASSET_NBV_TBL pant ON pant.BUSINESS_UNIT = pb.BUSINESS_UNIT
AND pant.ASSET_ID = pb.ASSET_ID
AND pant.BOOK = pb.BOOK
INNER JOIN INTGRT_RPT.DIM_LOCATION IR ON pdr.deptid = ir.erac_branch_ps_org_cd AND ir.curr_lrd_row_flg = 1
WHERE pdr.ACCOUNT_FA IN ('130','150','315',330','350')
AND pdr.FISCAL_YEAR =2016
AND pdr.ACCOUNTING_PERIOD =3
AND pdr.GROUP_ASSET_FLAG <> 'M'
答案 0 :(得分:0)
尝试将psfs.ps_asset_acq_det上的联接更改为:
INNER JOIN( SELECT MAX(seq_nbr_6), voucher_id, po_id, asset_id
FROM psfs.ps_asset_acq_det
WHERE business_unit = p
GROUP BY voucher_id, po_id, asset_id
) pad ON pad.ASSET_ID= pb.ASSET_ID