【问题标题】:How to sum over selected dates from table如何汇总表中选定的日期
【发布时间】:2014-05-05 08:27:54
【问题描述】:

考虑以下查询,该查询生成 customerid 和他们购买特定产品的日期,显然每个客户都有不同的购买日期。我想做的是在客户购买该产品的那几天获得总购买量。

我有 ff 查询。

Select customerid, eventdate
into #days
from table1
where product='chocolate'

现在我想总结客户购买“巧克力”的那几天的所有购买。 所以我有

select customerid, sum(purchases) purchases
into #pur
from table1 a
where eventdate in (select eventdate from #days where customerid=a.customerid)
group by customerid

但是上面的运行需要很长时间,所以我取消了它。 请协助更好的查询。

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    不太确定这是否可行,但请尝试一下。如果没有,请提供示例数据和预期输出,以便我们以更好的方式进行尝试

    select customerid, sum(purchases) purchases
    into #pur
    from table1 a
    inner join #days d 
    ON d.customerid = a.customerid
    AND d.eventdate = a.eventdate
    group by customerid
    

    或者试试这个查询

    select customerid, sum(purchases) purchases
    into #pur
    from table1 a
    inner join #days d 
    ON d.customerid = a.customerid
    WHERE d.eventdate = a.eventdate
    group by customerid
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      客户所有购买的总和,仅计算他购买巧克力的日期。

      SELECT customerid, sum(purchases) purchases
      FROM table1 a
      WHERE eventdate IN
        (SELECT eventdate
         FROM table1
         WHERE product = 'chocolate'
         AND customerid = a.customerid)
      GROUP BY customerid;
      

      【讨论】:

      • 我想我得让你知道表结构是怎样的。 table1 有如下结构,customerid, product, purchase... 1 chocoloate 10.00
      【解决方案3】:

      这给出了每个客户购买巧克力的每一天的购买总和。

      select customerid, eventdate ,sum(purchases) purchases
      into #pur
      from table1
      where product='chocolate'
      group by customerid,eventdate 
      

      如果您想在送来巧克力时购买全部商品,请执行此操作

      select customerid,sum(purchases) purchases
      into #pur
      from table1
      where product='chocolate'
      group by customerid
      

      根据您的说明

      select customerid, sum(purchases) purchases
      into #pur
      from table1 a
      where eventdate in (select eventdate from table1 where product='chocolate')
      group by customerid
      

      我建议您对 eventdate 列应用索引以提高查询性能。

      【讨论】:

      • 我想我必须让你知道表结构是怎样的。 table1 具有以下结构,customerid, eventdate, product, purchase ... 1 20140101 chocoloate 10.00 1 20140203 candy 15.00 product='chocolate' 的 where 子句将仅计算巧克力的购买量,我想要的是获取日期哪个顾客只买了巧克力,然后将包括巧克力在内的所有购买相加
      • 所以首先你想找到购买巧克力的所有日期,然后将它们分组到 customerid 中并计算总和?
      • 是的,计算所有产品(包括巧克力)的购买总和,在仅购买巧克力的那一天。
      • 检查我发布的答案中的最后一个查询
      • 谢谢,但该查询意味着每个客户将使用相同的事件日期,实际上,每个客户在不同的日期购买巧克力,所以对于那些不同的日期,我只想总结(购买)每位顾客购买巧克力的日期
      【解决方案4】:

      经过仔细考虑,以下内容对我有用,而且速度更快。

      --删除表#days 选择客户ID,事件日期 进入#days 从表 1 与(无锁,索引(ix_eventdate)) WHERE 事件在 20140401 和 20140430 之间 和 product='巧克力'

      --删除表#pur 选择 customerid、eventdate、购买
      进入#pur 从表 1 与(无锁,索引(ix_eventdate)) 其中 eventdate 在 20140401 和 20140430 之间

      --删除表 #first 选择 a.*, b.purchases 从 #days a 进入 #first 左加入#pur b 在 a.customerid=b.customerid 和 a.EventDate =b.EventDate

      --从#first中选择*

      --删除表#purdays 从#first 中选择customerid、sum(purchases) 收入到#purdays 按客户ID分组 按客户编号订购

      从#purdays中选择*

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 2022-07-13
        • 1970-01-01
        相关资源
        最近更新 更多