假设有一家餐厅,里面有以下桌子 -
@Entity(tablename = "pizza-sales")
data class Pizza(
val id,
val name,
val price,
val quantitySold,
val date,
) //Pizza sales of a day
@Entity(tablename = "burger-sales")
data class Burger(
val id,
val name,
val price,
val quantitySold,
val date,
) //Burger sales of a day
现在,我想要第三个表(总销售额),其中的行是根据上面的表格计算出来的
@Entity(tablename= "total-sales")
data class TotalSales(
val id,
val date,
val totalQuantity = Sum(Pizza[quantitySold]) + Sum(Burger[quantitySold]),
val totalPrice = Sum(Pizza[quantitySold * price]) + Sum(Burger[quantitySold * price])
) //Total sales of a day
请注意,我对 Pizza、Burger 进行了求和,其中 TotalSales.date = (Burger/Pizza).date。
另外,是否可以在查询中进行这样的算术运算-
@Dao
interface ToatalSalesDao {
@Query(
"ADD pizza-sales.quantitySold WHERE pizza-sales.date = :date"+
"ADD"+
"ADD burger-sales.quantitySold WHERE burger-sales.date = :date"
)
fun loadTotalSalesQuantityOfTheDay(date: String): Int
}