DROP TABLE IF EXISTS public.historical_sales; CREATE TABLE public.historical_sales ( order_id bigint, order_date date, order_year int, region text, product_category text, channel text, quantity int, unit_price double precision, amount double precision ); INSERT INTO public.historical_sales SELECT s, DATE '2019-01-01' + ((s*7)%2190), EXTRACT(YEAR FROM (DATE '2019-01-01' + ((s*7)%2190)))::int, (ARRAY['EMEA','AMER','APAC','LATAM'])[(s%4)+1], (ARRAY['Servers','Storage','Networking','Laptops','Services'])[(s%5)+1], (ARRAY['Direct','Partner','Online'])[(s%3)+1], (1+(s%50))::int, round((100+(s%9000)*0.5)::numeric,2)::double precision, round(((1+(s%50))*(100+(s%9000)*0.5))::numeric,2)::double precision FROM generate_series(1,25000) s; SELECT order_year, count(*), round(sum(amount)::numeric,0) FROM public.historical_sales GROUP BY order_year ORDER BY 1;