1
  SELECT DISTINCT dha.order_number,
                  dha.ordered_date,
                  dha.org_id,
                  houf.name Business_Unit,
                  dha.TRANSACTIONAL_CURRENCY_CODE,
                  dla.ORDERED_QTY,
                  dla.UNIT_SELLING_PRICE,
                  esib.item_number,
                  esit.description,
                  hca.account_name,
                  hzps.PARTY_SITE_NAME,
                  cicev.UNIT_COST_AVERAGE,
                  MAX (cicev.cost_date) AS MaxDate
    FROM DOO_HEADERS_ALL dha,
         DOO_PRICE_ADJUSTMENTS dpa,
         hr_organization_units_f_tl houf,
         DOO_LINES_ALL dla,
         DOO_FULFILL_LINES_ALL dfla,
         egp_system_items_b esib,
         egp_system_items_tl esit,
         hz_cust_accounts hca,
         HZ_party_sites hzps,
         CST_ITEM_COST_ELEMENTS_V cicev
   WHERE     dha.org_id = houf.organization_id
         AND dha.header_id = dla.header_id
         AND esib.inventory_item_id = dfla.inventory_item_id
         AND dfla.inventory_item_id = esit.INVENTORY_ITEM_ID
         AND dfla.header_id = dha.header_id
         AND dha.SOLD_TO_PARTY_ID = hca.party_id
         AND dha.SOLD_TO_PARTY_ID = hzps.party_id
         AND esit.inventory_item_id = cicev.inventory_item_id
GROUP BY (dha.order_number)
4

1 回答 1

2

所有未聚合的列都必须包含在GROUP BY子句中,这意味着它应该看起来像这样;请注意,必须删除列别名!

GROUP BY dha.order_number,
         dha.ordered_date,
         dha.org_id,
         houf.name,
         dha.TRANSACTIONAL_CURRENCY_CODE,
         dla.ORDERED_QTY,
         dla.UNIT_SELLING_PRICE,
         esib.item_number,
         esit.description,
         hca.account_name,
         hzps.PARTY_SITE_NAME,
         cicev.UNIT_COST_AVERAGE

除此之外,不需要DISTINCT(in SELECT) 因为GROUP BY无论如何都会选择不同的值。

[编辑:如何在 WHERE 子句中包含新条件?]

select ...
from ...
where cicev.cost_date = (select max(cicev1.cost_date)
                         from CST_ITEM_COST_ELEMENTS_V cicev1
                         where cicev1.inventory_item_id = cicev.inventory_item_id
                        )
  and ...
于 2018-04-04T08:06:06.767 回答