'I need to find an efficient way to count a field in Teradata segmented by month and year

I need distinct count of a field in Teradata for every month from 2018 to 2021. The date field and the field that I am counting are in string format.

I wonder if there’s a more efficient way of doing it instead of repeating below code for different months.

SELECT COUNT (DISTINCT category_A) FROM my_table WHERE tran_date BETWEEN '2018-01-01' AND '2018-01-31'

This is the table I need to complete with the counts.

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source