string-aggregation

CONCAT(column) OVER(PARTITION BY…)? Group-concatentating rows without grouping the result itself

半城伤御伤魂 提交于 2020-05-11 04:40:49
问题 I need a way to make a concatenation of all rows (per group) in a kind of window function like how you can do COUNT(*) OVER(PARTITION BY...) and the aggregate count of all rows per group will repeat across each particular group. I need something similar but a string concatenation of all values per group repeated across each group. Here is some example data and my desired result to better illustrate my problem: grp | val ------------ 1 | a 1 | b 1 | c 1 | d 2 | x 2 | y 2 | z And here is what I

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

こ雲淡風輕ζ 提交于 2020-02-06 20:16:56
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

牧云@^-^@ 提交于 2020-02-06 20:09:33
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

送分小仙女□ 提交于 2020-02-06 20:09:22
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

SQL unique combinations

跟風遠走 提交于 2020-02-04 04:09:11
问题 I have a table with three columns with an ID, a therapeutic class, and then a generic name. A therapeutic class can be mapped to multiple generic names. ID therapeutic_class generic_name 1 YG4 insulin 1 CJ6 maleate 1 MG9 glargine 2 C4C diaoxy 2 KR3 supplies 3 YG4 insuilin 3 CJ6 maleate 3 MG9 glargine I need to first look at the individual combinations of therapeutic class and generic name and then want to count how many patients have the same combination. I want my output to have three

Aggregate string values to a list

久未见 提交于 2020-01-30 12:01:39
问题 I am trying to work some very simple logic to transform an unpivoted column into what essentially amounts to a grouped list. However, having troubles doing this efficiently. Essentially, I have a data set that looks as follows: CUST_ID ORDER 1 Cake 1 Bread 2 Cake 3 Cake 3 Bread 3 Croissant 4 Croissant But would like to output it as follows: CUST_ID ORDERS 1 Cake 1 Bread, Cake 3 Cake, Bread, Croissant 4 Croissant I have tried subqueries (which I cannot get to work), but this seems brutal

Aggregate string values to a list

会有一股神秘感。 提交于 2020-01-30 12:00:51
问题 I am trying to work some very simple logic to transform an unpivoted column into what essentially amounts to a grouped list. However, having troubles doing this efficiently. Essentially, I have a data set that looks as follows: CUST_ID ORDER 1 Cake 1 Bread 2 Cake 3 Cake 3 Bread 3 Croissant 4 Croissant But would like to output it as follows: CUST_ID ORDERS 1 Cake 1 Bread, Cake 3 Cake, Bread, Croissant 4 Croissant I have tried subqueries (which I cannot get to work), but this seems brutal

From multiple rows to single column in Postgres

狂风中的少年 提交于 2020-01-23 13:00:27
问题 I am using Postgres 9.5 via pgAdmin 4 with read only access and im trying to write a select query that converts data from this form: +----------+-------------------+--------------------+----------------+ | username | filters | groups | roles | +----------+-------------------+--------------------+----------------+ | kd24 | Khaled <27607> | V1 | NewsStand User | | kd24 | Khaled <27607> | V1 | User | | kd24 | Khaled <27607> | Weekly KPIs | NewsStand User | | kd24 | Khaled <27607> | Detailed

how to stuff date and time in SQL

橙三吉。 提交于 2020-01-22 03:20:09
问题 i am having trouble stuffing the date and time for the same item in SQL sever. Below are three simple records. Date_Time ID 9/6/2018 5:36:30 PM 12345 9/6/2018 6:19:02 PM 12345 9/7/2018 4:15:55 AM 12345 I would like my result to be in 1 row and the Date_Time column got stuffed followed by a comma (,). Date_Time ID 9/6/2018 5:36:30 PM, 9/6/2018 6:19:02 PM, 9/7/2018 4:15:55 AM 12345 Would someone please point me to the right direction/link or a simple query to resolve the issue? Thanks, XH. 回答1:

How to concat strings in different row on Postgres

蓝咒 提交于 2020-01-22 02:35:07
问题 Let's say I have a table Foo and has a column name . I want to concatenate all names in Foo . For example Table Foo Name --------- name1 name2 name3 I want to write a query that returns name1name2name3 or if possible name1,name2,name3 . I have done some googling and see concat function but it only concats columns of same row. I couldn't find a function or a way to accomplish this. 回答1: use string_agg SELECT string_agg(Foo, ', ') AS col FROM tbl 来源: https://stackoverflow.com/questions/58743015