select子句中的SQL逗号分隔列表

时间:2016-06-10 04:51:56

标签: sql sql-server-2008 subquery

我有以下表格

表A

--------------------------
part no  description
-----------------------
001      Brake
002      Clutch
003      Alternator

表B

--------------------------
Vehicle_id  brand  Part_No
-----------------------
42          Honda        001
34          Toyota       002
42          Honda        003
34          Toyota       001

我想写一个将返回以下内容的查询

Vehicle_id   brand    parts
42           Honda    Brake,Alternator

任何想法

1 个答案:

答案 0 :(得分:0)

尝试这个

SELECT B.VID,
  B.BRND,
  WMSYS.WM_CONCAT(A.DESCP) PARTS
FROM
  (SELECT 001 prtno,'Brake' Descp FROM dual
  UNION ALL
  SELECT 002 prtno,'Clutch' Descp FROM dual
  UNION ALL
  SELECT 003 prtno,'Alternator' Descp FROM dual
  )a,
  (SELECT 42 VID,'Honda' Brnd,001 prt_no FROM dual
  UNION ALL
  SELECT 34 VID,'Toyota' Brnd,002 prt_no FROM dual
  UNION ALL
  SELECT 42 VID,'Honda' Brnd,003 prt_no FROM dual
  UNION ALL
  SELECT 34 VID,'Toyota' Brnd,001 prt_no FROM dual
  )b
WHERE A.PRTNO = B.PRT_NO GROUP BY B.VID,
  B.BRND;