Sql 如何获取基于订单总值和邮政组的报告

Sql 如何获取基于订单总值和邮政组的报告,sql,sql-server-2012,Sql,Sql Server 2012,您好,我正在尝试生成一份报告,其中一个表中的订单金额低于15英镑,交货方式按标准,另一个表中的订单金额低于15英镑至20英镑,订单金额低于标准。 下面是我的问题 [Customer no] [Order No], [Amount], [Shipping Service] [Shipping Amount] FROM [Better$Internet Order] io INNER JOIN [Better$Consignment] IC

您好,我正在尝试生成一份报告,其中一个表中的订单金额低于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

从内部开始,在派生表中,使用case表达式来确定price类。根据该结果分组,以计算每个价格类别/装运类型:

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"

使用双引号(例如,装运服务)作为分隔标识符是ANSI SQL。一些dbms产品也有方括号作为替代。

似乎您已经丢失了查询的第一部分。复制和粘贴问题?添加一些示例表数据和预期结果-所有内容都是格式良好的文本。您好,我只是在学习sql,我没有第一部分。@jarlh已用表数据更新了问题,预期结果似乎需要一个分组依据,以及价格类的大小写表达式。
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"