我想在A级聚合数据,并且还显示一个级别的详细信息,比如B. 以下是我在product_line级别(A)聚合金额的查询。
SELECT
pl.business_unit AS "BU"
,p.prod_line
,SUM(i.inv_amt) AS "Amount"
FROM invoice i
LEFT JOIN product p ON p.item_num = i.item_num
LEFT JOIN product_level pl ON pl.pl_num = p.pl_num
WHERE i.date BETWEEN '01-APR-2014' AND '30-APR-2014'
GROUP BY pl.business_unit
,p.prod_line
HAVING SUM(i.inv_amt) > 500000
在invoice
表中,我有一个item_desc
字段(B级)。所有项目描述都汇总到产品系列描述,但如果我将item_desc
添加到主查询,则它将无法正确聚合。我在FROM and SELECT
中尝试了所有类型的子查询但没有正确的结果。我之前没有使用过子查询,但认为这就是我需要的。如果是这样,我如何在prod_line
级别完成聚合,同时为每一行包含item_desc
?谢谢你的帮助!
答案 0 :(得分:0)
如果产品系列是一对一映射到item_desc,那么您可以在查询中添加MAX(item_desc)
SELECT
pl.business_unit AS "BU"
,p.prod_line
,max(p.item_desc)
,SUM(i.inv_amt) AS "Amount"
FROM invoice i
LEFT JOIN product p ON p.item_num = i.item_num
LEFT JOIN product_level pl ON pl.pl_num = p.pl_num
WHERE i.date BETWEEN '01-APR-2014' AND '30-APR-2014'
GROUP BY pl.business_unit
,p.prod_line
HAVING SUM(i.inv_amt) > 500000