Я смог сделать эту работу для меня, используя встроенную аннотацию, позволяющую прямой доступ к полям встроенного типа данных.
public class DayInformation {
@Embedded
public PeriodInformation periodInformation;
@Override
public String toString() {
return "DayInformation{" +
"periodInformation=" + periodInformation +
'}';
}
}
и
public class PeriodInformation {
PeriodInformation(Calendar timestamp,
int periodSpendingCount,
float periodSpendingSum) {
this.timestamp = timestamp;
this.periodSpendingCount = periodSpendingCount;
this.periodSpendingSum = periodSpendingSum;
}
@ColumnInfo(name = "DateTime")
public final Calendar timestamp;
@ColumnInfo(name = "SpendingCount")
public Integer periodSpendingCount;
@ColumnInfo(name = "SpendingSum")
public Float periodSpendingSum;
@Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "PeriodInformation{" +
"timestamp='" + date + '\'' +
", periodSpendingCount=" + periodSpendingCount +
", periodSpendingSum=" + periodSpendingSum +
'}';
}
}
плюс
@Entity
public class Spending {
@PrimaryKey(autoGenerate = true)
public int uid;
@ColumnInfo(name = "calendarDate")
public Calendar timestamp;
@ColumnInfo(name = "value")
public float value;
public Spending(@NonNull Calendar timestamp, float value) {
this.timestamp = timestamp;
this.value = value;
}
@Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "Spending{" +
"uid=" + uid +
", timestamp='" + date + '\'' +
", value=" + value +
'}';
}
}
и DAO
@Dao
public interface SpendingDao {
@Insert
void insertAll(Spending... spendings);
@Query("SELECT * FROM spending")
LiveData<List<Spending>> findAll();
@Query("SELECT calendarDate AS 'DateTime', count(uID) AS 'SpendingCount', sum(value) AS 'SpendingSum' from spending GROUP BY date(datetime(calendarDate))")
LiveData<List<DayInformation>> loadDayInformation();
}
дает следующий вывод
aggregated data is
DayInformation{periodInformation=PeriodInformation{timestamp='Jun 26, 2018', periodSpendingCount=8, periodSpendingSum=184.0}}
spending data is Spending{uid=1, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=2, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=3, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=4, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=5, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=6, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=7, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=8, timestamp='Jun 26, 2018', value=23.0}