简体   繁体   English

在Google表格查询中选择每月的第一天

[英]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.

相关问题 Google 表格中一个月的最后一天和第一天 - The last & the first day of a month in Google Sheets 如何在谷歌表格中自动创建月历。 月份的第一天从(名称)日的列开始 - How to auto create a Month calendar in google sheets. Where first day of the month start in the colum of the (name)day Google表格使用涉及month()的绝对引用选择查询 - Google Sheets Select Query using Absolute reference involving month() 给定月份和年份的谷歌工作表中按天计算的总计 - Total by day in google sheets for a given month and year Google表格= MONTH有效,但QUERY中的MONTH无效 - Google Sheets =MONTH works but MONTH in QUERY does not Google表格查询从包含日期和时间作为字符串的一组行中选择年,月和日 - Google sheets query selecting year, month and day from a group of rows containing date and time as string 谷歌表格 - 查询每个月的最小值 - Google Sheets - Query Minimum value for each month Google表格,如何根据每月的日期设置单元格颜色的格式 - Google Sheets, how to format cell color based on day of month 谷歌表查询第一行乱七八糟 - google sheets query first row mess 谷歌表格中的查询第一个结果不是日期 - Query in google sheets first result is not a date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM