从SQL Join查询中检索计数

时间:2013-12-19 16:06:30

标签: sql sql-server

SELECT table.productid, product.weight
FROM table1
INNER JOIN product
ON table1.productid=product.productid
where table1.box = '55555';

基本上,它是一个内部联接,用于查看框中的产品列表,然后查看这些产品的权重。

所以我的结果,产品和每个产品的重量都有2列。

是否有一种简单的方法可以获得此查询中列出的权重的SUM?

由于

4 个答案:

答案 0 :(得分:1)

SELECT table.productid, SUM(product.weight) weight
FROM table1
      INNER JOIN product
      ON table1.productid=product.productid
where table1.box = '55555'
Group By table.productid

答案 1 :(得分:0)

这将为您提供每个不同productid的总重量。

SELECT table1.productid, SUM(product.weight) AS [Weight]
FROM table1 INNER JOIN product ON table1.productid = product.productid
WHERE table1.box = '55555'
GROUP BY table1.productid

答案 2 :(得分:0)

这里不清楚这个问题,你只想要总重量还是分组?

SELECT Sum(product.weight)
FROM table1
INNER JOIN product
ON table1.productid=product.productid
where table1.box = '55555';

将给出总数。

答案 3 :(得分:0)

我读了它,因为您想要该框的权重总和以及每个产品的行数。我可能错了,但如果没有,就这么说了。

SELECT table1.productid, product.weight, SUM(weight) Over(PARTITION BY table1.box) AS SumWeights
FROM table1
INNER JOIN product
ON table1.productid=product.productid
WHERE table1.box = '55555';