您好我正在尝试生成报告总客户,总订单,收入和平均订单价值,其中订单低于15英镑,交付方法按标准在一个表中,15英镑 - 20英镑订单和标准在其他表中。 贝娄是我的疑问。
[Customer no]
[Order No],
[Amount],
[Shipping Service]
[Shipping Amount]
FROM [Better$Internet Order] io
INNER JOIN [Better$Consignment] IC
ON IC.[Order No]=OH.[Order No]
WHERE [order date] >= '2016-08-01' AND
[order date] <= '2016-08-31' AND [COUNTRY]='UNITED KINGDOM' and oh.
[type] like 'order%' and [Shipping Service]='Standard'
Group by
[Customer no]
[Order No],
[Amount],
[Shipping Service]
[Shipping Amount]
ORDER BY [AMOUNT] ASC
在我的查询中,我给了“客户否”以获得总客户,并且“订购否”获得总订单,因为一个客户可能在一个月内订购几次。 “订单日期”,“国家/地区”和“类型”是订单,因此它只能获得订单而不退款或替换。我想只获得“送货服务”是标准的。提前感谢您的帮助。
**table data:**
Customer no| order No| Amount | Shipping Service
-------------------------------------------------
1 | 254 | 8 | standard
2 | 258 | 12 | Tracked
1 | 260 | 10 | Standard
3 | 285 | 13 | Tracked
4 | 295 | 11 | Standard
Expected results:
Month Aug 2016
Under 15 | Standard Delivery | Total customer - 2
Total orders - 3
total revenue - £18
AOV - £9
Tracked Delivery | Total customer - 2
Total orders - 2
total revenue - 25
AOV - 12.50
答案 0 :(得分:0)
从内部开始,在派生表中,使用case
表达式来确定价格等级。 GROUP BY
计算每个价格等级/运输类型的结果:
select amountclass, "Shipping Service",
count(distinct "Customer no") as "Total customers",
count(*) as "Total orders",
sum("Amount") as "total revenue",
avg("Amount" * 1.0) as "AOV"
from
(
select "Customer no", "order No", "Amount", "Shipping Service"
case when "Amount" < 15 then "Under 15"
else "Over 15"
end as amountclass
from tablename
) dt
group by amountclass, "Shipping Service"
order by amountclass, "Shipping Service"
使用双引号(例如"Shipping Service"
作为分隔标识符是ANSI SQL。(某些dbms产品也有方括号作为替代。)