+my @select = (
+ 'Transactions.*',
+ 'Tickets.Id AS ticketid',
+ 'Tickets.Subject',
+ 'Users.name AS otaker',
+);
+my @select_total = ( 'COUNT(*)' );
+
+my $transaction_time;
+my $applied_time = '';
+my $join = 'JOIN Tickets ON Transactions.ObjectId = Tickets.Id '.
+ 'JOIN Users ON Transactions.Creator = Users.Id '.
+ "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)
+ ";
+
+my $where = "WHERE Transactions.ObjectType = 'RT::Ticket'";
+
+# the intrinsic TimeWorked/TimeTaken fields
+$transaction_time = "CASE Transactions.Type when 'Set'