Thursday, 15 January 2015

sql server - How to group multiple values into a single column in SQL -



sql server - How to group multiple values into a single column in SQL -

this question has reply here:

how can combine multiple rows comma-delimited list in oracle? [duplicate] 11 answers

given next sql

select t1."pn" "part number", t2."qty" "quantity", t2."branch" "location", t3."stock" "bin" "xyz"."parts" t1, "xyz"."balances" t2, "xyz"."details" t3 (t2."part_id" = t1."part_id") , (t3."part_id" = t1."part_id") order "part number" asc, "location" asc

we results such as

yz-7-ca-080 88 01 stock7 yz-7-ca-080 88 01 03482 yz-7-ca-080 88 01 a8k2d

for location 01, there 88 pieces of part number yz-7-ca-080 , can found in of 3 bins stock7, 03482, or a8k2d. location value refers mutual branch warehouse , quantity entire warehouse, not bins.

i need alter output can write out instead 1 entry bins list

yz-7-ca-080 88 01 stock7,03482,a8k2d

so looking way refactoring of results in sql. sense there should way utilize function or subquery or , hoping there single multi-db solution assume there need different solutions on different dbs. (oracle primary solution trying solve secondary priority db need sql server).

note: there multiple locations per part number not plenty set distinct on first column cut down multiple part number entries. there multiple of same part number @ location 02 same issue.

ideas?

for older versions, guess wm_concat work. modifying gordon linoff's query:

select t1."pn" "part number", max(t2."qty") "quantity", t2."branch" "location", wm_concat(t3."stock") bins "xyz"."parts" t1 bring together "xyz"."balances" t2 on t2."part_id" = t1."part_id" bring together "xyz"."details" t3 on t3."part_id" = t1."part_id" grouping t1.pn, t2.branch order "part number", "location";

also refer this link alternate approach: including reply in link refernce:

create table countries ( country_name varchar2 (100)); insert countries values ('albania'); insert countries values ('andorra'); insert countries values ('antigua'); select substr (sys_connect_by_path (country_name , ','), 2) csv (select country_name , row_number () on (order country_name ) rn, count (*) on () cnt countries) rn = cnt start rn = 1 connect rn = prior rn + 1; csv -------------------------- albania,andorra,antigua

sql sql-server oracle

No comments:

Post a Comment