sql where子句中sum的替代方法
我有表1,它代表了订单摘要。表1与表2之间存在一对多关系,表2表示订单中的不同项目。表1有一个“数量”字段,该字段应表示作为订单一部分的单个项目的总数。在表2中,数量字段表示该物料类型的单个物料数量 我想看看有什么不符合这条规则我有理由相信这条规则被打破了sql where子句中sum的替代方法,sql,sql-server,sum,Sql,Sql Server,Sum,我有表1,它代表了订单摘要。表1与表2之间存在一对多关系,表2表示订单中的不同项目。表1有一个“数量”字段,该字段应表示作为订单一部分的单个项目的总数。在表2中,数量字段表示该物料类型的单个物料数量 我想看看有什么不符合这条规则我有理由相信这条规则被打破了 SELECT table1.id, table1.quantity, table2.orderid, table2.itemqty FROM table1 INNER JOIN table2 on tabel1.id = table2.ord
SELECT table1.id, table1.quantity, table2.orderid, table2.itemqty
FROM table1
INNER JOIN table2 on tabel1.id = table2.orderid
WHERE table1.quantity != SUM(table2.itemqty)
可以使用having子句将条件应用于组:
select t1.id
, t1.customer
, t1.quantity
, sum(t2.itemqty)
from table1 t1
join table2 t2
on t1.id = t2.orderid
group by
t1.id
, t1.customer
, t1.quantity
having t1.quantity <> sum(t2.itemqty)
可以使用having子句将条件应用于组:
select t1.id
, t1.customer
, t1.quantity
, sum(t2.itemqty)
from table1 t1
join table2 t2
on t1.id = t2.orderid
group by
t1.id
, t1.customer
, t1.quantity
having t1.quantity <> sum(t2.itemqty)
如何查看更多字段?假设我有t1.Customer您可以根据从t1中选择的任何列进行分组,并在回答中进行更新。我如何查看更多字段?假设我有t1.Customer,您可以根据从t1中选择的任何列进行分组,并在回答中进行更新