dongshushen4392 2014-12-19 07:21
浏览 17
已采纳

在子查询和主查询之前的日期

I'm have 2 tables: table for customers and table for activity. Im trying to find how many customers have 2 activity specific. For example, how many customers was bought and arrived to the branch. (i have line in the activity table for every act that the customer did.) this is the query:

SELECT COUNT(*) as total FROM activity
WHERE activity = 'arrived' AND
customerid IN
(SELECT DISTINCT(customerid) FROM activity
WHERE activity = 'bought')

but i need to know how much bought AFTER they arrvied, because there are customers that bought from the website, than arrived to the branch after 1 year for example. so i want only the customer that arrived and than bought. i tried this:

SELECT COUNT(*) as total, daten as odaten FROM activity
WHERE activity = 'arrived' AND
customerid IN
(SELECT DISTINCT(customerid), daten as tdaten FROM activity
WHERE activity = 'bought') HAVING odaten < tdaten

but its not working.. any idea?

  • 写回答

2条回答 默认 最新

  • dream_high1026 2014-12-19 08:12
    关注

    You are in a wrong direction. You should not use sub query. Instead, you should try self join. Here is my solution:

    --activity customerid daten
    select count(customerid) as finalcount from(
    select distinct t1.customerid
    from tablename t1
    inner join tablename t2 on t1.customerid=t2.customerid
    where t1.activity='arrivived' and t2.activity='bought' and t1.daten<t2.daten
    )t
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误