Google表格:连接每个日期的所有项目

时间:2016-06-10 14:00:35

标签: sql google-sheets formula spreadsheet

我在Google表格中列出了日期和事件

| date     | eventName |
 ------------------
| 21/05/16 |   evt1    |
| 21/05/16 |   evt2    |
| 30/05/16 |   evt3    |
| 01/06/16 |   evt1    |
| 01/06/16 |   evt4    |

我想输出一个唯一日期列表,将当天的所有事件连接成1个字符串,如下所示:

| date     |     events  |
 ------------------------
| 21/05/16 |  evt1, evt2 |
| 30/05/16 |     evt3    |
| 01/06/16 |  evt1, evt4 |

我完全难倒,我想我需要某种查询,但无法弄清楚如何将事件名称字符串连接在一起。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

Since you tagged the question , I guess you were interested in using the QUERY function to accomplish this. I don't know how to do that, since any GROUP BY or PIVOT operation requires an aggregation function, and none of the supported aggregators can accomplish what you're asking.

Here's an alternative, using common formulas.

In your Google Spreadsheet, you can first use UNIQUE() to get the list of unique dates, and then a FILTER() in the next column to collect coincident events.

For example, here D2 contains the formula =UNIQUE($A$2:A), while E2 contains =join(", ",filter($B$2:$B,$A$2:$A=$D2)), which is copied down to following cells.

screenshot

答案 1 :(得分:1)

在sql server中:

我首先创建以下函数来连接单个日期的事件

CREATE FUNCTION [dbo].[EventsDate] (@Date date) RETURNS varchar(50)
BEGIN 
DECLARE @Events VARCHAR(50)=''
SELECT @Events = @Events + case when len(@Events)>0 then ',' else '' end + eventName
FROM  (select eventName from [Events] nolock where [date]=@Date) as S
RETURN @Events
END

然后请求:

select [date], [dbo].[EventsDate]([date]) as eventsName from [Events] nolock group by [date]

答案 2 :(得分:0)

也尝试这个公式:

={UNIQUE(FILTER(A2:A,A2:A>0)),TRANSPOSE(
   SPLIT(
      ", "&join(", ",
         ARRAYFORMULA(
            if(query(A:B,"select A where not A is null order by A",0)=
               query(A:B,"select A where not A is null order by A limit "&COUNT(query(A:B,"select A where not A is null",0))-1,1),"","|")
             & query(A:B,"select B where not A is null order by A",0)
             & " "
         )
      )
   ,", |",0)
)}

这是单一的配方解决方案。

相关问题