-1

表: 注文商品

orderid   quantity       weight
-----------------------------------
4           5             0.1
4           2             0.5
5           3             2.5
5           7             0.9

1) 次の SQL コマンドでは、総重量は正しく計算されますが、製品の数量は無視されます。SQL コマンドを拡張して製品の数量を照会するにはどうすればよいですか?

select orderid, sum (weight) as totalweight from orderproducts group by orderid

今、私は ORDERID 4 の値 (totalweight) => 0.6 を持っています

正しくなければならない => 1.5

2) また、別のテーブルから「WHERE」(where other_table.status = "finish") への SQL コマンドを拡張したい。また、機能しません。:-(

select orderid, sum (weight) as totalweight from orderproducts where other_table.status = "finish" group by orderid
4

3 に答える 3

2

クエリ:

SELECT orderid,
       SUM(weight*quantity) AS totalweight
FROM orderproducts AS a
JOIN othertable AS b ON (b.orderid=b=id)
WHERE b.status = "finish"
GROUP BY orderid
于 2013-09-04T05:34:49.443 に答える
0

1)

select orderid,sum (amount) as totalamount, sum (weight) as totalweight from orderproducts group by orderid

2) 結合を使用する必要があります

  select orderid, sum (weight*quantity) as totalweight from orderproducts as a join othertable AS b on (b.orderid=b=id) where b.status = "finish" group by orderid
于 2013-09-04T04:54:18.320 に答える