I would be grateful if someone could help me out with this. I have a sql statement (see below) which is dynamically built from vb. The sql works perfectly exept one line marked with ***
SELECT LBTRMATR.PROJECT_NO, max(LBBALCDE.Name) As BalName, '' As LedgerId , max(LBTRMATR.USER_ID) as 'Author',max(LBTRMATR.NAME) as 'Project Desc', max(LBPRJARC.ARCH_NO)as 'Archive',
min(case when LBBALCDE.Name = 'Project(Updated)' and LBFMBANK.Name = 'ASB TRUST A/C' then LBPRJBAL.Balance end) as 'Project(Updated)/ASB TRUST A/C',
min(case when LBBALCDE.Name = 'Project(Updated)' and LBFMBANK.Name = 'ASB TRUST A/C' and LBBALCDE.balance_code_Id in ('P', 'Z') and lbtrtran.tran_status >= 2 then lbtrtran.tran_status end) as 'Project(Updated)/ASB TRUST A/C Pending' ,
min(case when LBBALCDE.Name = 'Work In Progress' and LBFMBANK.Name = 'ASB TRUST A/C' then LBPRJBAL.Balance end) as 'Work In Progress/ASB TRUST A/C',
min(case when LBBALCDE.Name = 'Work In Progress' and LBFMBANK.Name = 'ASB TRUST A/C' and LBBALCDE.balance_code_Id in ('P', 'Z') and lbtrtran.tran_status >= 2 then lbtrtran.tran_status end) as 'Work In Progress/ASB TRUST A/C Pending' ,
**************************************************************************************************************
min(case when lbprjbal.Balance_Code_id ='P' and lbprjbal.client_acc_no = ' 00023' and lbfmbank.ledgtype_id=1 then sum(LBPRJBAL.Balance) end) as 'Project(Updated)/All Interest Bearing',
*********************************************************************************************
max(case when lbtrtran.client_acc_no = '00023' and lbfmbank.ledgtype_id=1 then lbtrtran.tran_status end) as 'Project(Updated)/All Interest Bearing Pending' ,
FROM LBTRTRAN RIGHT OUTER JOIN LBPRJBAL INNER JOIN LBTRMATR ON LBPRJBAL.PROJECT_NO = LBTRMATR.PROJECT_NO AND LBPRJBAL.CLIENT_ACC_NO = LBTRMATR.CLIENT_ACC_NO INNER JOIN LBFMBANK ON LBPRJBAL.LEDGER_ID = LBFMBANK.LEDGER_ID INNER JOIN LBBALCDE ON LBPRJBAL.BALANCE_CODE_ID = LBBALCDE.BALANCE_CODE_ID ON LBTRTRAN.CLIENT_ACC_NO = LBTRMATR.CLIENT_ACC_NO AND LBTRTRAN.PROJECT_NO = LBTRMATR.PROJECT_NO
AND LBTRTRAN.LEDGER_ID = LBFMBANK.LEDGER_ID LEFT OUTER JOIN LBPRJARC ON LBTRMATR.PROJECT_NO = LBPRJARC.PROJECT_NO AND LBTRMATR.CLIENT_ACC_NO = LBPRJARC.CLIENT_ACC_NO Where LBTRMATR.ACTIVE= 1 AND LBTRMATR.CLIENT_ACC_NO='00023' And LBTRMATR.PROJECT_NO<10000 Group by LBTRMATR.PROJECT_NO ORDER BY LBTRMATR.PROJECT_NO DESC
I understand I can not use Aggregae inside aggregate but I have no idea how to get round it.
I would be grateful for any help.
Thanks