我对SQL很陌生并且一直试图解决这个问题..但仍然没有运气。如果有人在这里可以帮助我,我将不胜感激。
我有一个包含列的数据库:
ClientID
,
VisitID
,
Date
,
PurchaseID
(数组)
等
我想要达到的目标是:
ClientID
,
Last Visit Date
,
First Visit Date
,
Last Purchase Date
,
Visits Count
,
Purchases Count
,
Visits After Last Purchase Count
最后购买次数后的访问次数 - 我坚持了。
SELECT
ClientID,
FirstVisit,
LastVisit,
LastPurchaseDate,
Visits,
Purchases,
VisitsAfterPurchase
FROM
(
SELECT
h.ClientID,
max(h.Date) AS LastVisit,
min(h.Date) AS FirstVisit,
count(VisitID) AS Visits
FROM s7_visits AS h
WHERE Date > '2017-12-01'
GROUP BY h.ClientID
LIMIT 100
)
ANY LEFT JOIN
(
SELECT
d.ClientID,
max(d.Date) AS LastPurchaseDate,
sum(length(d.PurchaseID)) AS Purchases,
sum(
(
SELECT count(x.VisitID)
FROM s7_visits AS x
WHERE x.ClientID = d.ClientID
HAVING x.Date >= max(d.Date)
)) AS VisitsAfterPurchase
FROM s7_visits AS d
WHERE (length(PurchaseID) > 0) AND (Date > '2017-12-01')
GROUP BY d.ClientID
) USING (ClientID)
我使用的数据库是Yandex Clickhouse。
USING
事情绝对正常(使用它代替ON)
此查询给出了错误:
DB ::异常:列日期不在聚合函数下,不在GROUP BY ..
示例数据:
+----------+---------+------------+------------+
| CliendID | VisitID | Date | PurchaseID |
+----------+---------+------------+------------+
| 123 | 136 | 01.12.2017 | |
| 123 | 522 | 05.12.2017 | |
| 123 | 883 | 08.12.2017 | |
| 123 | 293 | 09.12.2017 | ['345'] |
| 123 | 278 | 12.12.2017 | |
| 123 | 508 | 12.12.2017 | |
| 123 | 562 | 15.12.2017 | |
| 123 | 523 | 21.12.2017 | |
| 456 | 736 | 29.11.2017 | |
| 456 | 417 | 03.12.2017 | |
| 456 | 950 | 04.12.2017 | |
| 456 | 532 | 05.12.2017 | ['346'] |
| 456 | 880 | 09.12.2017 | |
| 456 | 296 | 12.12.2017 | |
| 456 | 614 | 15.12.2017 | |
+----------+---------+------------+------------+
结果将是:
+----------+-----------------+------------------+--------------------+--------------+-----------------+----------------------------------+
| ClientID | Last Visit Date | First Visit Date | Last Purchase Date | Visits Count | Purchases Count | Visits After Last Purchase Count |
+----------+-----------------+------------------+--------------------+--------------+-----------------+----------------------------------+
| 123 | 21.12.2017 | 01.12.2017 | 09.12.2017 | 8 | 1 | 4 |
| 456 | 15.12.2017 | 29.11.2017 | 05.12.2017 | 7 | 1 | 3 |
+----------+-----------------+------------------+--------------------+--------------+-----------------+----------------------------------+
答案 0 :(得分:0)
问题归因于HAVING x.Date >= MAX(d.Date)
。引用x.Date
列是没有意义的,因为您首先会聚合x.ClientID = d.ClientID
的所有行,而x.Date
只会来自该组中随机选择的行。
如果要选择要计算的行,则需要在WHERE
子句中执行此操作。所以子查询应该是:
(
SELECT
d.ClientID,
max(d.Date) AS LastPurchaseDate,
sum(length(d.PurchaseID)) AS Purchases,
sum(
(
SELECT count(x.VisitID)
FROM s7_visits AS x
WHERE x.ClientID = d.ClientID AND x.Date >= max(d.Date)
)) AS VisitsAfterPurchase
FROM s7_visits AS d
WHERE (length(PurchaseID) > 0) AND (Date > '2017-12-01')
GROUP BY d.ClientID
) USING (ClientID)