I am using a table as below:
| Period | County | Quarter | company | product |
|---|---|---|---|---|
| 01/01/2020 | DE | 01/01/2020 | WKDM2 | Product1 |
| 01/01/2020 | DE | 01/01/2020 | 2GFSDG37 | Product1 |
| 01/02/2020 | DE | 01/01/2020 | ORD56 | Product2 |
| 01/03/2020 | DE | 01/01/2020 | GFDS | Product3 |
| 01/03/2020 | DE | 01/01/2020 | 24GFDSGF2 | Product1 |
| 01/03/2020 | DE | 01/01/2020 | 2GFSDG37 | Product3 |
| 01/03/2020 | DE | 01/01/2020 | 24GSFD1 | Product1 |
| 01/04/2020 | DE | 01/04/2020 | 2GFSDG37 | Product4 |
| 01/04/2020 | DE | 01/04/2020 | 23GSFDG5 | Product6 |
| 01/04/2020 | DE | 01/04/2020 | 24GSFD1 | Product1 |
| 01/05/2020 | DE | 01/04/2020 | 23GSDF6 | Product3 |
| 01/06/2020 | DE | 01/04/2020 | 24GSFD1 | Product8 |
I tried to extract wanted data but this is not working as expected, i have bad count for Company Q & Product Q (code to reproduce):
CREATE OR REPLACE TEMPORARY TABLE "TMP_TEST" (
"Period" TIMESTAMP,
"Country" VARCHAR,
"Quarter" TIMESTAMP,
"Company" VARCHAR,
"Product" VARCHAR
);
INSERT INTO "TMP_TEST"
VALUES
('01/01/2020','DE ','01/01/2020 ','WKDM2 ','Product1'),
('01/01/2020','DE ','01/01/2020 ','2GFSDG37 ','Product1'),
('01/02/2020','DE ','01/01/2020 ','ORD56 ','Product2'),
('01/03/2020','DE ','01/01/2020 ','GFDS ','Product3'),
('01/03/2020','DE ','01/01/2020 ','24GFDSGF2 ','Product1'),
('01/03/2020','DE ','01/01/2020 ','2GFSDG37 ','Product3'),
('01/03/2020','DE ','01/01/2020 ','24GSFD1 ','Product1'),
('01/04/2020','DE ','01/04/2020 ','2GFSDG37 ','Product4'),
('01/04/2020','DE ','01/04/2020 ','23GSFDG5 ','Product6'),
('01/04/2020','DE ','01/04/2020 ','24GSFD1 ','Product1'),
('01/05/2020','DE ','01/04/2020 ','23GSDF6 ','Product3'),
('01/06/2020','DE ','01/04/2020 ','24GSFD1 ','Product8');
Not working query:
SELECT t1."Period",t1."Country",t1."Quarter"
,COUNT(DISTINCT(t1."Company")) AS "Company",COUNT(DISTINCT(t1."Product")) AS "Product",
(SELECT COUNT(DISTINCT("Company")) from "TMP_TEST" t2 where t1."Period" >= t2."Quarter" AND t1."Period" <= t2."Period" AND t1."Country" = t2."Country") AS "Company Q",
(SELECT COUNT(DISTINCT("Product")) from "TMP_TEST" t3 where t1."Period" >= t3."Quarter" AND t1."Period" <= t3."Period" AND t1."Country" = t3."Country") AS "Product Q"
FROM "TMP_TEST" t1
group by 1,2,3
ORDER BY 1
Query working for 01/03/2020 :
SELECT COUNT(DISTINCT("Company")) from "TMP_TEST" WHERE "Period" IN('01/01/2020','01/02/2020','01/03/2020' )
The results I want :
| Period | County | Quarter | Company | Product | Company Q | Product Q |
|---|---|---|---|---|---|---|
| 2020-01-01 | DE | 2020-01-01 | 2 | 1 | 2 | 1 |
| 2020-01-02 | DE | 2020-01-01 | 1 | 1 | 3 | 2 |
| 2020-01-03 | DE | 2020-01-01 | 4 | 2 | 6 | 3 |
| 2020-01-04 | DE | 2020-01-04 | 3 | 3 | 3 | 3 |
| 2020-01-05 | DE | 2020-01-04 | 1 | 1 | 4 | 4 |
| 2020-01-06 | DE | 2020-01-04 | 1 | 1 | 4 | 5 |
For period 2020-02-01 (including 2020-02-01 and 2020-01-01 data), I need to find 3 unique companies (WKDM2,2GFSDG37,ORD56) and 2 unique Products (Product1,Product2)
For period 2020-03-01 (including 2020-03-01,2020-02-01 & 2020-01-01 data), I need to find 6 unique companies (WKDM2,2GFSDG37,ORD56,GFDS,24GFDSGF2,24GSFD1) and 3 unique Products (Product1,Product2,Product3).
Could you tell me were am I wrong please?
Update:
I created this working query which is overkill in terms of performance.
WITH M AS (
SELECT t3."Period",t3."Country",t3."Quarter"
,COUNT(DISTINCT(t3."Company")) AS "Company"
FROM "TMP_TEST" t3
group by 1,2,3
ORDER BY 1
), Q AS (
SELECT COUNT(DISTINCT(t1."Company")) AS "Company Q",t2."Period",t2."Country" from "TMP_TEST" t2,"TMP_TEST" t1 where t1."Period" >= t2."Quarter" AND t1."Period" <= t2."Period" AND t1."Country" = t2."Country"
GROUP BY t2."Period", t2."Country"
ORDER BY t2."Period"
), Y AS (
SELECT COUNT(DISTINCT(t1."Company")) AS "Company Y",t2."Period",t2."Country" from "TMP_TEST" t2,"TMP_TEST" t1 where t1."Period" >= DATE_TRUNC('YEAR',t2."Quarter") AND t1."Period" <= t2."Period" AND t1."Country" = t2."Country"
GROUP BY t2."Period", t2."Country"
ORDER BY t2."Period"
)
SELECT M.*,Q."Company Q",Y."Company Y" from M,Q,Y WHERE M."Period" = Q."Period" AND M."Country" = Q."Country" AND M."Period" = Y."Period" AND M."Country" = Y."Country"