select a.Name, a.Accounttypec, a.Territoryc, a.Currency, a.UnappliedCreditMemoAmount,…
- User
select a.Name, a.Accounttype__c, a.Territory__c, a.Currency, a.UnappliedCreditMemoAmount, a.UnappliedBalance, i.Amount, i.Balance, ii.ChargeAmount, ii.Balance, i.DueDate, i.InvoiceDate, i.InvoiceNumber, rpc.ChargeType, CASE WHEN (date_diff('day', Duedate, current_date) > 1 AND date_diff('day', Duedate, current_date) < 30) THEN '1-30 Days' WHEN (date_diff('day', Duedate, current_date) > 31 AND date_diff('day', Duedate, current_date) < 60) THEN '31-60 Days' WHEN (date_diff('day', Duedate, current_date) > 60 AND date_diff('day', Duedate, current_date) < 90) THEN '61-90 Days' WHEN (date_diff('day', Duedate, current_date) > 90 AND date_diff('day', Duedate, current_date) < 120) THEN '91-120 Days' ELSE 'More than 120 Days' END from invoiceitem ii join invoice i on i.Id = ii.InvoiceId join account a on a.Id = i.AccountId join rateplancharge rpc on rpc.Id = ii.RatePlanChargeId where i.Balance > 0 and ii.Balance > 0 and i.Status = 'Posted' Order by i.InvoiceNumber ASC ------How to add 1-30 Days, 31-60 Days, 61-90 Days,91-120 Days & More than 120 Days as column headers
- Assistant
To add the mentioned categories as column headers, you can use the GROUP BY clause in your query. Here is an updated version of your query with the added column headers:
SELECT a.Name, a.Accounttype__c, a.Territory__c, a.Currency, a.UnappliedCreditMemoAmount, a.UnappliedBalance, i.Amount, i.Balance, ii.ChargeAmount, ii.Balance, i.DueDate, i.InvoiceDate, i.InvoiceNumber, rpc.ChargeType, CASE WHEN (date_diff('day', Duedate, current_date) > 1 AND date_diff('day', Duedate, current_date) < 30) THEN '1-30 Days' WHEN (date_diff('day', Duedate, current_date) > 31 AND date_diff('day', Duedate, current_date) < 60) THEN '31-60 Days' WHEN (date_diff('day', Duedate, current_date) > 60 AND date_diff('day', Duedate, current_date) < 90) THEN '61-90 Days' WHEN (date_diff('day', Duedate, current_date) > 90 AND date_diff('day', Duedate, current_date) < 120) THEN '91-120 Days' ELSE 'More than 120 Days' END AS AgeCategory FROM invoiceitem ii JOIN invoice i ON i.Id = ii.InvoiceId JOIN account a ON a.Id = i.AccountId JOIN rateplancharge rpc ON rpc.Id = ii.RatePlanChargeId WHERE i.Balance > 0 AND ii.Balance > 0 AND i.Status = 'Posted' GROUP BY a.Name, a.Accounttype__c, a.Territory__c, a.Currency, a.UnappliedCreditMemoAmount, a.UnappliedBalance, i.Amount, i.Balance, ii.ChargeAmount, ii.Balance, i.DueDate, i.InvoiceDate, i.InvoiceNumber, rpc.ChargeType, AgeCategory ORDER BY i.InvoiceNumber ASC;