编写一个clickhouse实现一段日期内发生交易的工作日最大连续天数的sql
时间: 2023-03-21 14:02:02 浏览: 115
以下是一个使用ClickHouse编写的SQL语句,用于计算在指定日期范围内发生交易的最大连续工作日天数:
```sql
SELECT max(consecutive_days)
FROM (
SELECT count(*) AS consecutive_days
FROM (
SELECT date,
dateDiff('day', toDate('1970-01-01'), toDate(transaction_date)) AS transaction_day
FROM transactions
WHERE date BETWEEN 'start_date' AND 'end_date' AND is_workday(date)
ORDER BY date ASC
) AS tmp
GROUP BY transaction_day - toUInt32(number)
HAVING consecutive_days = max(consecutive_days)
)
```
请注意,此查询中的“transactions”应替换为实际包含交易数据的表名。此外,“start_date”和“end_date”应替换为实际日期范围的起始和结束日期。函数"is_workday"是自定义的,用于判断某天是否为工作日。
阅读全文