T SQL Group column with accumulating data in one record

merzidaruvala

I am trying to write a query on SQL Server to merge data items in 1 record

Input table:

sales_ref_no    Description
001 Hello
001 Hi
002 Dear
002 All
002 Please
003 Thanks

Output table:

sales_ref_no    Description
001 Hello | Hi
002 Dear | All | Please
003 Thanks

The description under the same sales_ref_no is accumulated under the same record using a | delimiter

Can anyone help?

Thanks!

Palanikumar
Select 
  sales_ref_no,
  STUFF((
  SELECT ' | ' + B.Description
   FROM YOUR_TABLE B
   WHERE (B.sales_ref_no = A.sales_ref_no) 
   FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS Description
From YOUR_TABLE A
Group By sales_ref_no

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Group a dataframe on one column and take max from one column and its corresponding value from the other col

分類Dev

One value for each df Column group

分類Dev

Retrieve sorted data based on one numeric column in SQL

分類Dev

SQL Server : group by column with another column

分類Dev

SQL count occurrence of every record (integer) in one table with multiple columns

分類Dev

sql select last but one record from all group

分類Dev

Why when using MIN function and selecting another column, we require GROUP BY clause? Doesn't MIN return single record?

分類Dev

SQL to group by and select a column when not null

分類Dev

How to group by on selected column in sql?

分類Dev

R Data.Table Mode Imputation First Record By Group

分類Dev

Python - Can't apply Data Validation to column group

分類Dev

How to select multiple columns and group by one column

分類Dev

DataFrame: Group by one column and average other columns

分類Dev

SQL Group by but record Relation

分類Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

分類Dev

Oracle - SQL query to bin the data from one table to another based on a column value?

分類Dev

Oracle - SQL query to bin the data from one table to another based on a column value?

分類Dev

SQL Select/Group by a list of column names

分類Dev

SQL how to ORDER BY Statement in one column

分類Dev

Trying to find multiple values from one column and group by another column - can't seem to figure it out

分類Dev

select a record from each group if it has given value in column otherwise any one record

分類Dev

Converting Group By T-Sql To Linq To Sql

分類Dev

MYSQL condense group by two columns into group by one column with breakdown

分類Dev

Show One Column Data as Two Columns in SQL

分類Dev

Merge data into one column - sql server 2012

分類Dev

How to delete duplicate record which has one unique column

分類Dev

how to insert same record in one column but different row in mysql?

分類Dev

Populate one column with data from a list and match other column data

分類Dev

SQL sum data between 2 dates in one column

Related 関連記事

  1. 1

    Group a dataframe on one column and take max from one column and its corresponding value from the other col

  2. 2

    One value for each df Column group

  3. 3

    Retrieve sorted data based on one numeric column in SQL

  4. 4

    SQL Server : group by column with another column

  5. 5

    SQL count occurrence of every record (integer) in one table with multiple columns

  6. 6

    sql select last but one record from all group

  7. 7

    Why when using MIN function and selecting another column, we require GROUP BY clause? Doesn't MIN return single record?

  8. 8

    SQL to group by and select a column when not null

  9. 9

    How to group by on selected column in sql?

  10. 10

    R Data.Table Mode Imputation First Record By Group

  11. 11

    Python - Can't apply Data Validation to column group

  12. 12

    How to select multiple columns and group by one column

  13. 13

    DataFrame: Group by one column and average other columns

  14. 14

    SQL Group by but record Relation

  15. 15

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  16. 16

    Oracle - SQL query to bin the data from one table to another based on a column value?

  17. 17

    Oracle - SQL query to bin the data from one table to another based on a column value?

  18. 18

    SQL Select/Group by a list of column names

  19. 19

    SQL how to ORDER BY Statement in one column

  20. 20

    Trying to find multiple values from one column and group by another column - can't seem to figure it out

  21. 21

    select a record from each group if it has given value in column otherwise any one record

  22. 22

    Converting Group By T-Sql To Linq To Sql

  23. 23

    MYSQL condense group by two columns into group by one column with breakdown

  24. 24

    Show One Column Data as Two Columns in SQL

  25. 25

    Merge data into one column - sql server 2012

  26. 26

    How to delete duplicate record which has one unique column

  27. 27

    how to insert same record in one column but different row in mysql?

  28. 28

    Populate one column with data from a list and match other column data

  29. 29

    SQL sum data between 2 dates in one column

ホットタグ

アーカイブ