[英]select first day of month in google sheets query
I am trying to construct a query that would among other columns return a date, which is in column J of an "rawData" table and change this date to the first day of month. 我正在尝试构造一个查询,该查询将在其他列中返回一个日期,该日期位于“ rawData”表的J列中,并将此日期更改为月份的第一天。
In my query all date statements but the last one are for testing purposes. 在我的查询中,除最后一个语句外,所有日期语句都是出于测试目的。 The last one causes an error (Unknown range name: 'J'.).
最后一个导致错误(未知范围名称:“ J”。)。 Anybody knows where is the mistake?
有人知道错误在哪里吗? This is my query:
这是我的查询:
=query(rawData;"select C, YEAR(J), MONTH(J)+1, date'2010-08-31', date '"&TEXT(2010-10-1;"yyyy-mm-dd")&"', date '"&TEXT(CONCATENATE("2020-12";"-01");"yyyy-mm-dd")&"', date '"&TEXT(CONCATENATE(YEAR(J);"-";MONTH(J);"-01");"yyyy-mm-dd")&"', SUM(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C, YEAR(J), MONTH(J)+1";1 ) = query(rawData;“选择C,YEAR(J),MONTH(J)+1,日期'2010-08-31',日期'”&TEXT(2010-10-1;“ yyyy-mm-dd”)& “',日期'”&TEXT(CONCATENATE(“ 2020-12”;“-01”);“ yyyy-mm-dd”)&“',日期'”&TEXT(CONCATENATE(YEAR(J);“-”; MONTH(J);“-01”);“ yyyy-mm-dd”)&“',SUM(L)WHERE(C <>'')AND(F = 18 OR F = 20 OR F = 62)组通过C,YEAR(J),MONTH(J)+1“; 1)
Thanks, Andrej 谢谢,安德烈
First, refer the documentation for the Query Language Reference, particular the use of [Scalar Functions] ( https://developers.google.com/chart/interactive/docs/querylanguage#scalar-functions ). 首先,请参阅《查询语言参考》的文档,特别是[标量函数]的用法( https://developers.google.com/chart/interactive/docs/querylanguage#scalar-functions )。 The options here are very limited and do not appear to permit the calculation of the 'first of the month' within the query, per se.
此处的选项非常有限,并且似乎不允许其本身在查询中计算“月初”。
It is possible to build the 'first of the month' within the rawData sheet, and I'll demonstrate the process, but I'll think you will agree that it doesn't achieve any perceivable advantage. 可以在rawData工作表中构建“每月的第一个月”,我将演示该过程,但是我认为您会同意它没有实现任何明显的优势。
Try this. 尝试这个。
Helper Column P 助手列P
1) Create a helper column "P" in rawData. 1)在rawData中创建一个辅助列“ P”。 In cell P2, enter this formula:
在单元格P2中,输入以下公式:
=arrayformula(IF(ISBLANK(J2:J);"";DATE(Year(J2:J);Month(J2:J);1)))
Select the entire column and format it as Date 'yyyy-mm-dd'. 选择整个列并将其格式化为Date'yyyy-mm-dd'。
This is your "first of the month date". 这是您的“月初日期”。 For identification, enter "Date yyyy-mm-dd" in Cell P1.
为了进行识别,请在单元格P1中输入“日期yyyy-mm-dd”。
Query#1 查询#1
2) Create a new sheet for testing. 2)创建一个新的表格进行测试。 In cell A1 enter this formula:
在单元格A1中输入以下公式:
=query(rawData!A2:P;"Select C, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C label C 'Reg_st_vozila',sum(L) 'Total'")
. =query(rawData!A2:P;"Select C, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C label C 'Reg_st_vozila',sum(L) 'Total'")
。
This is the product code and sum by quantity, grouped by Product Code; 这是产品代码和数量的总和,按产品代码分组; you should have 24 records.
您应该有24条记录。
Query#2 查询#2
3) In the same new sheet, in cell D1 enter this formula 3)在同一张新工作表的单元格D1中,输入此公式
=query(rawData!A2:P;"Select C, P, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,P label C 'Reg_st_vozila', P 'Date', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code and First of the Month date (the date from the Helper Column). 这是产品代码和数量的总和,按产品代码和“月初”日期(“帮助者”列中的日期)分组。 You should have 1142 records.
您应该有1142条记录。
Query#3 查询#3
4) In the same new sheet, in cell H1 enter this formula: 4)在同一张新工作表的单元格H1中,输入以下公式:
=query(rawData!A2:P;"Select C, Year(J), Month(J)+1, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,Year(J),Month(J)+1 label C 'Reg_st_vozila', Year(J) 'Year', Month(J)+1 'Month', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code by Year by Month date. 这是产品代码和数量的总和,按产品代码按年份和月份日期分组。 You should have 1142 records.
您应该有1142条记录。
The output is identical to the preceding formula, though the query has an extra field (one for Year, one for Month). 输出与前面的公式相同,尽管查询有一个额外的字段(一个表示年份,一个表示月份)。 My point is that the 'first of month' doesn't achieve anything special.
我的观点是,“第一个月”并没有取得什么特别的成就。
Helper Column Q 辅助栏Q
5) Create a second helper column "Q" in rawData. 5)在rawData中创建第二个帮助器列“ Q”。 In cell Q2, enter this formula:
在单元格Q2中,输入以下公式:
=arrayformula(J2:J)
Select the entire column and format it as Date 'yyyy-mm'. 选择整个列并将其格式化为Date'yyyy-mm'。
This is a modification of the date column, but formatted for year-month. 这是对日期列的修改,但格式为年份-月份。 For identification, enter "Date yyyy-mm" in Cell Q1.
为了进行识别,请在单元格Q1中输入“日期yyyy-mm”。
Query#4 查询#4
6) In the new query sheet, in cell M1 enter this formula: 6)在新的查询表中,在单元格M1中输入以下公式:
=query(rawData!A2:Q;"Select C, Q, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,Q label C 'Reg_st_vozila', Q'Year/Month', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code by Year & Month. 这是产品代码和数量的总和,按产品代码按年和月分组。 You should have 1142 records.
您应该有1142条记录。 The output is identical to Query#2 but without the aggravation of calculating the "first of the month. Again, it shows that grouping on 'fom' doesn't achieve any perceivable advantage.
输出与Query#2相同,但不增加计算“第一个月”的时间。同样,它表明对'fom'进行分组并没有获得任何明显的优势。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.