按表达式分组错误?
Error with group by expression?
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)
所有未聚合的列都必须包含在 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
(在 SELECT
中),因为 GROUP BY
无论如何都会 select 不同的值。
[编辑:如何将新条件包含到 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 ...
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)
所有未聚合的列都必须包含在 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
(在 SELECT
中),因为 GROUP BY
无论如何都会 select 不同的值。
[编辑:如何将新条件包含到 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 ...