コード例 #1
0
        public BucketedDatetimes(
            string tableName,
            string columnName,
            DValueType columnType = DValueType.Datetime)
        {
            QueryComponents = columnType switch
            {
                DValueType.Datetime => DateComponents.Concat(TimeComponents).ToArray(),
                DValueType.Timestamp => TimeComponents,
                DValueType.Date => DateComponents,
                _ => throw new System.ArgumentException($"Expected Datetime, Date or Time, got {columnType}."),
            };
            var groupsFragment = string.Join(",\n", QueryComponents.Select(s => $"date_trunc('{s}', {columnName})"));
            var groupingSets   = string.Join(", ", Enumerable.Range(2, QueryComponents.Length));

            QueryStatement = $@"
                select
                    grouping_id(
                        {groupsFragment}
                    ),
                    {groupsFragment},
                    count(*),
                    count_noise(*)
                from {tableName}
                group by grouping sets ({groupingSets})";
        }