Domanda

I did a basic Firebird Report to call on all debtors and transactions The report looks as follows


SELECT 
POSPAY.TXNO,
DEBTORS.COMPANY,
POSPAY.AMOUNT,
POSINVTRANS.TXDATE
FROM
POSPAY
INNER JOIN DEBTORS ON (POSPAY.ACCTNUMBER = DEBTORS.ACCOUNT)
INNER JOIN POSINVTRANS ON (POSPAY.TXNO = POSINVTRANS.TXNO)
WHERE
PAYMNTTYPID = '7' 
and  
weekly = :weekly and
txdate >= :fromdate and
txdate <= :todate

This works correctly and gives me output on Debtor Name, TXNO, TXDATE, AMOUNT

I now want to write a similar report but need to group the debtors and give totals on the transactions ie I need the output Debtor name (If JOHN is twice, need to list once), Total ammount (Sum of John's transactions)

I still need innerjoin on debtors but no longer on posinvtrans, I was thinking it should look something like

SELECT 
POSPAY.TXNO,
DEBTORS.COMPANY,
POSPAY.AMOUNT

FROM
POSPAY
INNER JOIN DEBTORS ON (POSPAY.ACCTNUMBER = DEBTORS.ACCOUNT)

WHERE
PAYMNTTYPID = '7' 
and  
weekly = :weekly and
txdate >= :fromdate and
txdate <= :todate

Group by DEBTORS.COMPANY

but no luck, get errors on Group by 'invalid expression in the select list (not containing in either an aggregate function or the GROUP BY CLAUSE)' any suggestions?

È stato utile?

Soluzione

The list of fields in the select list have to be either also listed in the group by list or be aggregate functions like count(*), max(amount), etc.

The problem is that you have not told Firebird what to do with POSPAY.TXNO and POSPAY.AMOUNT and it is not sufficient to tell what you do want to happen to them.

I suggest you remove those 2 fields from the query and have a select list of DEBTORS.COMPANY, sum(POSPAY.AMOUNT) as a starting point.

Altri suggerimenti

If you use GROUP BY you either need to include a column in the GROUP BY, or apply an aggregate function on the column. In your example you need to leave out POSPAY.TXNO, as that is transaction specific (or you could use the aggregate function LIST), and you need to apply the aggregate function SUM to AMOUNT to get the total:

SELECT 
DEBTORS.COMPANY,
SUM(POSPAY.AMOUNT)

FROM
POSPAY
INNER JOIN DEBTORS ON (POSPAY.ACCTNUMBER = DEBTORS.ACCOUNT)

WHERE
PAYMNTTYPID = '7' 
and  
weekly = :weekly and
txdate >= :fromdate and
txdate <= :todate

Group by DEBTORS.COMPANY
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top