-my $transaction_time = "( SELECT SUM($transactiontime) $transactions )";
-my $support_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $support )";
+ my $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
+
+ push @select, "$applied_time AS applied_time";
+ push @select_total, "SUM($applied_time)";
+
+}
+
+my $addl_from = " LEFT JOIN (
+ SELECT DISTINCT ON (ObjectId)
+ ObjectId, Content
+ FROM ObjectCustomFieldValues
+ JOIN CustomFields
+ ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
+ WHERE CustomFields.Name = 'TimeType'
+ AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
+ AND ObjectCustomFieldValues.Disabled = 0
+ ORDER BY ObjectId ASC, ObjectCustomFieldValues.LastUpdated DESC
+ ) AS ocfv_TimeType ON (Tickets.Id = ocfv_TimeType.ObjectId)
+";