- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我最近跳进了房间,我面临着一个关于别名的奇怪问题。
我将简化这些类,以便我的示例更加清晰。假设我有包含一些产品的账单。
我想获取一段时间内(天、小时、...)的账单列表及其产品。
我需要实体 Bill 和 Product :
Entity Bill.java
@Entity
public class Bill{
@PrimaryKey(autoGenerate = true)
int id;
Date date;
}
Entity Product.java
@Entity
public class Product{
@PrimaryKey(autoGenerate = true)
int id;
int bill_id;
BigDecimal price;
String name;
}
我有一个 POJO,代表一张包含其产品的账单。
POJO BillWithProducts.java :
public class BillWithProducts{
@Embedded
public Bill bill;
@Relation(parentColumn = "id", entityColumn = "bill_id")
public List<Product> products;
}
以及用于查询的 Daos :
Dao BillDao.java
@Dao
public abstract class BillDao{
@Query("select * from bill")
public abstract List<Bill> getAllBills();
@Transaction
@Query("select * from bill where date between :startDate and :endDate")
public abstract List<BillWithProducts> getBillsWithProductsByDate(long startDate, long endDate);
}
Dao ProductDao.java :
@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();
}
到目前为止一切顺利。但是对于显示偏好,我希望在一行中使用相同名称的产品,并相应地添加它们的数量和价格。
为此目的(并且因为我的 Product.java 有很多这里不需要的信息;保持最小化),我创建了我的 Product 类的 POJO。
POJO ProductMinimal.java :
public class ProductMinimal{
int id;
int bill_id;
BigDecimal price;
String name;
int quantity;
BigDecimal sumPrices;
}
我编辑了 BillWithProducts.java 中的关系以获得 ProductMinimal 的列表。
Edited POJO BillWithProducts.java :
public class BillWithProducts{
@Embedded
Bill bill;
@Relation(parentColumn = "id", entityColumn = "bill_id", entity = Product.class)
public List<ProductMinimal> productsMinimal; //<-- error pointing here
}
并在 ProductDao.java 中添加查询。
Edited dao ProductDao.java :
@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();
@Query("select id, bill_id, price, name, " +
"count(*) as quantity, sum(price) as sumPrices " +
"from product " +
"group by name")
public abstract List<ProductMinimal> getAllProductsMinimal();
}
最后,我来这里的原因是我得到了指向 BillWithProducts.java 中的关系的错误...
error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such column: quantity)
当然没有这样的栏目,是别名...
我试过了:
- 为别名添加简单引号,例如“count(*) as 'quantity'”
- 删除计数(*);错误转到 sumPrices
- 将 Product 嵌入到 ProductPojo 中(因此具有“select *”和许多无用的字段)
- 我什至尝试使用 Product 的现有列作为别名...
你们中有人遇到过类似的情况吗?
编辑:我尝试在该数量别名中获取 bill_id,例如:
@Query("select id, bill_id, price, name, " +
"bill_id as quantity " +
"from product " +
"group by name")
public abstract List<ProductMinimal> getAllProductsMinimal();
仍然没有这样的列错误,因此它没有链接到求和或计数操作。
EDIT2:编辑帖子以包含 POJO BillWithProducts.java(它在我的代码中但不在帖子中)。
最佳答案
好的,为了测试,我尝试在我的产品表中添加数量列,代码运行顺利。
问题是我在 BillWithProducts.java 中的 productsMinimal 列表包含数量字段为 0 的所有产品。
好像查询没有考虑分组依据和计数。
(我删除了这个数量栏,它只是为了测试而添加的)
然后,我尝试使用类似的场景查询我的数据库,但使用 getDatabase().productDao().getAllProductsMinimal(bill_id) 调用“超出”此账单/产品关系。
Dao ProductDao.java :
@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();
@Query("select id, bill_id, price, name, " +
"count(*) as quantity, sum(price) as sumPrices " +
"from product " +
"group by name " +
"where bill_id = :bill_id")
public abstract List<ProductMinimal> getAllProductsMinimal(int bill_id);
}
在这里我可以得到我想要的结果!
所以我最终想到的是 @Relation 注释不允许任何条件。
最后,我保留了原始的@Relation,得到了我所有的产品,具有名称冗余,并从原始列表中用 java 中的名称创建了我自己的数量和总和列表。
由于账单上不会有大量产品,而且产品列表仅显示在更多详细信息屏幕上,因此在单击此更多详细信息按钮时构建我的列表不会延迟所需数据的显示。
关于android - Room 中的 Sum 和 Count - 别名没有这样的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50841252/
我有这个示例代码: #include #include int main() { Eigen::MatrixXf M = Eigen::MatrixXf::Random(1000, 1000)
我有一个像这样的数据框: +-----+--------+ |count| country| +-----+--------+ | 12| Ireland| | 5|Thailand| +-
我想要 SUM(tot_bill_1+tot_bill_2) AS 总计,但这不起作用 SELECT *, IF(SUM(bill_1) IS NULL, '99', SUM(bill_1)) AS
如果我们有两个矩阵 X 和 Y,都是二维的,现在在数学上我们可以说:sum(X-Y)=sum(X)-总和(Y). Matlab 哪个效率更高?哪个更快? 最佳答案 在我的机器上,sum(x-y) 对于
我正在运行 Hive 1.1.0 并看到对于两个 bigint 列,active_users 和 inactive_users,SUM(active_users + inactive_users) <
是否可以在一个选择查询中求和? 类似这样的事情: SELECT id, SUM(current_price - bought_price)*amount AS profit FROM purchase
这是一个相当奇怪的结果。我希望这些具有相同的产量。 下面还有从数据库中提取的 excel 链接。 https://twentius.opendrive.com/files?89038281_muoyg
我必须对 2 个字段求和,然后再求和。从性能的角度来看,先添加字段还是在对列求和之后添加字段有什么区别? 方法 1 = SELECT SUM(columnA + columnB) 方法 2 = SEL
这是一个经典问题,但我很好奇是否有可能在这些条件下做得更好。 问题:假设我们有一个长度为4*N的排序数组,即每个元素重复4次。请注意,N 可以是任何自然数。此外,数组中的每个元素都受制于 0 A. 执
我正在编写一个 Pig 程序,该程序加载一个用制表符分隔整个文件的文件 例如:名称 TAB 年份 TAB 计数 TAB... file = LOAD 'file.csv' USING PigStora
我有一个包含以下字段的表: EmpID, Code, Amount, TransDate, CM, CMDate 我想要进入数据网格的是 SUM所有的Amount具有相同的 Code和 SUM CM具
我有两个单独的查询用于提取报告信息。一年效果很好。但是,如果一个月超过 1 年,则不会显示正确的响应。 这是我的两个查询: select SUM(rpt_complete.total) total,
我想查询一个团队的积分。通过在列上执行 SUM + 来自具有相同团队 ID 的另一个表的 SUM 来添加这些点。我试着这样写: SELECT k.id, s.fylke, s.
这个问题在这里已经有了答案: How to deal with floating point number precision in JavaScript? (47 个回答) Unexpected
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 5 年前。 Improve
我已经找了一段时间,但找不到这个问题的答案(也许我没有搜索正确的术语或其他东西)。基本上,我有一个数据库,每个日期有任意数量的条目。我需要取包含条目的最后 X 天的总和(忽略没有条目的天数)。我知道如
我正在尝试获取 B 行中包含 A 行中某个值的所有值中的一些值。我猜这个问题很简单。 这是我的查询: =QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C c
我正在尝试运行以下查询,但出现以下错误: You have an error in your SQL syntax; check the manual that corresponds to your
我有一个 tableA,其中包含以下结构 我将此结构修改为如下所示的tableB,以减少行数,并且类别是固定长度的 假设我在 tableA 中修改为新结构后有 210 万条数据,tableB 仅包含
我的表在 Postgres 中的数据: id user_id sell_amount sell_currency_id buy_amount buy_currency_id type
我是一名优秀的程序员,十分优秀!