无法使用WITH关键字进行子查询的sql查询
这是原始查询:
with policy_debit(TOTAL_POLICY_DEBIT_AMOUNT) as
(
select SUM(POLICY_DEBIT_AMOUNT) as TOTAL_POLICY_DEBIT_AMOUNT from
(select
CASE WHEN (ais.BILLING_TYPE_ID = 1 AND ai.STORED_INST_BALANCE <> 0 )
THEN ((ISNULL(STORED_INST_BALANCE,0) + ISNULL(ait.COMMISSION_AMOUNT,0)
+ ISNULL(ait.AWARDED_COMMISSION_AMOUNT,0)))
WHEN (ais.BILLING_TYPE_ID = 2 AND ISNULL(ai.STORED_INST_AMOUNT,0)-ISNULL(ait.PAID_AMOUNT_IN_BODRO,0) != 0)
THEN (ISNULL(ai.STORED_INST_AMOUNT,0) - ISNULL(ait.PAID_AMOUNT_IN_BODRO,0)
+ISNULL(ait.COMMISSION_AMOUNT,0) + ISNULL(ait.AWARDED_COMMISSION_AMOUNT,0))
END as POLICY_DEBIT_AMOUNT
from AC_INSTALLMENT ai
inner join AC_INSTALLMENT_TRK ait on ai.ID = ait.ID
inner join AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
inner join P_POLICY pol on ai.POLICY_ID = pol.ID
where
pol.AGENT_ACCOUNT_ID = 3039262
and ai.INSTALLMENT_STATUS not in (4,10005)-- Installment Status not Cancelled or Archived
and ((ais.BILLING_TYPE_ID = 1 AND ai.STORED_INST_BALANCE <> 0) OR
(ais.BILLING_TYPE_ID = 2 AND ISNULL(ai.STORED_INST_AMOUNT,0)-ISNULL(ait.PAID_AMOUNT_IN_BODRO,0) != 0))
and convert(Date,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
and ai.ID not in
( select ai2.ID from
AC_INSTALLMENT ai2 left join AC_CHEQUE_INST_MATCHING cim on ai2.ID = cim.INSTALLMENT_ID
left join AC_CHEQUE_MATCHING cm on cm.ID = cim.CHEQUE_MATCHING_DETAILS_ID
left join AC_CHEQUE ac on ac.ID = cm.CHEQUE_ID
left join P_POLICY pol on pol.ID = ai2.POLICY_ID
where pol.AGENT_ACCOUNT_ID = 3039262
and(
((ai2.COLLECTION_METHOD_ID=1000001 and convert(Date,ais.INTERMEDIARY_DUE_DATE) >= convert(DATE,getdate()))
or( ai2.COLLECTION_METHOD_ID=1 and ac.CHEQUE_STATUS_ID in (10001,1000001)
and (convert(Date,ac.MATURITY_DATE) >= convert(DATE,getdate()) and cm.ACCOUNT_ID=3039262 ))
)
)
)
) as temp
),
--To calculate UNALLOCATED_INTERMEDIARY_COMMISSION, we are first considering the installments which has been paid, (In this case
-- the AC_ENTRY.INSTALLMENT_ID will not be null) and for which the intermediaryDueDate has come
--And then we are considering the installments which has not been paid yet. (in this case, AC_ENTRY.INSTALLMENT_ID is null)
unallocated_commission(UNALLOCATED_INTERMEDIARY_COMMISSION) as (
select sum(ISNULL(ENTRY_BALANCE,0)) as UNALLOCATED_INTERMEDIARY_COMMISSION from (
select aeb.ENTRY_BALANCE from AC_ENTRY ae
inner join AC_ENTRY_BALANCE aeb on ae.ID = aeb.ENTRY_ID
left join AC_INSTALLMENT ai on ai.ID = ae.INSTALLMENT_ID
left join AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
where ae.ACCOUNT_ID = 3039262 -- Intermediary Account Id
and ae.SUPER_ENTRY_TYPE = 5 -- Intermediary Comission
and ae.ENTRY_STATUS in (2,4) -- Payable or Partially Paid
and aeb.ENTRY_BALANCE != 0
and REFERENCE_ID is null
and (ae.INSTALLMENT_ID is null or ae.INSTALLMENT_ID in (
select ai.ID from AC_INSTALLMENT ai JOIN AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID
AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
AND convert(DATE,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
))
) as temp2
),
unpaid_commission(UNPAID_COMMISSION_IN_BORDRO) as (
select SUM(ISNULL(AMOUNT,0)) as UNPAID_COMMISSION_IN_BORDRO
from AC_ENTRY ae
left join AC_INSTALLMENT ai on ae.INSTALLMENT_ID = ai.id
where ae.ACCOUNT_ID = 3039262 -- Intermediary Account Id
and ae.SUPER_ENTRY_TYPE = 5 -- Intermediary Comission
and ae.ENTRY_STATUS = 2 -- Payable
and ae.REFERENCE_ID is not null
and not exists (select * from AC_INSTALLMENT_HISTORY his
JOIN AC_INSTALLMENT_STATUS status ON his.id=status.INSTALLMENT_HISTORY_ID
where status.INSTALLMENT_STATUS = 3
and his.INSTALLMENT_ID = ae.REFERENCE_ID)
and (ae.INSTALLMENT_ID is null or ae.INSTALLMENT_ID in (
select ai.ID from AC_INSTALLMENT ai JOIN AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID
AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
AND convert(DATE,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
))
)
SELECT ((SELECT ISNULL(SUM(TOTAL_POLICY_DEBIT_AMOUNT),0) FROM policy_debit) +
(SELECT ISNULL(SUM(UNPAID_COMMISSION_IN_BORDRO),0) FROM unpaid_commission) +
(SELECT ISNULL(SUM(UNALLOCATED_INTERMEDIARY_COMMISSION),0) FROM unallocated_commission)) as TOTAL_AGENT_DEBT_OR_OPEN_BAL_OR_DUE_DEBT
我只需要添加 select * from (上面的查询)。但它给了我这样的错误 SQL 错误 [156] [S0001]:关键字“with”附近的语法不正确。请帮助我如何将上述查询转换为子查询。
This is the original query :
with policy_debit(TOTAL_POLICY_DEBIT_AMOUNT) as
(
select SUM(POLICY_DEBIT_AMOUNT) as TOTAL_POLICY_DEBIT_AMOUNT from
(select
CASE WHEN (ais.BILLING_TYPE_ID = 1 AND ai.STORED_INST_BALANCE <> 0 )
THEN ((ISNULL(STORED_INST_BALANCE,0) + ISNULL(ait.COMMISSION_AMOUNT,0)
+ ISNULL(ait.AWARDED_COMMISSION_AMOUNT,0)))
WHEN (ais.BILLING_TYPE_ID = 2 AND ISNULL(ai.STORED_INST_AMOUNT,0)-ISNULL(ait.PAID_AMOUNT_IN_BODRO,0) != 0)
THEN (ISNULL(ai.STORED_INST_AMOUNT,0) - ISNULL(ait.PAID_AMOUNT_IN_BODRO,0)
+ISNULL(ait.COMMISSION_AMOUNT,0) + ISNULL(ait.AWARDED_COMMISSION_AMOUNT,0))
END as POLICY_DEBIT_AMOUNT
from AC_INSTALLMENT ai
inner join AC_INSTALLMENT_TRK ait on ai.ID = ait.ID
inner join AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
inner join P_POLICY pol on ai.POLICY_ID = pol.ID
where
pol.AGENT_ACCOUNT_ID = 3039262
and ai.INSTALLMENT_STATUS not in (4,10005)-- Installment Status not Cancelled or Archived
and ((ais.BILLING_TYPE_ID = 1 AND ai.STORED_INST_BALANCE <> 0) OR
(ais.BILLING_TYPE_ID = 2 AND ISNULL(ai.STORED_INST_AMOUNT,0)-ISNULL(ait.PAID_AMOUNT_IN_BODRO,0) != 0))
and convert(Date,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
and ai.ID not in
( select ai2.ID from
AC_INSTALLMENT ai2 left join AC_CHEQUE_INST_MATCHING cim on ai2.ID = cim.INSTALLMENT_ID
left join AC_CHEQUE_MATCHING cm on cm.ID = cim.CHEQUE_MATCHING_DETAILS_ID
left join AC_CHEQUE ac on ac.ID = cm.CHEQUE_ID
left join P_POLICY pol on pol.ID = ai2.POLICY_ID
where pol.AGENT_ACCOUNT_ID = 3039262
and(
((ai2.COLLECTION_METHOD_ID=1000001 and convert(Date,ais.INTERMEDIARY_DUE_DATE) >= convert(DATE,getdate()))
or( ai2.COLLECTION_METHOD_ID=1 and ac.CHEQUE_STATUS_ID in (10001,1000001)
and (convert(Date,ac.MATURITY_DATE) >= convert(DATE,getdate()) and cm.ACCOUNT_ID=3039262 ))
)
)
)
) as temp
),
--To calculate UNALLOCATED_INTERMEDIARY_COMMISSION, we are first considering the installments which has been paid, (In this case
-- the AC_ENTRY.INSTALLMENT_ID will not be null) and for which the intermediaryDueDate has come
--And then we are considering the installments which has not been paid yet. (in this case, AC_ENTRY.INSTALLMENT_ID is null)
unallocated_commission(UNALLOCATED_INTERMEDIARY_COMMISSION) as (
select sum(ISNULL(ENTRY_BALANCE,0)) as UNALLOCATED_INTERMEDIARY_COMMISSION from (
select aeb.ENTRY_BALANCE from AC_ENTRY ae
inner join AC_ENTRY_BALANCE aeb on ae.ID = aeb.ENTRY_ID
left join AC_INSTALLMENT ai on ai.ID = ae.INSTALLMENT_ID
left join AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
where ae.ACCOUNT_ID = 3039262 -- Intermediary Account Id
and ae.SUPER_ENTRY_TYPE = 5 -- Intermediary Comission
and ae.ENTRY_STATUS in (2,4) -- Payable or Partially Paid
and aeb.ENTRY_BALANCE != 0
and REFERENCE_ID is null
and (ae.INSTALLMENT_ID is null or ae.INSTALLMENT_ID in (
select ai.ID from AC_INSTALLMENT ai JOIN AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID
AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
AND convert(DATE,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
))
) as temp2
),
unpaid_commission(UNPAID_COMMISSION_IN_BORDRO) as (
select SUM(ISNULL(AMOUNT,0)) as UNPAID_COMMISSION_IN_BORDRO
from AC_ENTRY ae
left join AC_INSTALLMENT ai on ae.INSTALLMENT_ID = ai.id
where ae.ACCOUNT_ID = 3039262 -- Intermediary Account Id
and ae.SUPER_ENTRY_TYPE = 5 -- Intermediary Comission
and ae.ENTRY_STATUS = 2 -- Payable
and ae.REFERENCE_ID is not null
and not exists (select * from AC_INSTALLMENT_HISTORY his
JOIN AC_INSTALLMENT_STATUS status ON his.id=status.INSTALLMENT_HISTORY_ID
where status.INSTALLMENT_STATUS = 3
and his.INSTALLMENT_ID = ae.REFERENCE_ID)
and (ae.INSTALLMENT_ID is null or ae.INSTALLMENT_ID in (
select ai.ID from AC_INSTALLMENT ai JOIN AC_INSTALLMENT_STATUS ais on ai.ID = ais.INSTALLMENT_ID
AND ai.LAST_INSTALLMENT_STATUS_ID = ais.ID
AND convert(DATE,ais.INTERMEDIARY_DUE_DATE) <= convert(DATE,getdate())
))
)
SELECT ((SELECT ISNULL(SUM(TOTAL_POLICY_DEBIT_AMOUNT),0) FROM policy_debit) +
(SELECT ISNULL(SUM(UNPAID_COMMISSION_IN_BORDRO),0) FROM unpaid_commission) +
(SELECT ISNULL(SUM(UNALLOCATED_INTERMEDIARY_COMMISSION),0) FROM unallocated_commission)) as TOTAL_AGENT_DEBT_OR_OPEN_BAL_OR_DUE_DEBT
I just need to add select * from ( above query). But it is giving me error like
SQL Error [156] [S0001]: Incorrect syntax near the keyword 'with'. Please help me out how can I make the above query to sub query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论