我有一个包含〜百万个条目的表,如下所示:
customer_id | purchased_at | product
1 | 2012-06-01 00:00 | apples
1 | 2012-09-02 00:00 | apples
1 | 2012-10-01 00:00 | pears
2 | 2012-06-01 00:00 | apples
2 | 2012-07-01 00:00 | apples
3 | 2012-09-02 00:00 | pears
3 | 2012-10-01 00:00 | apples
3 | 2012-10-01 01:00 | bananas
Run Code Online (Sandbox Code Playgroud)
我想将产品连接到一行,DISTINCT和purchase_at的顺序
在MySQL中我只是使用
select customer_id, min(purchased_at) as first_purchased_at,
group_concat(DISTINCT product order by purchased_at) as all_purchased_products
from purchases group by customer_id;
Run Code Online (Sandbox Code Playgroud)
要得到
customer_id | first_purchased_at | all_purchased_products
1 …Run Code Online (Sandbox Code Playgroud) 我有一张看起来像这样的表:

这个表包含343行.
我正在尝试对它运行此查询:
create table newTest2
select function_name, service_name, min(concurrency), substring_index(group_concat(date order by concurrency ), ',',1) as minDate,
max(concurrency), substring_index(group_concat(date order by concurrency desc), ',',1) as maxDate , avg(concurrency)
from conc_intermidate
group by function_name,service_name;
Run Code Online (Sandbox Code Playgroud)
当我运行查询时,它给了我:"行203被GROUP_CONCAT()剪切",我不知道为什么它给了我这个错误.请帮忙!谢谢...
我有下表:
Code Ref Value
A1 Car A
A1 Car -
A1 Car B
B2 Truck CC
B2 Truck D
B2 Truck -
C3 Van E
C3 Van F
C3 Van -
C3 Van G
Run Code Online (Sandbox Code Playgroud)
我试图实现的目标是一个将所有值组合在一起的连接字符串,如下所示:
Code Ref Value
A1 Car A-B
B2 Truck CCD-
C3 Van EF-G
Run Code Online (Sandbox Code Playgroud)
我离开了这里的例子,但一无所获。这是我想出的:
SELECT [Table].[Code]
, [Table].[Ref]
, STUFF((SELECT DISTINCT [Value]
FROM [Table2]
FOR XML PATH ('')),1, 1,'') AS Values
FROM [Table]
LEFT JOIN [Table2] ON
[Table2].[Code] = [Table].[Code]
Run Code Online (Sandbox Code Playgroud)
我哪里错了?有没有更有效的方法来做到这一点?
sql t-sql sql-server sql-server-2008-r2 sql-server-group-concat
我去桌子了。
TABLE 1: [..fields..] [CATEGORIE] [..fields..]
TABLE 2: [..fields..] [ID] [CATEGORIE] [..fields..]
我想连接有点特殊,并尝试这样:
SELECT [..other fields..], CATEGORIE, (SELECT ID FROM TABLE2 WHERE TABLE2.CATEGORIE = TABLE1.CATEGORIE) FROM TABLE1;
我想在主查询的列中有子查询的ID
这样([]代表列)
[resultfield1] [resultfield2] [resultfield3] [ID1,ID2,ID3,ID4,...]
有没有办法负担得起?
非常感谢您的帮助,
提前致谢