在PostgreSQL中这个查询可能有什么问题?

时间:2011-05-25 06:50:46

标签: sql postgresql aggregate-functions

我在Postgres中有这样的查询:

select sum("ATD_AMOUNT") AS CREDIT_SUM, 0 AS DEBIT_SUM,"ATD_ACCOUNT_MST_ID","AAD_OPEN_AMOUNT","AAM_ACCOUNT_NAME", "AAM_ACCOUNT_CODE" ,"AAD_YEAR_ID" 
from db_accounts."ACC_TRANSACTION_DET" 
left outer join db_accounts."ACC_TRANSACTION_MST" ON "ATD_TRANSACTION_MST_ID"=    "ATM_TRANSACTION_MST_ID"
left outer join db_accounts."ACC_ACCOUNT_MST" ON "ATD_ACCOUNT_MST_ID"="AAM_ACCOUNT_MST_ID" 
left outer join db_accounts."ACC_ACCOUNT_DET" on "AAM_ACCOUNT_MST_ID"  = "AAD_ACCOUNT_MST_ID" and "AAD_YEAR_ID"=(select "AAY_YEAR_ID" from db_accounts."ACC_ACCOUNT_YEAR" where "AAY_IS_CURRENT_YEAR"=true)
where "ATM_TRANSACTION_DATE"<= $1 and "ATM_TRANSACTION_DATE">=(select "AAY_START_DATE" from db_accounts."ACC_ACCOUNT_YEAR" where "AAY_IS_CURRENT_YEAR"=true)
and "ATM_ON_REVERSE_PARENT_TRANSACTION_ID" is null and "ATM_IS_CANCELLED"=false
    and  "AAM_DEL_FLAG" =false
AND "ATD_CREDIT_DEBIT_TRANSACTION" = 'CREDIT'
GROUP BY "ATD_ACCOUNT_MST_ID","AAD_OPEN_AMOUNT","AAM_ACCOUNT_NAME", "AAM_ACCOUNT_CODE","AAD_YEAR_ID

这里的db_accounts。“ACC_TRANSACTION_DET”包含大约50万条记录也是db_accounts。“ACC_TRANSACTION_MST”有60000条记录

现在我的问题是当我在我的系统中使用相同的数据库运行此查询时,我得到一个结果,但是当我在其他系统中尝试时它不起作用?

我注意到的是,如果删除以下任何一个条件,它会显示结果

"ATM_TRANSACTION_DATE"<= $1 //field in db_accounts."ACC_TRANSACTION_MST" table

"ATM_TRANSACTION_DATE">=(select "AAY_START_DATE" from db_accounts."ACC_ACCOUNT_YEAR" where "AAY_IS_CURRENT_YEAR"=true) //field in db_accounts."ACC_TRANSACTION_MST" table

"ATD_CREDIT_DEBIT_TRANSACTION" = 'CREDIT' //field in db_accounts."ACC_TRANSACTION_DET" table

1 个答案:

答案 0 :(得分:1)

我已使用www.prettysql.net重新格式化您的查询:

SELECT
 sum("ATD_AMOUNT") AS CREDIT_SUM,
 0 AS DEBIT_SUM,
 "ATD_ACCOUNT_MST_ID",
 "AAD_OPEN_AMOUNT",
 "AAM_ACCOUNT_NAME",
 "AAM_ACCOUNT_CODE",
 "AAD_YEAR_ID"
FROM
 db_accounts."ACC_TRANSACTION_DET" LEFT OUTER JOIN db_accounts."ACC_TRANSACTION_MST"
 ON
   "ATD_TRANSACTION_MST_ID"=
   "ATM_TRANSACTION_MST_ID" LEFT OUTER JOIN db_accounts."ACC_ACCOUNT_MST" ON "ATD_ACCOUNT_MST_ID" = "AAM_ACCOUNT_MST_ID" LEFT OUTER JOIN db_accounts."ACC_ACCOUNT_DET" ON
    "AAM_ACCOUNT_MST_ID" = "AAD_ACCOUNT_MST_ID"
  AND
   "AAD_YEAR_ID"=
   (
    SELECT "AAY_YEAR_ID"
    FROM db_accounts."ACC_ACCOUNT_YEAR"
    WHERE "AAY_IS_CURRENT_YEAR"=true
   )
WHERE
  "ATM_TRANSACTION_DATE"<=$1
 AND
  "ATM_TRANSACTION_DATE">=
  (
   SELECT "AAY_START_DATE"
   FROM db_accounts."ACC_ACCOUNT_YEAR"
   WHERE "AAY_IS_CURRENT_YEAR"=true
  )
 AND
  "ATM_ON_REVERSE_PARENT_TRANSACTION_ID" is null
 AND
  "ATM_IS_CANCELLED"=false
 AND
  "AAM_DEL_FLAG"=false
 AND
  "ATD_CREDIT_DEBIT_TRANSACTION"='CREDIT'
GROUP BY "ATD_ACCOUNT_MST_ID","AAD_OPEN_AMOUNT","AAM_ACCOUNT_NAME","AAM_ACCOUNT_CODE","AAD_YEAR_ID

您的查询中有一个where子句,其中包含$ 1,即Postgresql中用于参数传递的模式。

尝试将其替换为“2011-05-25”:: date,并查看是否有帮助。