根据季度划分 ID,并通过确定季度来计数 1 或 0
Divide Ids based on quarter and the count either 1 or 0 by determining the quarter
我们有两列 Id
和 month Id
。
我正在寻找的输出是根据季度粒度将年与月 Id 分开。 activity 列应该来自季度。如果 id 处于活动状态 activity 应该是 1 否则 0 。如果 id 出现在第一季度的任何一个(例如:只有 1),则 activity 仍然是 1 。
像这样:
id month_dt
-----------------------------------
1000000000 2012-03-01 00:00:00.0
1000000000 2015-09-01 00:00:00.0
1000000000 2016-10-01 00:00:00.0
1000000000 2015-11-01 00:00:00.0
1000000000 2014-01-01 00:00:00.0
1000000000 2013-04-01 00:00:00.0
1000000000 2014-12-01 00:00:00.0
1000000000 2015-02-01 00:00:00.0
1000000000 2014-06-01 00:00:00.0
1000000000 2013-01-01 00:00:00.0
1000000000 2014-05-01 00:00:00.0
1000000000 2016-05-01 00:00:00.0
1000000000 2013-07-01 00:00:00.0
期望值:
ID YEAR QTR ACTIVITY (1 or 0)
--------------------------------------------------
1000000000 2012 1 1
1000000000 2012 2 0
1000000000 2012 3 0
1000000000 2012 4 0
1000000000 2013 1 1
1000000000 2013 2 1
1000000000 2013 3 1
1000000000 2013 4 0
下面是我试过的方法,但没有 return 预期的结果。请帮我实现这个
SELECT
a.id, a.year,
SUM(CASE WHEN quarter BETWEEN 1 AND 3 THEN 1 ELSE 0 END) AS Q1,
SUM(CASE WHEN quarter BETWEEN 4 AND 6 THEN 1 ELSE 0 END) AS Q2,
SUM(CASE WHEN quarter BETWEEN 7 AND 9 THEN 1 ELSE 0 END) AS Q3,
SUM(CASE WHEN quarter BETWEEN 10 AND 12 THEN 1 ELSE 0 END) AS Q4
FROM
(SELECT
id,
TRIM(SUBSTRING(month_id, 1, 4)) AS year,
TRIM(regexp_replace(SUBSTR(month_id, 5, 4), "-", "")) as quarter
FROM
test.patientid) a
GROUP BY
a.id, a.year
我想你正在寻找这样的东西:
select y.yyyy, q.q,
(case when count(t.month_dt) > 0 then 1 else 0 end) as activity_flag
from (select distinct year(month_dt) as yyyy from t) y cross join
(select distinct quarter(month_dt) as q from t) q left join
t
on year(t.month_dt) = y.yyyy and quarter(t.month_dt) = q.q
group by y.yyyy, q.q;
这假设一年中的每个季度至少有一个 activity(无论是哪个季度)。否则,您只需输入 1、2、3 和 4 的列表即可获得宿舍。
@巴布;如果您的配置单元版本中不存在季度功能,我有一个备用功能可以在给定日期获取季度。希望这可以帮助。谢谢!
create table qtrs(qtr int);
insert into qtrs values (1),(2),(3),(4);
create table ims
(id int,
month_dt date
);
insert into ims values
(100, '2012-03-01'),
(100, '2013-04-01'),
(100, '2013-01-01'),
(100, '2013-07-01'),
(100, '2014-01-01'),
(100, '2014-05-01'),
(100, '2014-06-01'),
(100, '2014-12-01'),
(100, '2015-02-01'),
(100, '2015-09-01'),
(100, '2015-11-01'),
(100, '2016-05-01'),
(100, '2016-10-01');
insert into ims values
(200, '2012-03-01'),
(200, '2013-04-01');
Query:
select DISTINCT NVL(ims.id, qtr.id) as id,qtr.year as year,qtr.qtr as qtr,
IF(ims.id is null, 0, 1) as activity
from jbacoy.ims ims
right join (select distinct ims.id,YEAR(ims.month_dt) as year,qtrs.qtr from jbacoy.ims ims join jbacoy.qtrs qtrs) qtr
on (ims.id=qtr.id and year(ims.month_dt)=qtr.year and int((month(month_dt)-1)/3)+1=qtr.qtr)
sort by id, year, qtr;
Result:
id year qtr activity
100 2012 1 1
100 2012 2 0
100 2012 3 0
100 2012 4 0
100 2013 1 1
100 2013 2 1
100 2013 3 1
100 2013 4 0
100 2014 1 1
100 2014 2 1
100 2014 3 0
100 2014 4 1
100 2015 1 1
100 2015 2 0
100 2015 3 1
100 2015 4 1
100 2016 1 0
100 2016 2 1
100 2016 3 0
100 2016 4 1
200 2012 1 1
200 2012 2 0
200 2012 3 0
200 2012 4 0
200 2013 1 0
200 2013 2 1
200 2013 3 0
200 2013 4 0
我们有两列 Id
和 month Id
。
我正在寻找的输出是根据季度粒度将年与月 Id 分开。 activity 列应该来自季度。如果 id 处于活动状态 activity 应该是 1 否则 0 。如果 id 出现在第一季度的任何一个(例如:只有 1),则 activity 仍然是 1 。
像这样:
id month_dt
-----------------------------------
1000000000 2012-03-01 00:00:00.0
1000000000 2015-09-01 00:00:00.0
1000000000 2016-10-01 00:00:00.0
1000000000 2015-11-01 00:00:00.0
1000000000 2014-01-01 00:00:00.0
1000000000 2013-04-01 00:00:00.0
1000000000 2014-12-01 00:00:00.0
1000000000 2015-02-01 00:00:00.0
1000000000 2014-06-01 00:00:00.0
1000000000 2013-01-01 00:00:00.0
1000000000 2014-05-01 00:00:00.0
1000000000 2016-05-01 00:00:00.0
1000000000 2013-07-01 00:00:00.0
期望值:
ID YEAR QTR ACTIVITY (1 or 0)
--------------------------------------------------
1000000000 2012 1 1
1000000000 2012 2 0
1000000000 2012 3 0
1000000000 2012 4 0
1000000000 2013 1 1
1000000000 2013 2 1
1000000000 2013 3 1
1000000000 2013 4 0
下面是我试过的方法,但没有 return 预期的结果。请帮我实现这个
SELECT
a.id, a.year,
SUM(CASE WHEN quarter BETWEEN 1 AND 3 THEN 1 ELSE 0 END) AS Q1,
SUM(CASE WHEN quarter BETWEEN 4 AND 6 THEN 1 ELSE 0 END) AS Q2,
SUM(CASE WHEN quarter BETWEEN 7 AND 9 THEN 1 ELSE 0 END) AS Q3,
SUM(CASE WHEN quarter BETWEEN 10 AND 12 THEN 1 ELSE 0 END) AS Q4
FROM
(SELECT
id,
TRIM(SUBSTRING(month_id, 1, 4)) AS year,
TRIM(regexp_replace(SUBSTR(month_id, 5, 4), "-", "")) as quarter
FROM
test.patientid) a
GROUP BY
a.id, a.year
我想你正在寻找这样的东西:
select y.yyyy, q.q,
(case when count(t.month_dt) > 0 then 1 else 0 end) as activity_flag
from (select distinct year(month_dt) as yyyy from t) y cross join
(select distinct quarter(month_dt) as q from t) q left join
t
on year(t.month_dt) = y.yyyy and quarter(t.month_dt) = q.q
group by y.yyyy, q.q;
这假设一年中的每个季度至少有一个 activity(无论是哪个季度)。否则,您只需输入 1、2、3 和 4 的列表即可获得宿舍。
@巴布;如果您的配置单元版本中不存在季度功能,我有一个备用功能可以在给定日期获取季度。希望这可以帮助。谢谢!
create table qtrs(qtr int);
insert into qtrs values (1),(2),(3),(4);
create table ims
(id int,
month_dt date
);
insert into ims values
(100, '2012-03-01'),
(100, '2013-04-01'),
(100, '2013-01-01'),
(100, '2013-07-01'),
(100, '2014-01-01'),
(100, '2014-05-01'),
(100, '2014-06-01'),
(100, '2014-12-01'),
(100, '2015-02-01'),
(100, '2015-09-01'),
(100, '2015-11-01'),
(100, '2016-05-01'),
(100, '2016-10-01');
insert into ims values
(200, '2012-03-01'),
(200, '2013-04-01');
Query:
select DISTINCT NVL(ims.id, qtr.id) as id,qtr.year as year,qtr.qtr as qtr,
IF(ims.id is null, 0, 1) as activity
from jbacoy.ims ims
right join (select distinct ims.id,YEAR(ims.month_dt) as year,qtrs.qtr from jbacoy.ims ims join jbacoy.qtrs qtrs) qtr
on (ims.id=qtr.id and year(ims.month_dt)=qtr.year and int((month(month_dt)-1)/3)+1=qtr.qtr)
sort by id, year, qtr;
Result:
id year qtr activity
100 2012 1 1
100 2012 2 0
100 2012 3 0
100 2012 4 0
100 2013 1 1
100 2013 2 1
100 2013 3 1
100 2013 4 0
100 2014 1 1
100 2014 2 1
100 2014 3 0
100 2014 4 1
100 2015 1 1
100 2015 2 0
100 2015 3 1
100 2015 4 1
100 2016 1 0
100 2016 2 1
100 2016 3 0
100 2016 4 1
200 2012 1 1
200 2012 2 0
200 2012 3 0
200 2012 4 0
200 2013 1 0
200 2013 2 1
200 2013 3 0
200 2013 4 0