每个订单之前的特定类型的订单数量

时间:2017-07-21 07:14:57

标签: sql hana

客户有订单,每个订单都是类型1或类型2.我想计算每种类型在每个订单之前的次数。

例如,对于一位客户:

customer | orders| Type
---------+-------------
customerA| Order1| 1
customerA| Order2| 1
CustomerA| Order3| 2
customerA| Order1| 1
customerA| Order2| 2
customerA| Order3| 2
CustomerA| Order1| 1
customerA| Order2| 2

所需结果:

    customer | orders| Type| Nr_typ1| Nr_typ2
    ---------+-------+-----+--------+-------
    customerA| Order1| 1   |   0    |   0
    customerA| Order2| 1   |   1    |   0
    CustomerA| Order3| 2   |   2    |   0
    customerA| Order1| 1   |   2    |   1
    customerA| Order2| 2   |   3    |   1
    customerA| Order3| 2   |   3    |   2
    CustomerA| Order1| 1   |   3    |   3
    customerA| Order2| 2   |   4    |   3
我正在尝试的是:

select t.customer, t.orders, t.type, sum(t.type1), sum(t.type2) from
(select customer, orders, type,
case type = 1 then 1 else 0 end as type1, 
case type = 2 then 1 else 0 end as type2
from table customer_orders) t group by customer, orders, type

任何建议都会受到高度赞赏!感谢

2 个答案:

答案 0 :(得分:1)

我相信HANA支持windowed aggregates。在SQL Server上测试了以下脚本,但我相信相同的查询应该适用于HANA:

declare @t table(customer varchar(20) not null, orders varchar(10) not null, Type int not null)
insert into @t(customer,orders,Type) values
('customerA','Order1',1),
('customerA','Order2',1),
('CustomerA','Order3',2),
('customerA','Order1',1),
('customerA','Order2',2),
('customerA','Order3',2),
('CustomerA','Order1',1),
('customerA','Order2',2)

select
    *,
    SUM(CASE WHEN Type=1 THEN 1 ELSE 0 END) OVER
    (PARTITION BY customer
    ORDER BY orders
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) -
    CASE WHEN Type=1 THEN 1 ELSE 0 END as TotalType1,
    SUM(CASE WHEN Type=2 THEN 1 ELSE 0 END) OVER
    (PARTITION BY customer
    ORDER BY orders
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) -
    CASE WHEN Type=2 THEN 1 ELSE 0 END as TotalType1
from @t

结果:

customer             orders     Type        TotalType1  TotalType1
-------------------- ---------- ----------- ----------- -----------
customerA            Order1     1           0           0
customerA            Order1     1           1           0
CustomerA            Order1     1           2           0
customerA            Order2     2           3           0
customerA            Order2     2           3           1
customerA            Order2     1           3           2
CustomerA            Order3     2           4           2
customerA            Order3     2           4           3

希望窗口的定义相当容易分开 - 没有人指责SQL是简洁的。

(我没有得到问题中要求的确切结果,但这是因为样本数据集中没有足够的列放在ORDER BY子句中,因此“before”定义得很好。我目前正在使用orders,但这不是唯一的)

答案 1 :(得分:0)

您可以在SQL中使用WHILE循环,如下所示。 (#order是我的订单表,您将其更改为您的实际表格)

创建表来存储结果:

CREATE TABLE #res 
    (customer varchar(100),
    orders varchar(100),
    [type] int,
    Nr_typ1 int,
    Nr_typ2 int)

WHILE循环声明一些变量和游标:

DECLARE @Customer varchar(100), @Order varchar(100), @Type int, @nr_type1 int = 0, @nr_type2 int = 0

DECLARE order_cursor CURSOR FOR
SELECT customer, orders, [type]
FROM #orders

循环你的桌子:

OPEN order_cursor
FETCH NEXT FROM order_cursor INTO  @Customer, @Order, @Type

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO #res
    VALUES(@Customer, @Order, @Type, @nr_type1, @nr_type2)    

    IF @Type = 1
        SET @nr_type1 = @nr_type1 + 1
    ELSE 
        SET @nr_type2 = @nr_type2 + 1

    FETCH NEXT FROM order_cursor INTO  @Customer, @Order, @Type
END

打印结果:

SELECT * FROM #res

使用后关闭光标:

CLOSE order_cursor
DEALLOCATE order_cursor
GO

如果您不再需要,请删除结果表:

DROP TABLE #res