web-dev-qa-db-ja.com

1つの列の複数の行からの文字列を連結する方法+1つのクエリの内部結合

次の結果のクエリがあります:query:

SELECT Tasks.TaskId, Comments.Comment, comments.timespent       
FROM   comments
INNER JOIN tasks ON comments.entityid = tasks.taskid                
WHERE  ( comments.entity = 1 ) 
GROUP  BY Tasks.TaskId, Comments.Comment, comments.timespent

結果:

TaskID  Comment  TimeSpent
__________________________
111754    C1        4
111754    C2        1
111754    C3       79

次のように結果を取得するためのクエリの記述方法を教えてください。

TaskID  Comment          TimeSpent
__________________________________
111754  ,C1,C2,C3           84

前もって感謝します。

7
Saleem

動作するSQLフィドルは次のとおりです。 http://sqlfiddle.com/#!3/3597a/

これが実際に機能するSQLです。

SELECT Tasks.TaskId, SUBSTRING(
(SELECT ',' + Comments.Comment
FROM Comments
INNER JOIN tasks ON comments.entityid = tasks.taskid
FOR XML PATH('')),2,200000) AS Comments
, SUM(comments.timespent) AS TimeSpent      
FROM   comments
INNER JOIN tasks ON comments.entityid = tasks.taskid                
WHERE  ( comments.entity = 1 ) 
GROUP  BY Tasks.TaskId

テーブルを作成してデータを入力する

CREATE TABLE Tasks
(
  TaskID NVARCHAR(20) NOT NULL,
);

CREATE TABLE Comments
( 
  Entity  INT NOT NULL,
  EntityID NVARCHAR(20) NOT NULL,
  Comment NVARCHAR(50) NOT NULL,
  TimeSpent INT NOT NULL
);


INSERT INTO Tasks VALUES
( '111754' );

INSERT INTO Comments VALUES
(1,'111754', 'C1',4 ),
(1,'111754', 'C2',1 ),
(1,'111754', 'C3',79 );

SQLを実行する

SELECT Tasks.TaskId, SUBSTRING(
(SELECT ',' + Comments.Comment
FROM Comments
INNER JOIN tasks ON comments.entityid = tasks.taskid
FOR XML PATH('')),2,200000) AS Comments
, SUM(comments.timespent) AS TimeSpent     
FROM   comments
INNER JOIN tasks ON comments.entityid = tasks.taskid                
WHERE  comments.entity = 1 
GROUP  BY Tasks.TaskId

結果を表示します。

TASKID  COMMENTS    TIMESPENT
111754  C1,C2,C3    84
12
Shiva

次のようなXMLパスを使用してCROSSAPPLYを使用できます。

   `
 Select *
   from table1 t1
     CROSS APPLY
      (
 SELECT
    [text()] = t.[Name] + '; '
    FROM table2 t2
    WHERE t1.[Id] = t2.[Id]
   ORDER BY t2.name
   FOR XML PATH('')
       ) a (Type)
`
2
Maverick

調べる必要があります FOR XML PATH

2
AJ.

わかりました、これは少し複雑ですが、xmlを使用せず、SQLサーバー以外のデータベースで使用できます。

    WITH orig
         AS (SELECT 1 AS f1, 'C11' AS f2
             UNION ALL
             SELECT 1 AS f1, 'C12' AS f2
             UNION ALL
             SELECT 1 AS f1, 'C13' AS f2
             UNION ALL
             SELECT 2 AS f1, 'C21' AS f2
             UNION ALL
             SELECT 2 AS f1, 'C22' AS f2
             UNION ALL
             SELECT 2 AS f1, 'C23' AS f2
             UNION ALL
             SELECT 3 AS f1, 'C31' AS f2)
       , orig2 AS (SELECT DISTINCT f1, f2 FROM orig)
       , orig3 AS (SELECT f1, f2, row_number() OVER(PARTITION BY f1 ORDER BY f2) AS RowNum FROM orig2)
       , orig4
         -- Use recursion to concatenate the fields
         AS (SELECT f1, CONVERT(VARCHAR(MAX), f2) AS val, rownum
               FROM orig3
              WHERE RowNum = 1
             UNION ALL
             SELECT orig4.f1, orig4.val + ', ' + orig3.f2 AS val, orig3.rownum
               FROM orig4
                    INNER JOIN orig3
                      ON orig4.RowNum + 1 = orig3.RowNum
                     AND orig4.f1 = orig3.f1)
    SELECT *
      FROM orig4
     -- select only the rows that match the maximum rownum
     WHERE NOT EXISTS
             (SELECT 1
                FROM orig4 o44
               WHERE o44.f1 = orig4.f1
                 AND o44.rownum > orig4.rownum)

SQL Serverでのみ機能する別のアプローチは、値を連結する集約CLR関数を作成することです。 http://msdn.Microsoft.com/en-us/library/91e6taax%28v=vs.90%29 .aspx

この記事に出くわしたがOracleを使用している場合は、上記のクエリを使用するか、pl/sqlでカスタム集計関数を定義するかを選択できます( http://docs.Oracle.com/cd/B28359_01/appdev .111/b28425/aggr_functions.htm )。

1
costa