-1

这是我到目前为止所拥有的,但它显示了太多的结果。它将描述行很好地组合在一起,但它是针对每个结果进行的。我基本上想要它,所以只有 3 行。每个班级一个http://prntscr.com/1la4j2

    /* 3. List the part number, part description, and item class for each pair of parts that are in the same item
class */
SELECT 
  t1.partnum,
  STUFF((
    SELECT ', ' + t2.description 
    FROM part t2
    WHERE t1.class = t2.class
    FOR XML PATH (''))
  ,1,2,'') AS description, t1.class
FROM part t1
4

1 回答 1

0

您可以从您的SELECT

或将 Partnum 和 Description 一起列出:

SELECT DISTINCT
  STUFF((
    SELECT ', ' t2.PartNum + '-' + t2.description 
    FROM part t2
    WHERE t1.class = t2.class
    FOR XML PATH (''))
  ,1,2,'') AS description, t1.class
FROM part t1

或者将partnum也放入一个单独的列表中,例如:

SELECT DISTINCT
  STUFF((
    SELECT ', ' + t3.partnum
    FROM part t3
    WHERE t1.class = t3.class
    FOR XML PATH (''))
  ,1,2,'') AS partnum
  ,STUFF((
    SELECT ', ' + t2.description 
    FROM part t2
    WHERE t1.class = t2.class
    FOR XML PATH (''))
  ,1,2,'') AS description
  , t1.class
FROM part t1
于 2013-08-14T22:10:05.393 回答