我正在尝试查询以查看客户在完成订单后必须支付的总价,但我遇到了此错误语句的问题。如何获得正确的查询?
<块引用>错误:HAVING 的参数必须是布尔类型,而不是货币类型
涉及的表格和插入一些虚假的快速数据:
部分客户:
CREATE TABLE customers (
id integer PRIMARY KEY,
first_name varchar(50),
surname varchar(50),
country varchar(200),
email varchar(100) NOT NULL
);
INSERT INTO customers
VALUES (1, 'Mohamed', 'M', 'UK', 'momu@gmail.com');
INSERT INTO customers
VALUES (2, 'Hass', 'H', 'UK', 'hass@gmail.com');
INSERT INTO customers
VALUES (9, 'Zaza', 'Z', 'UK', 'zaza@gmail.com');
部分产品:
CREATE TABLE store_products (
id integer PRIMARY KEY,
name varchar(100),
type varchar(50),
description text,
price money,
quantity integer
);
INSERT INTO store_products
VALUES (1, 'Portable Chain Saw', 'Garden Tools', 'cant be asked for now LOREMIPSON', 21.00, 500);
INSERT INTO store_products
VALUES (3, 'Waterproof USB Plasma Lighter', 'Ignitions',
'For all you cool smokers look even cooler with the Plasma ARC lighter', 41.70, 3000);
INSERT INTO store_products
VALUES (4, 'Digital Multimeter', 'Measurement Tools', 'Work Safely with this Multimeter', 39.24, 333);
INSERT INTO store_products
VALUES (5, 'Cordless Screwdriver', 'Hand Tools', 'Get the job done just quicker', 97.20, 248);
INSERT INTO store_products
VALUES (6, 'Rustic Wooden Wall', 'Storage', 'Hang, Hang and Hang AWAY!!', 19.35, 995);
然后我将这些表组合成一个多对多关系表ordered_items:
CREATE TABLE ordered_items (
customers_id integer REFERENCES customers (id),
store_products_id integer REFERENCES store_products (id),
PRIMARY KEY (customers_id, store_products_id),
quantity integer,
order_date date
);
INSERT INTO ordered_items
VALUES (1, 4, 2, '2020, 09, 05');
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE ordered_items.store_products_id = 4)
WHERE id = 4;
INSERT INTO ordered_items
VALUES (1, 5, 1, '2020, 09, 05');
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE store_products_id = 5 AND customers_id = 1)
WHERE id = 5;
INSERT INTO ordered_items
VALUES (2, 1, 1, '2020, 09, 25');
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE store_products_id = 1 AND customers_id = 2)
WHERE id = 1;
INSERT INTO ordered_items
VALUES (2, 3, 10, '2020, 09, 25');
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE store_products_id = 3 AND customers_id = 2)
WHERE id = 3;
INSERT INTO ordered_items
VALUES (9, 6, 20, '2021, 01, 04');
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE store_products_id = 6 AND customers_id = 9)
WHERE id = 6;
当客户同时在商店购买一件商品时,我减去了他们在商店中的原始数量。比如说,如果他们有 100 个苹果,而顾客买了 5 个,那么商店还剩下 95 个苹果。到目前为止,这一切都很好。示例:
UPDATE store_products
SET quantity = quantity - (SELECT quantity FROM ordered_items WHERE store_products_id = 6 AND customers_id = 9)
WHERE id = 6;
但是,现在当我尝试查询客户必须支付的最终价格时,我遇到了这个错误
<块引用>错误:HAVING 的参数必须是布尔类型,而不是货币类型
这是我目前的查询代码:
-- If I put it as WHERE clause I get the same
--- ERROR: argument of WHERE must be type boolean, not type money
-- this is the WHERE query
SELECT customers.first_name || ' ' || customers.surname AS customer_name,
SUM(ordered_items.quantity) AS number_of_items_bought,
SUM(store.price) AS final_price
FROM customers
JOIN ordered_items
ON ordered_items.customers_id = customers.id
JOIN store_products AS store
ON store.id = ordered_items.store_products_id
WHERE store.price * ordered_items.quantity
GROUP BY 1;
-- AND here is my HAVING Clause same problem
-- ERROR: argument of HAVING must be type boolean, not type money
SELECT customers.first_name || ' ' || customers.surname AS customer_name,
SUM(ordered_items.quantity) AS number_of_items_bought,
SUM(store.price) AS final_price
FROM customers
JOIN ordered_items
ON ordered_items.customers_id = customers.id
JOIN store_products AS store
ON store.id = ordered_items.store_products_id
GROUP BY 1
HAVING SUM(store.price) * ordered_items.quantity;
我该如何解决?
我希望结果是这样的:
customer_name | number_of_items_bought | final_price
---------------+------------------------+-------------
Mohamed M | 3 | ú175.68
Hass H | 11 | ú438.00
Zaza Z | 20 | ú387.00
(3 rows)
而不是这个,如果我不包括任何 HAVING 或 WHERE CLAUSE 我试图限制并拥有价格 * 数量,我就会得到它
-- With this query with which doesn't tell final price as PRICE * QUANTITY
SELECT customers.first_name || ' ' || customers.surname AS customer_name,
SUM(ordered_items.quantity) AS number_of_items_bought,
SUM(store.price) AS final_price
FROM customers
JOIN ordered_items
ON ordered_items.customers_id = customers.id
JOIN store_products AS store
ON store.id = ordered_items.store_products_id
GROUP BY 1;
-- Wrong result
customer_name | number_of_items_bought | final_price
---------------+------------------------+-------------
Mohamed M | 3 | ú136.44
Hass H | 11 | ú62.70
Zaza Z | 20 | ú19.35
(3 rows)
我应该如何去获取这个查询;我要价格 * 数量显示正确:
customer_name | number_of_items_bought | final_price
---------------+------------------------+-------------
Mohamed M | 3 | ú175.68
Hass H | 11 | ú438.00
Zaza Z | 20 | ú387.00
(3 rows)
答案 0 :(得分:1)
将 0
转换为 money
以给出与计算相同类型的零,从而提供合法的比较:
having sum(store.price) *
sum(ordered_items.quantity) > 0::money
另请注意,您必须使用 ordered_items.quantity
的聚合(即总和)值