添加每个客户的旧购买列以进行比较

时间:2017-12-30 17:03:48

标签: sql database sqlite subquery

阅读关于Sqlite的书,并决定测试某些查询以获得乐趣。

这个让我思考。

如何获得价格低于之前商品的新商品的持续买家数量?

  • 没有联接需要在同一张桌子上全部。

  • 相关的表格名称:day:已购买日期,customerprice

到目前为止,我找到了持续的客户,但无法检查他们之前的购买情况。

SELECT * FROM (SELECT * FROM ORDERS GROUP BY CUSTOMER HAVING COUNT(*)>1);

认为通过date_bought排序,将每个客户的购买添加到彼此相邻的列中可能是进行实际“每持续客户”检查的唯一方法。

对于再现性,您可以使用:

CREATE TABLE orders (
    day DATE,
    price FLOAT
    item char
    customer char
);

INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-2 day'), 0.5,'food','Jenny');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-23 day'), 1,'food','Jenny');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-1 day'), 11,'food','Betty');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-22 day'), 7,'food','Betty');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-3 day'), 8,'food','Katy');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-4 day'), 10,'food','Mary');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-23 day'), 1,'food','Mary');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-1 day'), 2,'food','Anna');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-2 day'), 12,'food','Anna');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-3 day'), 8,'food','Anna');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-4 day'), 10,'food','Lisa');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-5 day'), 5,'food','Lisa');
INSERT INTO orders(day, price,item,customer) VALUES(date('now', 'localtime', '-8 day'), 12,'food','Jenny');

3 个答案:

答案 0 :(得分:0)

如果您只是想要计算并且不关心特定客户是谁,那么可以通过一种方式获得多次订购同一商品的客户数量,以及以后更低的价格,是使用带有exists谓词的相关子查询:

select count(distinct customer) 
from orders o1
where exists (
    select customer 
    from orders o2 
    where o1.customer = o2.customer and 
          o1.item = o2.item and 
          o1.day > o2.day and 
          o1.price < o2.price
    );

这将返回不同客户的数量,这些客户的某一行具有相同的项目且较早的价格较高。

如果我理解你的问题,我认为这就是你想要的。

答案 1 :(得分:0)

我的自我加入方法就是这样。

select your field
from orders o1 join orders o2 on o1.customer = o2.customer

where o2.price < o1.price
and o1.day = (select max(day)
from orders
where day < o2.day
and customer = o2.customer)

假设their previous item是指最近订单上的商品。如果their previous item引用任何先前的订单,您可以将子查询替换为:

and o1.day < o2.day

答案 2 :(得分:0)

您可以使用CTE。这将为您提供实际细节。如果您只想计算,只需将最终SELECT *替换为SELECT COUNT(*)

with xxx(day,price,item,customer,previous_price,previous_date) as (
  select *,null,null from orders group by customer having min(day)
  union all
  select o.day,o.price,o.item,o.customer,x.price,x.day
    from orders o join xxx x using(customer)
    where o.price < x.price
      and o.day > x.day
  )
  select * from xxx
    where previous_price is not null;

如果与上一次购买相比较,那么这个替代方案:

with xxx(day,price,item,customer,previous_price,previous_date) as (
  select *,null,null from orders
  union all
  select o.day,o.price,o.item,o.customer,x.price,x.day
    from orders o join xxx x using(customer)
    where o.price < x.price
      and o.day > x.day
  )
  select * from xxx group by customer having max(previous_date);