用于计算父项的子值总计的SQL查询

时间:2013-01-24 10:54:10

标签: sql netezza

我有以下查询:

select
   AD.ALLOCATION_INSTR_ID AS "Allocation Id"
  ,AD.ALLOCATION_INSTR_VERSION AS "Allocation Version"
  ,SUM(AC.CHARGE_AMOUNT) AS "Allocation Commission"
  ,AD.ALLOCATION_ID AS "Sub Allocation Id"
  ,AD.ALLOCATION_VERSION AS "Sub Allocation Version"
  ,AC.CHARGE_AMOUNT AS "Allocation Commission"
  ,B.BLOCK_ID AS "Block Id"
  ,B.BLOCK_VERSION AS "Block Version"
  ,B.BLOCK_SOURCE_SYSTEM AS "Block Source System"
  ,BC.CHARGE_AMOUNT AS "Block Commission"
FROM  EQ_MO_ALLOCATION_DETAIL AS AD 
  LEFT OUTER JOIN EQ_MO_ALLOCATION_DETAIL_CHARGE AS AC 
      ON AC.ALLOCATION_DETAIL_OID = AD.ALLOCATION_DETAIL_OID 
  JOIN EQ_MO_ALLOCATION_INSTR AS AI 
      ON AI.ALLOCATION_INSTR_ID = AD.ALLOCATION_INSTR_ID
      AND AI.ALLOCATION_INSTR_VERSION = AD.ALLOCATION_INSTR_VERSION
  JOIN EQ_MO_ALLOCATION_INSTR_LINKAGE AS AL
      ON AI.ALLOCATION_INSTR_OID = AL.ALLOCATION_INSTR_OID 
  JOIN EQ_MO_BLOCK AS B
      ON B.BLOCK_ID = AL.LINK_PARENT_ID
      AND B.BLOCK_VERSION = AL.LINK_PARENT_VERSION
  LEFT OUTER JOIN EQ_MO_BLOCK_CHARGE AS BC 
      ON BC.BLOCK_OID = B.BLOCK_OID
WHERE AD.LAST_UPDATE_TIME BETWEEN '2013-01-17 00:00:00' AND '2013-01-18 00:00:00' 
AND AC.CHARGE_NAME = 'Commission' 
AND BC.CHARGE_NAME = 'COMMISSION'
AND AD.ALLOCATION_INSTR_VERSION = (SELECT MAX(AD1.ALLOCATION_INSTR_VERSION) 
                       FROM EQ_MO_ALLOCATION_DETAIL AD1
                       WHERE AD1.ALLOCATION_INSTR_VERSION = AD.ALLOCATION_INSTR_VERSION)
AND B.BLOCK_TIME = (SELECT MAX(b1.BLOCK_TIME)
                    FROM EQ_MO_BLOCK b1
                    WHERE b1.BLOCK_ID = B.BLOCK_ID
                   AND b1.BLOCK_VERSION = B.BLOCK_VERSION)
AND AI.VENDOR_PRODUCT = 'OA'
GROUP BY AD.ALLOCATION_INSTR_ID
ORDER BY 1,2

我正在尝试通过收集费用总额来计算分配佣金总额,并显示与父ID“分配ID”的对比

然而,我的小组是一些脱节的东西:

我将分组的子佣金加入他们的父母,该父母位于另一张桌子中,但我通过以下方式加入这两个:

LEFT OUTER JOIN EQ_MO_ALLOCATION_DETAIL_CHARGE AS AC 
  ON AC.ALLOCATION_DETAIL_OID = AD.ALLOCATION_DETAIL_OID

表AD包含可在第一个选择中看到的父ID。

但是我给出了以下错误:

ERROR:  Attribute AD.ALLOCATION_INSTR_VERSION must be GROUPed or used in an aggregate function

1 个答案:

答案 0 :(得分:0)

如果您GROUP BY AD.ALLOCATION_INSTR_ID,那么Select语句中不属于Group by子句的所有其他列都需要成为聚合函数的一部分,例如Max(AD.ALLOCATION_INSTR_VERSION)

如果您使用Group by的唯一原因是获取SUM(AC.CHARGE_AMOUNT),那么您可以考虑在子查询中执行此操作,然后将其加入主Select语句。

修改: -

以下内容可能会产生预期效果(未经测试):

SELECT
   AD.ALLOCATION_INSTR_ID AS "Allocation Id"
  ,AD.ALLOCATION_INSTR_VERSION AS "Allocation Version"
  ,J.Commission AS "Allocation Commission"
  ,AD.ALLOCATION_ID AS "Sub Allocation Id"
  ,AD.ALLOCATION_VERSION AS "Sub Allocation Version"
  ,AC.CHARGE_AMOUNT AS "Allocation Commission"
  ,B.BLOCK_ID AS "Block Id"
  ,B.BLOCK_VERSION AS "Block Version"
  ,B.BLOCK_SOURCE_SYSTEM AS "Block Source System"
  ,BC.CHARGE_AMOUNT AS "Block Commission"
FROM  EQ_MO_ALLOCATION_DETAIL AS AD
LEFT OUTER JOIN EQ_MO_ALLOCATION_DETAIL_CHARGE AS AC
    ON AC.ALLOCATION_DETAIL_OID = AD.ALLOCATION_DETAIL_OID
JOIN EQ_MO_ALLOCATION_INSTR AS AI
    ON AI.ALLOCATION_INSTR_ID = AD.ALLOCATION_INSTR_ID
    AND AI.ALLOCATION_INSTR_VERSION = AD.ALLOCATION_INSTR_VERSION
JOIN EQ_MO_ALLOCATION_INSTR_LINKAGE AS AL
    ON AI.ALLOCATION_INSTR_OID = AL.ALLOCATION_INSTR_OID
JOIN EQ_MO_BLOCK AS B
    ON B.BLOCK_ID = AL.LINK_PARENT_ID
    AND B.BLOCK_VERSION = AL.LINK_PARENT_VERSION
LEFT OUTER JOIN EQ_MO_BLOCK_CHARGE AS BC
    ON BC.BLOCK_OID = B.BLOCK_OID
RIGHT JOIN (SELECT
            AD.ALLOCATION_INSTR_ID AS "AllocId"
            ,SUM(AC.CHARGE_AMOUNT) AS "Commission"
        FROM  EQ_MO_ALLOCATION_DETAIL AS AD
        LEFT OUTER JOIN EQ_MO_ALLOCATION_DETAIL_CHARGE AS AC
            ON AC.ALLOCATION_DETAIL_OID = AD.ALLOCATION_DETAIL_OID
        JOIN EQ_MO_ALLOCATION_INSTR AS AI
            ON AI.ALLOCATION_INSTR_ID = AD.ALLOCATION_INSTR_ID
            AND AI.ALLOCATION_INSTR_VERSION = AD.ALLOCATION_INSTR_VERSION
        JOIN EQ_MO_ALLOCATION_INSTR_LINKAGE AS AL
            ON AI.ALLOCATION_INSTR_OID = AL.ALLOCATION_INSTR_OID
        JOIN EQ_MO_BLOCK AS B
            ON B.BLOCK_ID = AL.LINK_PARENT_ID
            AND B.BLOCK_VERSION = AL.LINK_PARENT_VERSION
        LEFT OUTER JOIN EQ_MO_BLOCK_CHARGE AS BC
            ON BC.BLOCK_OID = B.BLOCK_OID
        WHERE AD.LAST_UPDATE_TIME BETWEEN '2013-01-17 00:00:00' AND '2013-01-18 00:00:00'
        AND AC.CHARGE_NAME = 'Commission'
        AND BC.CHARGE_NAME = 'COMMISSION'
        AND AD.ALLOCATION_INSTR_VERSION = (
            SELECT MAX(AD1.ALLOCATION_INSTR_VERSION)
            FROM EQ_MO_ALLOCATION_DETAIL AD1
            WHERE AD1.ALLOCATION_INSTR_VERSION = AD.ALLOCATION_INSTR_VERSION)
        AND B.BLOCK_TIME = (
            SELECT MAX(b1.BLOCK_TIME)
            FROM EQ_MO_BLOCK b1
            WHERE b1.BLOCK_ID = B.BLOCK_ID
            AND b1.BLOCK_VERSION = B.BLOCK_VERSION)
        AND AI.VENDOR_PRODUCT = 'OA'
        GROUP BY AD.ALLOCATION_INSTR_ID) AS J
ON J.AllocId = AD.ALLOCATION_INSTR_ID
ORDER BY 1,2