【发布时间】:2017-03-04 03:06:40
【问题描述】:
我需要创建一个简化的报告,在其中列出我的所有销售来源,并仅针对杂项进行细分。
我之所以说 simplified 是因为我试图将一些东西组合在一起,这意味着所有动物销售都应该被标记为 Boarding Charges。
这是我目前的查询:
SELECT
CASE
WHEN aoi.is_animal = 'N' THEN TO_CHAR(aot.name)
ELSE 'Boarding Charges'
END AS display_name,
aoi.is_animal,
CASE
WHEN aot.name = 'Misc.' THEN 'Y'
ELSE 'N'
END AS show_details,
SUM(aoi.quantity * aoi.unit_price) as total
FROM ANIMAL_ORDER ao
LEFT JOIN ANIMAL_ORDER_ITEM aoi ON aoi.order_id = ao.id
LEFT JOIN ANIMAL_ORDER_TYPE aot ON aot.id = aoi.order_type_id
WHERE ao.order_stage != 'CANCELLED'
GROUP BY
aot.name,
CASE
WHEN aoi.is_animal = 'N' THEN 0
ELSE 1
END, aoi.is_animal
我现在正在尝试让这件事变得简单,所以我不担心 Misc. 的东西 - 我只是暂时添加了一个列让它说 Y 或 N。
上面的查询结果如下:
# Resulting table
DISPLAY_NAME IS_ANIMAL SHOW_DETAILS TOTALS
-------------------------------------------------------------
Boarding Charges Y N 8039.53
Truck Delivery Fee N N 1005.21
Misc. N Y 237.16
Cancellation Fee N N 45.00
Late Fee N N 410.25
Courier Fee N N 1338.40
Boarding Charges Y N 311.27
Boarding Charges Y N 7341.19
如您所见,登机费没有组合在一起,我明白原因是什么 - 我在GROUP BY 子句中有aot.name。它存在的唯一原因是因为当我尝试删除它时,我在TO_CHAR(aot.name) 上收到一个错误,说它不是GROUP BY 表达式。
我只想将所有寄宿费归为一组并汇总它们的总数。
附加信息
我正在尝试使用this question 中提到的方法。
【问题讨论】: