読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第1節 個数と単価

1.2 1行ごとの小計

明細の1行の小計は、個数×単価です。

SELECT reservation_id, product_id, quantity, unit_price,
       quantity * unit_price AS 小計
FROM reservation_items
ORDER BY reservation_id, product_id;
 reservation_id | product_id | quantity | unit_price | 小計
----------------+------------+----------+------------+------
 R-0001         | p001       |        1 |        380 |  380
 R-0001         | p002       |        2 |        220 |  440
 R-0002         | p004       |        3 |        240 |  720
 R-0003         | p003       |        4 |        180 |  720
 R-0003         | p005       |        2 |        200 |  400
(5 rows)

quantity * unit_price は、行ごとに計算されます。単位は「個×円」で、結果は円です。

行計算小計
R-0001・p0011個×380円380円
R-0001・p0022個×220円440円
解答

quantity * unit_price は 2×220=440 円で、変わりません。quantity * p.price は 2×240=480 円になります。予約したときの金額は440円です。

3 / 20