每次购买前30天按来源统计访问次数

ih99xse1  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(205)

我有一个记录网站活动的表,其中包含以下列和数据

ID  Date         Source  Revenue

1   2013-10-01   A       0
2   2013-10-01   A       0
3   2013-10-01   B       10
1   2013-10-02   A       40
4   2013-10-03   B       0
3   2013-10-03   B       0
4   2013-10-04   A       10

我正在尝试创建一个表,该表接受每个事务(revenue>0),并按来源统计过去30天内各个列中的所有访问量。应该是这样的。

ID  Date         Source  Revenue Count_A  Count_B

3   2013-10-01   B       10      0        1
1   2013-10-02   A       40      2        0
4   2013-10-04   A       10      1        1

我曾尝试对这些列中的每一列使用子查询,但计数相差甚远,我不知道为什么。

Select ID,
       Date,
       Source,
       Revenue,
       (SELECT Count(*) 
               FROM table t2
               WHERE t2.Date between t.Date-30 and t.Date and Source = 'A') AS Count_A
       (SELECT Count(*) 
               FROM table t3
               WHERE t3.Date between t.Date-30 and t.Date and Source = 'B') AS Count_B
  FROM table t
  Where Revenue > 0
  Order By WMEID

我正在使用microsoft sql server。

31moq8wy

31moq8wy1#

使用横向连接:

Select l.*, l2.*
from logs l outer apply
     (select sum(case when l2.source = 'A' then 1 else 0 end) as count_a,
             sum(case when l2.source = 'B' then 1 else 0 end) as count_b
      from logs l2
      where l2.id = l.id and
            l2.date >= dateadd(day, -30, l.date) and
            l2.date <= l.date
     ) l2
where l.Revenue > 0
order By l.WMEID;

我认为你的方法的问题是你没有匹配身份证。

4nkexdtk

4nkexdtk2#

您的计数是关闭的,因为您的子选择与外部查询不相关,所以总数是独立于行中的其他数据来计算的。而且,没有 GROUP BY 在sub selects中,得到一个表的总计数。我不太确定约会的逻辑。
您可以通过向每个子选择添加相关性来纠正所有这些问题( WHERE...t2.ID = t.ID AND t2.Date = t.Date, etc )包括适当的 GROUP BY 每个查询的子句。但这是相当多的打字,难以维护,难以阅读。它还可能生成多个表扫描,因此可能会成为性能问题。
相反,我会选择条件聚合:

Select t.ID,
       t.Date,
       t.Source,
       SUM(t.Revenue) AS Revenue,
       SUM(CASE WHEN t.Source = 'A' THEN 1 ELSE 0 END) AS Count_A,
       SUM(CASE WHEN t.Source = 'B' THEN 1 ELSE 0 END) AS Count_B
  FROM mytable t
  Where Revenue > 0
    AND t.Date >= DATEADD(DAY, -30, CAST(GETDATE() AS date))
    AND t.Date < CAST(GETDATE() AS date)
  GROUP BY 
       t.ID,
       t.Date,
       t.Source
  Order By t.Date

结果(基于问题的结构,而不是数据):

+----+------------+--------+---------+---------+---------+
| ID |    Date    | Source | Revenue | Count_A | Count_B |
+----+------------+--------+---------+---------+---------+
|  3 | 2020-05-01 | B      |      60 |       0 |       2 |
|  1 | 2020-05-02 | A      |      40 |       1 |       0 |
|  4 | 2020-05-04 | A      |      10 |       1 |       0 |
+----+------------+--------+---------+---------+---------+

这里有一个sql小提琴。

相关问题