CREATE TABLE CDT (
UID INTEGER NOT NULL
, COUNTRY varchar(100) NOT NULL
, MSDN varchar(100) NOT NULL
, MSSI varchar(100) NOT NULL
, CALL_DURATION INTEGER NOT NULL
, SDATA INTEGER NOT NULL
, COST INTEGER NOT NULL
, DATE_KEY INTEGER NOT NULL
);
查询以获取数据:
SELECT
UID
, COUNTRY
, MSDN
, MSSI
, CALL_DURATION
, SDATA
, COST
, c.DATE_KEY
, d.DATE_VAL
FROM CDT as C
INNER JOIN DATE_DIMENSION as d on c.DATE_KEY = d.DATE_KEY;
OP: 我需要以下计数: 每_DAY_COUNT | WEEK_WIE_COUNT | MONTHLY_COUNT | Year_COUNT个
答案 0 :(得分:0)
Sholk-下次将您的数据显示为文本而不是图片,以便我们将其复制粘贴到示例中。这样,我花了15分钟才能将示例数据汇总在一起...
你是这个意思吗?
-我设法简化了很多,想到了...
-- your input data
WITH cdt(uid,country,msdn,mssi,call_dur,sdata,cost,date_key,date_val) AS (
SELECT 50,'IND',9861388009,9761588009,466,477,48900,3290,DATE '2019-01-04'
UNION ALL SELECT 51,'IND',9861388009,9761588009,466,477,48900,3290,DATE '2019-01-04'
UNION ALL SELECT 52,'IND',9861388009,9761588009,466,477,48900,3290,DATE '2019-01-04'
UNION ALL SELECT 53,'IND',9861388009,9761588009,466,477,48900,3290,DATE '2019-01-04'
UNION ALL SELECT 45,'IND',9861388009,9761588009,466,477,48900,3291,DATE '2019-01-05'
UNION ALL SELECT 46,'IND',9861388009,9761588009,466,477,48900,3291,DATE '2019-01-05'
UNION ALL SELECT 47,'IND',9861388009,9761588009,466,477,48900,3291,DATE '2019-01-05'
UNION ALL SELECT 48,'IND',9861388009,9761588009,466,477,48900,3291,DATE '2019-01-05'
UNION ALL SELECT 41,'IND',9861388009,9761588009,466,477,48900,3292,DATE '2019-01-06'
UNION ALL SELECT 42,'IND',9861388009,9761588009,466,477,48900,3292,DATE '2019-01-06'
UNION ALL SELECT 43,'IND',9861388009,9761588009,466,477,48900,3292,DATE '2019-01-06'
UNION ALL SELECT 44,'IND',9861388009,9761588009,466,477,48900,3292,DATE '2019-01-06'
UNION ALL SELECT 37,'IND',9861388009,9761588009,466,477,48900,3293,DATE '2019-01-07'
UNION ALL SELECT 38,'IND',9861388009,9761588009,466,477,48900,3293,DATE '2019-01-07'
UNION ALL SELECT 39,'IND',9861388009,9761588009,466,477,48900,3293,DATE '2019-01-07'
UNION ALL SELECT 40,'IND',9861388009,9761588009,466,477,48900,3293,DATE '2019-01-07'
UNION ALL SELECT 33,'IND',9861388009,9761588009,466,477,48900,3294,DATE '2019-01-08'
UNION ALL SELECT 34,'IND',9861388009,9761588009,466,477,48900,3294,DATE '2019-01-08'
UNION ALL SELECT 35,'IND',9861388009,9761588009,466,477,48900,3294,DATE '2019-01-08'
UNION ALL SELECT 36,'IND',9861388009,9761588009,466,477,48900,3294,DATE '2019-01-08'
UNION ALL SELECT 29,'IND',9861388009,9761588009,466,477,48900,3295,DATE '2019-01-09'
)
-- real query starts here ...
SELECT
date_val
, COUNT(*) OVER(PARTITION BY DAY (date_val)) AS per_day
, COUNT(*) OVER(PARTITION BY WEEK (date_val)) AS per_week
, COUNT(*) OVER(PARTITION BY MONTH(date_val)) AS per_month
, COUNT(*) OVER(PARTITION BY YEAR (date_val)) AS per_year
FROM cdt
;
date_val | per_day | per_week | per_month | per_year
------------+---------+----------+-----------+----------
2019-01-05 | 4 | 8 | 21 | 21
2019-01-05 | 4 | 8 | 21 | 21
2019-01-05 | 4 | 8 | 21 | 21
2019-01-05 | 4 | 8 | 21 | 21
2019-01-04 | 4 | 8 | 21 | 21
2019-01-04 | 4 | 8 | 21 | 21
2019-01-04 | 4 | 8 | 21 | 21
2019-01-04 | 4 | 8 | 21 | 21
2019-01-09 | 1 | 13 | 21 | 21
2019-01-08 | 4 | 13 | 21 | 21
2019-01-08 | 4 | 13 | 21 | 21
2019-01-08 | 4 | 13 | 21 | 21
2019-01-08 | 4 | 13 | 21 | 21
2019-01-07 | 4 | 13 | 21 | 21
2019-01-07 | 4 | 13 | 21 | 21
2019-01-07 | 4 | 13 | 21 | 21
2019-01-07 | 4 | 13 | 21 | 21
2019-01-06 | 4 | 13 | 21 | 21
2019-01-06 | 4 | 13 | 21 | 21
2019-01-06 | 4 | 13 | 21 | 21
2019-01-06 | 4 | 13 | 21 | 21
(21 rows)