我需要通过删除直接或直接AND在线购买的产品来显示仅在线购买的产品(order_mode ='online')。
我尝试使用CASE,WHERE子句,但是没有用
select p.product_name,
sum(o.quantity) as total_quantity,
case
when extract(month from p.warranty_period) = 0
and extract(year from p.warranty_period) = 0
then 'No Warranty'
when extract(month from p.warranty_period) = 0
then extract(year from p.warranty_period) || ' years'
when extract(year from p.warranty_period) = 0
then extract(month from p.warranty_period) || ' months'
else extract(year from p.warranty_period) || ' years and ' ||
extract(month from p.warranty_period) || ' months'
end WARRANTY, oe.order_mode from PRODUCT_INFORMATION p
join order_items o on p.product_id = o.product_id
join orders oe on o.order_id = oe.order_id
group by p.product_name, p.warranty_period, oe.order_mode
having sum(o.QUANTITY) > 200
order by p.product_name;
我希望看到仅在线购买的Order_mode列。
答案 0 :(得分:1)
没有满足您要求的数量> 200的产品。我将限制设置为40。
select p.product_name,
sum(o.quantity) as total_quantity,
case
when extract(month from p.warranty_period) = 0
and extract(year from p.warranty_period) = 0
then 'No Warranty'
when extract(month from p.warranty_period) = 0
then extract(year from p.warranty_period) || ' years'
when extract(year from p.warranty_period) = 0
then extract(month from p.warranty_period) || ' months'
else extract(year from p.warranty_period) || ' years and ' ||
extract(month from p.warranty_period) || ' months'
end WARRANTY, 'online' order_mode from PRODUCT_INFORMATION p
join (
select o.product_id, sum(o.quantity) quantity
from order_items o
join orders oe on o.order_id = oe.order_id
group by o.product_id
having sum(case oe.order_mode when 'direct' then 1 else 0 end) = 0
and sum(o.quantity) > 40
) o on p.product_id = o.product_id
group by p.product_name, p.warranty_period
order by p.product_name;
PRODUCT_NAME TOTAL_QUANTITY WARRANTY ORDER_MODE
Chemicals - SW 68 5 years online
DIMM - 32MB 47 6 months online
PS 220V /HS/FR 45 9 months online
Pens - 10/FP 42 No Warranty online
Plastic Stock - B/HD 64 No Warranty online
TD 7GB/8 59 1 years and 6 months online