SQL查询异常

时间:2015-07-22 16:11:28

标签: sql

以下查询排除了AMT余额为0的所有帐户。

虽然我试图为帐户L1_ORG_SEG_ID = 101, 102 and 105设置例外,即使它的值等于0.

有人能指出我正确的方向吗?谢谢!

SELECT 'Table' colA
    ,O.L1_ORG_SEG_ID colb
    ,A.L1_ACCT_SEG_ID colc
    ,A.L2_ACCT_SEG_ID cold
    ,A.L3_ACCT_SEG_ID cole
    ,' ' colf
    ,A.ACCT_NAME colg
    ,'USD' colh
    ,SUM(G.AMT) coli
    ,' ' colj
    ,' ' colk
    ,' ' coll
    ,' ' colm
    ,'4' coln
    ,'2015' colo
    ,'4/24/15' colp
    ,' ' colq
    ,' ' colr
    ,' ' cols
    ,' ' colt
    ,' ' colu
    ,' ' colv
FROM GL_POST_SUM G
INNER JOIN ACCT A ON A.ACCT_ID = G.ACCT_ID
INNER JOIN ORG O ON O.ORG_ID = G.ORG_ID
WHERE G.FY_CD = '2015'
    AND G.PD_NO < 5
    AND A.S_ACCT_TYPE_CD IN (
        'L'
        ,'A'
        )
    AND G.ORG_ID NOT LIKE 'J%'
    AND A.ACTIVE_FL = 'Y'
    AND O.L1_ORG_SEG_ID NOT IN (
        '125'
        ,'126'
        ,'127'
        ,'129'
        )
GROUP BY O.L1_ORG_SEG_ID
    ,A.L1_ACCT_SEG_ID
    ,A.L2_ACCT_SEG_ID
    ,A.L3_ACCT_SEG_ID
    ,A.ACCT_NAME
HAVING SUM(G.AMT) <> 0
ORDER BY 2

4 个答案:

答案 0 :(得分:0)

您可以在HAVING中删除排除条件,因此它始终包含指定的帐户,如下所示:

HAVING (SUM(G.AMT) <> 0 OR O.L1_ORG_SEG_ID IN ('101', '102', '105'))

答案 1 :(得分:0)

这样的事情应该有效

SELECT 'Table' colA,O.L1_ORG_SEG_ID colb,A.L1_ACCT_SEG_ID colc,
A.L2_ACCT_SEG_ID cold,A.L3_ACCT_SEG_ID cole, ' ' colf,A.ACCT_NAME colg,
'USD' colh,SUM(G.AMT) coli,' '  colj,' '  colk,' '  coll,' '  colm,'4'
coln,'2015'  colo,
'4/24/15'  colp,' '  colq,' '  colr,' '  cols,' '  colt,' '  colu,' '  colv
FROM GL_POST_SUM G 
JOIN ACCT A ON A.ACCT_ID = G.ACCT_ID
JOIN ORG O ON O.ORG_ID = G.ORG_ID
WHERE G.FY_CD='2015' AND G.PD_NO < 5
AND A.S_ACCT_TYPE_CD IN ('L','A')
AND G.ORG_ID NOT LIKE 'J%'
AND A.ACTIVE_FL = 'Y'     
AND O.L1_ORG_SEG_ID NOT IN ('125', '126', '127', '129')
GROUP BY O.L1_ORG_SEG_ID,A.L1_ACCT_SEG_ID,A.L2_ACCT_SEG_ID,A.L3_ACCT_SEG_ID,A.ACCT_NAME
HAVING SUM(G.AMT) <> 0 OR O.L1_ORG_SEG_ID in (101, 102, 105)
order by 2

答案 2 :(得分:0)

您想将条件移至having子句,请尝试以下代码:

SELECT 'Table' colA,O.L1_ORG_SEG_ID colb,A.L1_ACCT_SEG_ID colc,
A.L2_ACCT_SEG_ID cold,A.L3_ACCT_SEG_ID cole, ' ' colf,A.ACCT_NAME colg,
'USD' colh,SUM(G.AMT) coli,' '  colj,' '  colk,' '  coll,' '  colm,'4'
coln,'2015'  colo,
'4/24/15'  colp,' '  colq,' '  colr,' '  cols,' '  colt,' '  colu,' '  colv
FROM GL_POST_SUM G 
JOIN ACCT A ON A.ACCT_ID = G.ACCT_ID
JOIN ORG O ON O.ORG_ID = G.ORG_ID
WHERE G.FY_CD='2015' AND G.PD_NO < 5
AND A.S_ACCT_TYPE_CD IN ('L','A')
AND G.ORG_ID NOT LIKE 'J%'
AND A.ACTIVE_FL = 'Y'     
GROUP BY
O.L1_ORG_SEG_ID,A.L1_ACCT_SEG_ID,A.L2_ACCT_SEG_ID,A.L3_ACCT_SEG_ID,A.ACCT_NAME
HAVING (SUM(G.AMT) <> 0 OR O.L1_ORG_SEG_ID IN ('125', '126', '127', '129'))
order by 2

答案 3 :(得分:0)

这应该就像在OR子句中添加Having运算符一样简单。这样,它将使符合SUM(AMT)不等于0的条件的记录或具有101,102 and 105帐户的记录。

SELECT 'Table'          AS colA
    ,O.L1_ORG_SEG_ID    AS colb
    ,A.L1_ACCT_SEG_ID   AS colc
    ,A.L2_ACCT_SEG_ID   AS cold
    ,A.L3_ACCT_SEG_ID   AS cole
    ,''                 AS colf
    ,A.ACCT_NAME        AS colg
    ,'USD'              AS colh
    ,SUM(G.AMT)         AS coli
    ,''                 AS colj
    ,''                 AS colk
    ,''                 AS coll
    ,''                 AS colm
    ,'4'                AS coln
    ,'2015'             AS colo
    ,'4/24/15'          AS colp
    ,''                 AS colq
    ,''                 AS colr
    ,''                 AS cols
    ,''                 AS colt
    ,''                 AS colu
    ,''                 AS colv
FROM GL_POST_SUM G
INNER JOIN ACCT A ON A.ACCT_ID = G.ACCT_ID
INNER JOIN ORG O ON O.ORG_ID = G.ORG_ID
WHERE G.FY_CD = '2015'
    AND G.PD_NO < 5
    AND A.S_ACCT_TYPE_CD IN ('L','A')
    AND G.ORG_ID NOT LIKE 'J%'
    AND A.ACTIVE_FL = 'Y'
    AND O.L1_ORG_SEG_ID NOT IN ('125','126','127','129')
GROUP BY O.L1_ORG_SEG_ID
    ,A.L1_ACCT_SEG_ID
    ,A.L2_ACCT_SEG_ID
    ,A.L3_ACCT_SEG_ID
    ,A.ACCT_NAME
HAVING SUM(G.AMT) <> 0
       OR O.L1_ORG_SEG_ID IN (101,102,105) --add this OR to your having clause
ORDER BY 2