Google Sheet 查询数据透视表 - 计算日期是否在日期范围内
Google Sheet Query Pivot - count if a date is between a range of dates
https://docs.google.com/spreadsheets/d/1AQUNPb4d3EqeSZb9SwfzemR7MfSIkMFkDAZ8rPLA3rc/edit?usp=sharing
我有 3 列:
- 城市
- 开始日期
- 结束日期
我要 3 个 table :
- 枢轴 Table 年内进入的城市(完成)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where year(Col2)=2018 or year(Col2)=2019 or year(Col2)=2020
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Start'";1)
以 Table 年内离开的人为中心的城市(完成)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where (year(Col3)=2018 or year(Col3)=2019 or year(Col3)=2020)
group by Col1
pivot year(Col3)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'End'";1)
- 以 Table 城市为中心,有一年内逗留的人(失败)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where
(2018>=YEAR(Col2) and 2018<=YEAR(Col3) or
(2019>=YEAR(Col2) and 2019<=YEAR(Col3) or
(2020>=YEAR(Col2) and 2020<=YEAR(Col3)
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Between'";1)
对于最后一个,我遇到了麻烦。
我想我的 Where 条件没有适应,我的枢轴也没有工作。
我知道枢轴年 (Col2) 不能用于最后一个,因为如果一行的日期开始于 2015 年和结束于 2020 年,我希望它被计算在内,但我的枢轴不会出现2018 2019 2020.
有什么想法吗?
感谢您的宝贵时间
使用:
=ARRAYFORMULA(QUERY(QUERY(UNIQUE(SPLIT(FLATTEN(IF(DAYS(C2:C10; B2:B10)>=
SEQUENCE(1; 5000; ); ROW(A2:A10)&"×"&A2:A10&"×"&TEXT(B2:B10+SEQUENCE(1; 5000; );
"yyyy-1-1"); )); "×"));
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))
更新:
=ARRAYFORMULA(QUERY(QUERY(SPLIT(FLATTEN(IF(DATEDIF(B2:B; C2:C; "Y")>=
SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); ); ROW(A2:A)&"×"&A2:A&"×"&
YEAR(B2:B)+SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); )&"-1-1"; )); "×");
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))
https://docs.google.com/spreadsheets/d/1AQUNPb4d3EqeSZb9SwfzemR7MfSIkMFkDAZ8rPLA3rc/edit?usp=sharing
我有 3 列:
- 城市
- 开始日期
- 结束日期
我要 3 个 table :
- 枢轴 Table 年内进入的城市(完成)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where year(Col2)=2018 or year(Col2)=2019 or year(Col2)=2020
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Start'";1)
以 Table 年内离开的人为中心的城市(完成)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where (year(Col3)=2018 or year(Col3)=2019 or year(Col3)=2020)
group by Col1
pivot year(Col3)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'End'";1)
- 以 Table 城市为中心,有一年内逗留的人(失败)
=query(QUERY({$A:$C};
"select Col1, count(Col1)
where
(2018>=YEAR(Col2) and 2018<=YEAR(Col3) or
(2019>=YEAR(Col2) and 2019<=YEAR(Col3) or
(2020>=YEAR(Col2) and 2020<=YEAR(Col3)
group by Col1
pivot year(Col2)");
"select * order by Col4 desc, Col3 desc, Col2 desc label Col1 'Between'";1)
对于最后一个,我遇到了麻烦。 我想我的 Where 条件没有适应,我的枢轴也没有工作。
我知道枢轴年 (Col2) 不能用于最后一个,因为如果一行的日期开始于 2015 年和结束于 2020 年,我希望它被计算在内,但我的枢轴不会出现2018 2019 2020.
有什么想法吗?
感谢您的宝贵时间
使用:
=ARRAYFORMULA(QUERY(QUERY(UNIQUE(SPLIT(FLATTEN(IF(DAYS(C2:C10; B2:B10)>=
SEQUENCE(1; 5000; ); ROW(A2:A10)&"×"&A2:A10&"×"&TEXT(B2:B10+SEQUENCE(1; 5000; );
"yyyy-1-1"); )); "×"));
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))
更新:
=ARRAYFORMULA(QUERY(QUERY(SPLIT(FLATTEN(IF(DATEDIF(B2:B; C2:C; "Y")>=
SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); ); ROW(A2:A)&"×"&A2:A&"×"&
YEAR(B2:B)+SEQUENCE(1; MAX(DATEDIF(B2:B; C2:C; "Y")); )&"-1-1"; )); "×");
"select Col2,count(Col2)
where year(Col3) matches '2018|2019|2020'
group by Col2
pivot year(Col3)
label Col2'Between'");
"order by Col4 desc, Col3 desc, Col2 desc"))